ExecutableDefinitionsRule.mjs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { GraphQLError } from '../../error/GraphQLError.mjs';
  2. import { Kind } from '../../language/kinds.mjs';
  3. import { isExecutableDefinitionNode } from '../../language/predicates.mjs';
  4. /**
  5. * Executable definitions
  6. *
  7. * A GraphQL document is only valid for execution if all definitions are either
  8. * operation or fragment definitions.
  9. *
  10. * See https://spec.graphql.org/draft/#sec-Executable-Definitions
  11. */
  12. export function ExecutableDefinitionsRule(context) {
  13. return {
  14. Document(node) {
  15. for (const definition of node.definitions) {
  16. if (!isExecutableDefinitionNode(definition)) {
  17. const defName =
  18. definition.kind === Kind.SCHEMA_DEFINITION ||
  19. definition.kind === Kind.SCHEMA_EXTENSION
  20. ? 'schema'
  21. : '"' + definition.name.value + '"';
  22. context.reportError(
  23. new GraphQLError(`The ${defName} definition is not executable.`, {
  24. nodes: definition,
  25. }),
  26. );
  27. }
  28. }
  29. return false;
  30. },
  31. };
  32. }