1 | import { devAssert } from '../jsutils/devAssert.mjs';
|
2 | import { Kind } from '../language/kinds.mjs';
|
3 | import { parse } from '../language/parser.mjs';
|
4 | import { specifiedDirectives } from '../type/directives.mjs';
|
5 | import { GraphQLSchema } from '../type/schema.mjs';
|
6 | import { assertValidSDL } from '../validation/validate.mjs';
|
7 | import { extendSchemaImpl } from './extendSchema.mjs';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export 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 |
|
47 |
|
48 |
|
49 | case 'Query':
|
50 |
|
51 | config.query = type;
|
52 | break;
|
53 |
|
54 | case 'Mutation':
|
55 |
|
56 | config.mutation = type;
|
57 | break;
|
58 |
|
59 | case 'Subscription':
|
60 |
|
61 | config.subscription = type;
|
62 | break;
|
63 | }
|
64 | }
|
65 | }
|
66 |
|
67 | const directives = [
|
68 | ...config.directives,
|
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 |
|
79 |
|
80 |
|
81 |
|
82 | export 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 | }
|