UNPKG

3.62 kBJavaScriptView Raw
1import devAssert from "../jsutils/devAssert.mjs";
2import { Kind } from "../language/kinds.mjs";
3import { parse } from "../language/parser.mjs";
4import { assertValidSDL } from "../validation/validate.mjs";
5import { GraphQLSchema } from "../type/schema.mjs";
6import { specifiedDirectives } from "../type/directives.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 * and Mutation.
15 *
16 * Given that AST it constructs a GraphQLSchema. The resulting schema
17 * has no resolve methods, so execution will use default resolvers.
18 *
19 * Accepts options as a second argument:
20 *
21 * - commentDescriptions:
22 * Provide true to use preceding comments as the description.
23 *
24 */
25export function buildASTSchema(documentAST, options) {
26 documentAST != null && documentAST.kind === Kind.DOCUMENT || devAssert(0, 'Must provide valid Document AST.');
27
28 if ((options === null || options === void 0 ? void 0 : options.assumeValid) !== true && (options === null || options === void 0 ? void 0 : options.assumeValidSDL) !== true) {
29 assertValidSDL(documentAST);
30 }
31
32 var emptySchemaConfig = {
33 description: undefined,
34 types: [],
35 directives: [],
36 extensions: undefined,
37 extensionASTNodes: [],
38 assumeValid: false
39 };
40 var config = extendSchemaImpl(emptySchemaConfig, documentAST, options);
41
42 if (config.astNode == null) {
43 for (var _i2 = 0, _config$types2 = config.types; _i2 < _config$types2.length; _i2++) {
44 var type = _config$types2[_i2];
45
46 switch (type.name) {
47 // Note: While this could make early assertions to get the correctly
48 // typed values below, that would throw immediately while type system
49 // validation with validateSchema() will produce more actionable results.
50 case 'Query':
51 config.query = type;
52 break;
53
54 case 'Mutation':
55 config.mutation = type;
56 break;
57
58 case 'Subscription':
59 config.subscription = type;
60 break;
61 }
62 }
63 }
64
65 var directives = config.directives; // If specified directives were not explicitly declared, add them.
66
67 var _loop = function _loop(_i4) {
68 var stdDirective = specifiedDirectives[_i4];
69
70 if (directives.every(function (directive) {
71 return directive.name !== stdDirective.name;
72 })) {
73 directives.push(stdDirective);
74 }
75 };
76
77 for (var _i4 = 0; _i4 < specifiedDirectives.length; _i4++) {
78 _loop(_i4);
79 }
80
81 return new GraphQLSchema(config);
82}
83/**
84 * A helper function to build a GraphQLSchema directly from a source
85 * document.
86 */
87
88export function buildSchema(source, options) {
89 var document = parse(source, {
90 noLocation: options === null || options === void 0 ? void 0 : options.noLocation,
91 allowLegacySDLEmptyFields: options === null || options === void 0 ? void 0 : options.allowLegacySDLEmptyFields,
92 allowLegacySDLImplementsInterfaces: options === null || options === void 0 ? void 0 : options.allowLegacySDLImplementsInterfaces,
93 experimentalFragmentVariables: options === null || options === void 0 ? void 0 : options.experimentalFragmentVariables
94 });
95 return buildASTSchema(document, {
96 commentDescriptions: options === null || options === void 0 ? void 0 : options.commentDescriptions,
97 assumeValidSDL: options === null || options === void 0 ? void 0 : options.assumeValidSDL,
98 assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
99 });
100}