UNPKG

3.39 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.graphql = graphql;
7exports.graphqlSync = graphqlSync;
8
9var _isPromise = _interopRequireDefault(require("./jsutils/isPromise.js"));
10
11var _parser = require("./language/parser.js");
12
13var _validate = require("./validation/validate.js");
14
15var _validate2 = require("./type/validate.js");
16
17var _execute = require("./execution/execute.js");
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
22 var _arguments = arguments;
23
24 /* eslint-enable no-redeclare */
25 // Always return a Promise for a consistent API.
26 return new Promise(function (resolve) {
27 return resolve( // Extract arguments from object args if provided.
28 _arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
29 schema: argsOrSchema,
30 source: source,
31 rootValue: rootValue,
32 contextValue: contextValue,
33 variableValues: variableValues,
34 operationName: operationName,
35 fieldResolver: fieldResolver,
36 typeResolver: typeResolver
37 }));
38 });
39}
40/**
41 * The graphqlSync function also fulfills GraphQL operations by parsing,
42 * validating, and executing a GraphQL document along side a GraphQL schema.
43 * However, it guarantees to complete synchronously (or throw an error) assuming
44 * that all field resolvers are also synchronous.
45 */
46
47
48function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
49 /* eslint-enable no-redeclare */
50 // Extract arguments from object args if provided.
51 var result = arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
52 schema: argsOrSchema,
53 source: source,
54 rootValue: rootValue,
55 contextValue: contextValue,
56 variableValues: variableValues,
57 operationName: operationName,
58 fieldResolver: fieldResolver,
59 typeResolver: typeResolver
60 }); // Assert that the execution was synchronous.
61
62 if ((0, _isPromise.default)(result)) {
63 throw new Error('GraphQL execution failed to complete synchronously.');
64 }
65
66 return result;
67}
68
69function graphqlImpl(args) {
70 var schema = args.schema,
71 source = args.source,
72 rootValue = args.rootValue,
73 contextValue = args.contextValue,
74 variableValues = args.variableValues,
75 operationName = args.operationName,
76 fieldResolver = args.fieldResolver,
77 typeResolver = args.typeResolver; // Validate Schema
78
79 var schemaValidationErrors = (0, _validate2.validateSchema)(schema);
80
81 if (schemaValidationErrors.length > 0) {
82 return {
83 errors: schemaValidationErrors
84 };
85 } // Parse
86
87
88 var document;
89
90 try {
91 document = (0, _parser.parse)(source);
92 } catch (syntaxError) {
93 return {
94 errors: [syntaxError]
95 };
96 } // Validate
97
98
99 var validationErrors = (0, _validate.validate)(schema, document);
100
101 if (validationErrors.length > 0) {
102 return {
103 errors: validationErrors
104 };
105 } // Execute
106
107
108 return (0, _execute.execute)({
109 schema: schema,
110 document: document,
111 rootValue: rootValue,
112 contextValue: contextValue,
113 variableValues: variableValues,
114 operationName: operationName,
115 fieldResolver: fieldResolver,
116 typeResolver: typeResolver
117 });
118}