ScalarLeafsRule.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.ScalarLeafsRule = ScalarLeafsRule;
  6. var _inspect = require('../../jsutils/inspect.js');
  7. var _GraphQLError = require('../../error/GraphQLError.js');
  8. var _definition = require('../../type/definition.js');
  9. /**
  10. * Scalar leafs
  11. *
  12. * A GraphQL document is valid only if all leaf fields (fields without
  13. * sub selections) are of scalar or enum types.
  14. */
  15. function ScalarLeafsRule(context) {
  16. return {
  17. Field(node) {
  18. const type = context.getType();
  19. const selectionSet = node.selectionSet;
  20. if (type) {
  21. if ((0, _definition.isLeafType)((0, _definition.getNamedType)(type))) {
  22. if (selectionSet) {
  23. const fieldName = node.name.value;
  24. const typeStr = (0, _inspect.inspect)(type);
  25. context.reportError(
  26. new _GraphQLError.GraphQLError(
  27. `Field "${fieldName}" must not have a selection since type "${typeStr}" has no subfields.`,
  28. {
  29. nodes: selectionSet,
  30. },
  31. ),
  32. );
  33. }
  34. } else if (!selectionSet) {
  35. const fieldName = node.name.value;
  36. const typeStr = (0, _inspect.inspect)(type);
  37. context.reportError(
  38. new _GraphQLError.GraphQLError(
  39. `Field "${fieldName}" of type "${typeStr}" must have a selection of subfields. Did you mean "${fieldName} { ... }"?`,
  40. {
  41. nodes: node,
  42. },
  43. ),
  44. );
  45. }
  46. }
  47. },
  48. };
  49. }