UNPKG

2.98 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 * Copyright (c) 2015-present, Facebook, Inc.
19 *
20 * This source code is licensed under the MIT license found in the
21 * LICENSE file in the root directory of this source tree.
22 *
23 *
24 */
25function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
26 var _arguments = arguments;
27
28 /* eslint-enable no-redeclare */
29 // Always return a Promise for a consistent API.
30 return new Promise(function (resolve) {
31 return resolve( // Extract arguments from object args if provided.
32 _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));
33 });
34}
35/**
36 * The graphqlSync function also fulfills GraphQL operations by parsing,
37 * validating, and executing a GraphQL document along side a GraphQL schema.
38 * However, it guarantees to complete synchronously (or throw an error) assuming
39 * that all field resolvers are also synchronous.
40 */
41
42
43function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
44 /* eslint-enable no-redeclare */
45 // Extract arguments from object args if provided.
46 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); // Assert that the execution was synchronous.
47
48 if (result.then) {
49 throw new Error('GraphQL execution failed to complete synchronously.');
50 }
51
52 return result;
53}
54
55function graphqlImpl(schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) {
56 // Validate Schema
57 var schemaValidationErrors = (0, _validate.validateSchema)(schema);
58
59 if (schemaValidationErrors.length > 0) {
60 return {
61 errors: schemaValidationErrors
62 };
63 } // Parse
64
65
66 var document;
67
68 try {
69 document = (0, _parser.parse)(source);
70 } catch (syntaxError) {
71 return {
72 errors: [syntaxError]
73 };
74 } // Validate
75
76
77 var validationErrors = (0, _validate2.validate)(schema, document);
78
79 if (validationErrors.length > 0) {
80 return {
81 errors: validationErrors
82 };
83 } // Execute
84
85
86 return (0, _execute.execute)(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver);
87}
\No newline at end of file