UNPKG

1.45 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.LoneSchemaDefinitionRule = LoneSchemaDefinitionRule;
7
8var _GraphQLError = require("../../error/GraphQLError.js");
9
10/**
11 * Lone Schema definition
12 *
13 * A GraphQL document is only valid if it contains only one schema definition.
14 */
15function LoneSchemaDefinitionRule(context) {
16 var _ref, _ref2, _oldSchema$astNode;
17
18 var oldSchema = context.getSchema();
19 var alreadyDefined = (_ref = (_ref2 = (_oldSchema$astNode = oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.astNode) !== null && _oldSchema$astNode !== void 0 ? _oldSchema$astNode : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getQueryType()) !== null && _ref2 !== void 0 ? _ref2 : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getMutationType()) !== null && _ref !== void 0 ? _ref : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getSubscriptionType();
20 var schemaDefinitionsCount = 0;
21 return {
22 SchemaDefinition: function SchemaDefinition(node) {
23 if (alreadyDefined) {
24 context.reportError(new _GraphQLError.GraphQLError('Cannot define a new schema within a schema extension.', node));
25 return;
26 }
27
28 if (schemaDefinitionsCount > 0) {
29 context.reportError(new _GraphQLError.GraphQLError('Must provide only one schema definition.', node));
30 }
31
32 ++schemaDefinitionsCount;
33 }
34 };
35}