UNPKG

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