ExecutableDefinitionsRule.js.flow 1.1 KB

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