UNPKG

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