UNPKG

4.33 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.graphql = graphql;
7exports.graphqlSync = graphqlSync;
8
9var _validate = require('./type/validate');
10
11var _parser = require('./language/parser');
12
13var _validate2 = require('./validation/validate');
14
15var _execute = require('./execution/execute');
16
17/**
18 * This is the primary entry point function for fulfilling GraphQL operations
19 * by parsing, validating, and executing a GraphQL document along side a
20 * GraphQL schema.
21 *
22 * More sophisticated GraphQL servers, such as those which persist queries,
23 * may wish to separate the validation and execution phases to a static time
24 * tooling step, and a server runtime step.
25 *
26 * Accepts either an object with named arguments, or individual arguments:
27 *
28 * schema:
29 * The GraphQL type system to use when validating and executing a query.
30 * source:
31 * A GraphQL language formatted string representing the requested operation.
32 * rootValue:
33 * The value provided as the first argument to resolver functions on the top
34 * level type (e.g. the query object type).
35 * variableValues:
36 * A mapping of variable name to runtime value to use for all variables
37 * defined in the requestString.
38 * operationName:
39 * The name of the operation to use if requestString contains multiple
40 * possible operations. Can be omitted if requestString contains only
41 * one operation.
42 * fieldResolver:
43 * A resolver function to use when one is not provided by the schema.
44 * If not provided, the default field resolver is used (which looks for a
45 * value or method on the source value with the field's name).
46 */
47
48/* eslint-disable no-redeclare */
49/**
50 * Copyright (c) 2015-present, Facebook, Inc.
51 *
52 * This source code is licensed under the MIT license found in the
53 * LICENSE file in the root directory of this source tree.
54 *
55 *
56 */
57
58function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
59 var _arguments = arguments;
60
61 /* eslint-enable no-redeclare */
62 // Always return a Promise for a consistent API.
63 return new Promise(function (resolve) {
64 return resolve(
65 // Extract arguments from object args if provided.
66 _arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver));
67 });
68}
69
70/**
71 * The graphqlSync function also fulfills GraphQL operations by parsing,
72 * validating, and executing a GraphQL document along side a GraphQL schema.
73 * However, it guarantees to complete synchronously (or throw an error) assuming
74 * that all field resolvers are also synchronous.
75 */
76
77/* eslint-disable no-redeclare */
78function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
79 // Extract arguments from object args if provided.
80 var result = arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver);
81
82 // Assert that the execution was synchronous.
83 if (result.then) {
84 throw new Error('GraphQL execution failed to complete synchronously.');
85 }
86
87 return result;
88}
89
90function graphqlImpl(schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
91 // Validate Schema
92 var schemaValidationErrors = (0, _validate.validateSchema)(schema);
93 if (schemaValidationErrors.length > 0) {
94 return { errors: schemaValidationErrors };
95 }
96
97 // Parse
98 var document = void 0;
99 try {
100 document = (0, _parser.parse)(source);
101 } catch (syntaxError) {
102 return { errors: [syntaxError] };
103 }
104
105 // Validate
106 var validationErrors = (0, _validate2.validate)(schema, document);
107 if (validationErrors.length > 0) {
108 return { errors: validationErrors };
109 }
110
111 // Execute
112 return (0, _execute.execute)(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver);
113}
\No newline at end of file