ScalarLeafs.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import inspect from '../../jsutils/inspect';
  2. import { GraphQLError } from '../../error/GraphQLError';
  3. import { getNamedType, isLeafType } from '../../type/definition';
  4. export function noSubselectionAllowedMessage(fieldName, type) {
  5. return "Field \"".concat(fieldName, "\" must not have a selection since type \"").concat(type, "\" has no subfields.");
  6. }
  7. export function requiredSubselectionMessage(fieldName, type) {
  8. return "Field \"".concat(fieldName, "\" of type \"").concat(type, "\" must have a selection of subfields. Did you mean \"").concat(fieldName, " { ... }\"?");
  9. }
  10. /**
  11. * Scalar leafs
  12. *
  13. * A GraphQL document is valid only if all leaf fields (fields without
  14. * sub selections) are of scalar or enum types.
  15. */
  16. export function ScalarLeafs(context) {
  17. return {
  18. Field: function Field(node) {
  19. var type = context.getType();
  20. var selectionSet = node.selectionSet;
  21. if (type) {
  22. if (isLeafType(getNamedType(type))) {
  23. if (selectionSet) {
  24. context.reportError(new GraphQLError(noSubselectionAllowedMessage(node.name.value, inspect(type)), selectionSet));
  25. }
  26. } else if (!selectionSet) {
  27. context.reportError(new GraphQLError(requiredSubselectionMessage(node.name.value, inspect(type)), node));
  28. }
  29. }
  30. }
  31. };
  32. }