UNPKG

4.94 kBJavaScriptView Raw
1import isPromise from "./jsutils/isPromise.mjs";
2import { parse } from "./language/parser.mjs";
3import { validate } from "./validation/validate.mjs";
4import { validateSchema } from "./type/validate.mjs";
5import { execute } from "./execution/execute.mjs";
6/**
7 * This is the primary entry point function for fulfilling GraphQL operations
8 * by parsing, validating, and executing a GraphQL document along side a
9 * GraphQL schema.
10 *
11 * More sophisticated GraphQL servers, such as those which persist queries,
12 * may wish to separate the validation and execution phases to a static time
13 * tooling step, and a server runtime step.
14 *
15 * Accepts either an object with named arguments, or individual arguments:
16 *
17 * schema:
18 * The GraphQL type system to use when validating and executing a query.
19 * source:
20 * A GraphQL language formatted string representing the requested operation.
21 * rootValue:
22 * The value provided as the first argument to resolver functions on the top
23 * level type (e.g. the query object type).
24 * contextValue:
25 * The context value is provided as an argument to resolver functions after
26 * field arguments. It is used to pass shared information useful at any point
27 * during executing this query, for example the currently logged in user and
28 * connections to databases or other services.
29 * variableValues:
30 * A mapping of variable name to runtime value to use for all variables
31 * defined in the requestString.
32 * operationName:
33 * The name of the operation to use if requestString contains multiple
34 * possible operations. Can be omitted if requestString contains only
35 * one operation.
36 * fieldResolver:
37 * A resolver function to use when one is not provided by the schema.
38 * If not provided, the default field resolver is used (which looks for a
39 * value or method on the source value with the field's name).
40 * typeResolver:
41 * A type resolver function to use when none is provided by the schema.
42 * If not provided, the default type resolver is used (which looks for a
43 * `__typename` field or alternatively calls the `isTypeOf` method).
44 */
45
46export function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
47 var _arguments = arguments;
48
49 /* eslint-enable no-redeclare */
50 // Always return a Promise for a consistent API.
51 return new Promise(function (resolve) {
52 return resolve( // Extract arguments from object args if provided.
53 _arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
54 schema: argsOrSchema,
55 source: source,
56 rootValue: rootValue,
57 contextValue: contextValue,
58 variableValues: variableValues,
59 operationName: operationName,
60 fieldResolver: fieldResolver,
61 typeResolver: typeResolver
62 }));
63 });
64}
65/**
66 * The graphqlSync function also fulfills GraphQL operations by parsing,
67 * validating, and executing a GraphQL document along side a GraphQL schema.
68 * However, it guarantees to complete synchronously (or throw an error) assuming
69 * that all field resolvers are also synchronous.
70 */
71
72export function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
73 /* eslint-enable no-redeclare */
74 // Extract arguments from object args if provided.
75 var result = arguments.length === 1 ? graphqlImpl(argsOrSchema) : graphqlImpl({
76 schema: argsOrSchema,
77 source: source,
78 rootValue: rootValue,
79 contextValue: contextValue,
80 variableValues: variableValues,
81 operationName: operationName,
82 fieldResolver: fieldResolver,
83 typeResolver: typeResolver
84 }); // Assert that the execution was synchronous.
85
86 if (isPromise(result)) {
87 throw new Error('GraphQL execution failed to complete synchronously.');
88 }
89
90 return result;
91}
92
93function graphqlImpl(args) {
94 var schema = args.schema,
95 source = args.source,
96 rootValue = args.rootValue,
97 contextValue = args.contextValue,
98 variableValues = args.variableValues,
99 operationName = args.operationName,
100 fieldResolver = args.fieldResolver,
101 typeResolver = args.typeResolver; // Validate Schema
102
103 var schemaValidationErrors = validateSchema(schema);
104
105 if (schemaValidationErrors.length > 0) {
106 return {
107 errors: schemaValidationErrors
108 };
109 } // Parse
110
111
112 var document;
113
114 try {
115 document = parse(source);
116 } catch (syntaxError) {
117 return {
118 errors: [syntaxError]
119 };
120 } // Validate
121
122
123 var validationErrors = validate(schema, document);
124
125 if (validationErrors.length > 0) {
126 return {
127 errors: validationErrors
128 };
129 } // Execute
130
131
132 return execute({
133 schema: schema,
134 document: document,
135 rootValue: rootValue,
136 contextValue: contextValue,
137 variableValues: variableValues,
138 operationName: operationName,
139 fieldResolver: fieldResolver,
140 typeResolver: typeResolver
141 });
142}