ScalarLeafsRule.mjs 1.2 KB

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