Initial commit

This commit is contained in:
Luis Fernando Molina committed 2018-09-03 15:19:57 +02:00
commit f47e08fdcb
2409 files changed
+363607

No files matched your search

@@ -0,0 +1,56 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import type { SDLValidationContext } from '../ValidationContext';
import { GraphQLError } from '../../error/GraphQLError';
import type { ASTVisitor } from '../../language/visitor';
export function schemaDefinitionNotAloneMessage(): string {
return 'Must provide only one schema definition.';
}
export function canNotDefineSchemaWithinExtensionMessage(): string {
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: SDLValidationContext,
): ASTVisitor {
const oldSchema = context.getSchema();
const alreadyDefined =
oldSchema &&
(oldSchema.astNode ||
oldSchema.getQueryType() ||
oldSchema.getMutationType() ||
oldSchema.getSubscriptionType());
let schemaDefinitionsCount = 0;
return {
SchemaDefinition(node) {
if (alreadyDefined) {
context.reportError(
new GraphQLError(canNotDefineSchemaWithinExtensionMessage(), node),
);
return;
}
if (schemaDefinitionsCount > 0) {
context.reportError(
new GraphQLError(schemaDefinitionNotAloneMessage(), node),
);
}
++schemaDefinitionsCount;
},
};
}