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