@graphql-tools/graphql
Version:
Fork of GraphQL.js
27 lines (26 loc) • 1.31 kB
JavaScript
import { GraphQLError } from '../../error/GraphQLError.js';
/**
* Lone Schema definition
*
* A GraphQL document is only valid if it contains only one schema definition.
*/
export function LoneSchemaDefinitionRule(context) {
var _a, _b, _c;
const oldSchema = context.getSchema();
const alreadyDefined = (_c = (_b = (_a = oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.astNode) !== null && _a !== void 0 ? _a : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getQueryType()) !== null && _b !== void 0 ? _b : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getMutationType()) !== null && _c !== void 0 ? _c : oldSchema === null || oldSchema === void 0 ? void 0 : oldSchema.getSubscriptionType();
let schemaDefinitionsCount = 0;
return {
SchemaDefinition(node) {
if (alreadyDefined) {
context.reportError(new GraphQLError('Cannot define a new schema within a schema extension.', { nodes: node }));
return;
}
if (schemaDefinitionsCount > 0) {
context.reportError(new GraphQLError('Must provide only one schema definition.', {
nodes: node,
}));
}
++schemaDefinitionsCount;
},
};
}