UNPKG

2.29 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true,
5});
6exports.graphql = graphql;
7exports.graphqlSync = graphqlSync;
8
9var _devAssert = require('./jsutils/devAssert.js');
10
11var _isPromise = require('./jsutils/isPromise.js');
12
13var _parser = require('./language/parser.js');
14
15var _validate = require('./type/validate.js');
16
17var _validate2 = require('./validation/validate.js');
18
19var _execute = require('./execution/execute.js');
20
21function graphql(args) {
22 // Always return a Promise for a consistent API.
23 return new Promise((resolve) => resolve(graphqlImpl(args)));
24}
25/**
26 * The graphqlSync function also fulfills GraphQL operations by parsing,
27 * validating, and executing a GraphQL document along side a GraphQL schema.
28 * However, it guarantees to complete synchronously (or throw an error) assuming
29 * that all field resolvers are also synchronous.
30 */
31
32function graphqlSync(args) {
33 const result = graphqlImpl(args); // Assert that the execution was synchronous.
34
35 if ((0, _isPromise.isPromise)(result)) {
36 throw new Error('GraphQL execution failed to complete synchronously.');
37 }
38
39 return result;
40}
41
42function graphqlImpl(args) {
43 // Temporary for v15 to v16 migration. Remove in v17
44 arguments.length < 2 ||
45 (0, _devAssert.devAssert)(
46 false,
47 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
48 );
49 const {
50 schema,
51 source,
52 rootValue,
53 contextValue,
54 variableValues,
55 operationName,
56 fieldResolver,
57 typeResolver,
58 } = args; // Validate Schema
59
60 const schemaValidationErrors = (0, _validate.validateSchema)(schema);
61
62 if (schemaValidationErrors.length > 0) {
63 return {
64 errors: schemaValidationErrors,
65 };
66 } // Parse
67
68 let document;
69
70 try {
71 document = (0, _parser.parse)(source);
72 } catch (syntaxError) {
73 return {
74 errors: [syntaxError],
75 };
76 } // Validate
77
78 const validationErrors = (0, _validate2.validate)(schema, document);
79
80 if (validationErrors.length > 0) {
81 return {
82 errors: validationErrors,
83 };
84 } // Execute
85
86 return (0, _execute.execute)({
87 schema,
88 document,
89 rootValue,
90 contextValue,
91 variableValues,
92 operationName,
93 fieldResolver,
94 typeResolver,
95 });
96}