UNPKG

3.19 kBJavaScriptView Raw
1import { devAssert } from '../jsutils/devAssert.mjs';
2import { Kind } from '../language/kinds.mjs';
3import { parse } from '../language/parser.mjs';
4import { specifiedDirectives } from '../type/directives.mjs';
5import { GraphQLSchema } from '../type/schema.mjs';
6import { assertValidSDL } from '../validation/validate.mjs';
7import { extendSchemaImpl } from './extendSchema.mjs';
8
9/**
10 * This takes the ast of a schema document produced by the parse function in
11 * src/language/parser.js.
12 *
13 * If no schema definition is provided, then it will look for types named Query,
14 * Mutation and Subscription.
15 *
16 * Given that AST it constructs a GraphQLSchema. The resulting schema
17 * has no resolve methods, so execution will use default resolvers.
18 */
19export function buildASTSchema(documentAST, options) {
20 (documentAST != null && documentAST.kind === Kind.DOCUMENT) ||
21 devAssert(false, 'Must provide valid Document AST.');
22
23 if (
24 (options === null || options === void 0 ? void 0 : options.assumeValid) !==
25 true &&
26 (options === null || options === void 0
27 ? void 0
28 : options.assumeValidSDL) !== true
29 ) {
30 assertValidSDL(documentAST);
31 }
32
33 const emptySchemaConfig = {
34 description: undefined,
35 types: [],
36 directives: [],
37 extensions: Object.create(null),
38 extensionASTNodes: [],
39 assumeValid: false,
40 };
41 const config = extendSchemaImpl(emptySchemaConfig, documentAST, options);
42
43 if (config.astNode == null) {
44 for (const type of config.types) {
45 switch (type.name) {
46 // Note: While this could make early assertions to get the correctly
47 // typed values below, that would throw immediately while type system
48 // validation with validateSchema() will produce more actionable results.
49 case 'Query':
50 // @ts-expect-error validated in `validateSchema`
51 config.query = type;
52 break;
53
54 case 'Mutation':
55 // @ts-expect-error validated in `validateSchema`
56 config.mutation = type;
57 break;
58
59 case 'Subscription':
60 // @ts-expect-error validated in `validateSchema`
61 config.subscription = type;
62 break;
63 }
64 }
65 }
66
67 const directives = [
68 ...config.directives, // If specified directives were not explicitly declared, add them.
69 ...specifiedDirectives.filter((stdDirective) =>
70 config.directives.every(
71 (directive) => directive.name !== stdDirective.name,
72 ),
73 ),
74 ];
75 return new GraphQLSchema({ ...config, directives });
76}
77/**
78 * A helper function to build a GraphQLSchema directly from a source
79 * document.
80 */
81
82export function buildSchema(source, options) {
83 const document = parse(source, {
84 noLocation:
85 options === null || options === void 0 ? void 0 : options.noLocation,
86 allowLegacyFragmentVariables:
87 options === null || options === void 0
88 ? void 0
89 : options.allowLegacyFragmentVariables,
90 });
91 return buildASTSchema(document, {
92 assumeValidSDL:
93 options === null || options === void 0 ? void 0 : options.assumeValidSDL,
94 assumeValid:
95 options === null || options === void 0 ? void 0 : options.assumeValid,
96 });
97}