UNPKG

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