ExecutableDefinitions.js.flow 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // @flow strict
  2. import { GraphQLError } from '../../error/GraphQLError';
  3. import { Kind } from '../../language/kinds';
  4. import { type ASTVisitor } from '../../language/visitor';
  5. import { isExecutableDefinitionNode } from '../../language/predicates';
  6. import { type ASTValidationContext } from '../ValidationContext';
  7. export function nonExecutableDefinitionMessage(defName: string): string {
  8. return `The ${defName} definition is not executable.`;
  9. }
  10. /**
  11. * Executable definitions
  12. *
  13. * A GraphQL document is only valid for execution if all definitions are either
  14. * operation or fragment definitions.
  15. */
  16. export function ExecutableDefinitions(
  17. context: ASTValidationContext,
  18. ): ASTVisitor {
  19. return {
  20. Document(node) {
  21. for (const definition of node.definitions) {
  22. if (!isExecutableDefinitionNode(definition)) {
  23. context.reportError(
  24. new GraphQLError(
  25. nonExecutableDefinitionMessage(
  26. definition.kind === Kind.SCHEMA_DEFINITION ||
  27. definition.kind === Kind.SCHEMA_EXTENSION
  28. ? 'schema'
  29. : definition.name.value,
  30. ),
  31. definition,
  32. ),
  33. );
  34. }
  35. }
  36. return false;
  37. },
  38. };
  39. }