ExecutableDefinitions.mjs 1.0 KB

12345678910111213141516171819202122232425262728
  1. import { GraphQLError } from '../../error/GraphQLError';
  2. import { Kind } from '../../language/kinds';
  3. import { isExecutableDefinitionNode } from '../../language/predicates';
  4. export function nonExecutableDefinitionMessage(defName) {
  5. return "The ".concat(defName, " definition is not executable.");
  6. }
  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 ExecutableDefinitions(context) {
  14. return {
  15. Document: function Document(node) {
  16. for (var _i2 = 0, _node$definitions2 = node.definitions; _i2 < _node$definitions2.length; _i2++) {
  17. var definition = _node$definitions2[_i2];
  18. if (!isExecutableDefinitionNode(definition)) {
  19. context.reportError(new GraphQLError(nonExecutableDefinitionMessage(definition.kind === Kind.SCHEMA_DEFINITION || definition.kind === Kind.SCHEMA_EXTENSION ? 'schema' : definition.name.value), definition));
  20. }
  21. }
  22. return false;
  23. }
  24. };
  25. }