33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
import { GraphQLError } from '../../error/GraphQLError';
|
|
export function schemaDefinitionNotAloneMessage() {
|
|
return 'Must provide only one schema definition.';
|
|
}
|
|
export function canNotDefineSchemaWithinExtensionMessage() {
|
|
return 'Cannot define a new schema within a schema extension.';
|
|
}
|
|
/**
|
|
* Lone Schema definition
|
|
*
|
|
* A GraphQL document is only valid if it contains only one schema definition.
|
|
*/
|
|
|
|
export function LoneSchemaDefinition(context) {
|
|
var oldSchema = context.getSchema();
|
|
var alreadyDefined = oldSchema && (oldSchema.astNode || oldSchema.getQueryType() || oldSchema.getMutationType() || oldSchema.getSubscriptionType());
|
|
var schemaDefinitionsCount = 0;
|
|
return {
|
|
SchemaDefinition: function SchemaDefinition(node) {
|
|
if (alreadyDefined) {
|
|
context.reportError(new GraphQLError(canNotDefineSchemaWithinExtensionMessage(), node));
|
|
return;
|
|
}
|
|
|
|
if (schemaDefinitionsCount > 0) {
|
|
context.reportError(new GraphQLError(schemaDefinitionNotAloneMessage(), node));
|
|
}
|
|
|
|
++schemaDefinitionsCount;
|
|
}
|
|
};
|
|
}
|