-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathgraphql.mjs
More file actions
59 lines (59 loc) · 1.9 KB
/
graphql.mjs
File metadata and controls
59 lines (59 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { isPromise } from "./jsutils/isPromise.mjs";
import { parse } from "./language/parser.mjs";
import { validateSchema } from "./type/validate.mjs";
import { validate } from "./validation/validate.mjs";
import { execute } from "./execution/execute.mjs";
export function graphql(args) {
// Always return a Promise for a consistent API.
return new Promise((resolve) => resolve(graphqlImpl(args)));
}
/**
* The graphqlSync function also fulfills GraphQL operations by parsing,
* validating, and executing a GraphQL document along side a GraphQL schema.
* However, it guarantees to complete synchronously (or throw an error) assuming
* that all field resolvers are also synchronous.
*/
export function graphqlSync(args) {
const result = graphqlImpl(args);
// Assert that the execution was synchronous.
if (isPromise(result)) {
throw new Error('GraphQL execution failed to complete synchronously.');
}
return result;
}
function graphqlImpl(args) {
const { schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver, hideSuggestions, } = args;
// Validate Schema
const schemaValidationErrors = validateSchema(schema);
if (schemaValidationErrors.length > 0) {
return { errors: schemaValidationErrors };
}
// Parse
let document;
try {
document = parse(source);
}
catch (syntaxError) {
return { errors: [syntaxError] };
}
// Validate
const validationErrors = validate(schema, document, undefined, {
hideSuggestions,
});
if (validationErrors.length > 0) {
return { errors: validationErrors };
}
// Execute
return execute({
schema,
document,
rootValue,
contextValue,
variableValues,
operationName,
fieldResolver,
typeResolver,
hideSuggestions,
});
}
//# sourceMappingURL=graphql.mjs.map