VariablesInAllowedPositionRule.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.VariablesInAllowedPositionRule = VariablesInAllowedPositionRule;
  6. var _inspect = require('../../jsutils/inspect.js');
  7. var _GraphQLError = require('../../error/GraphQLError.js');
  8. var _kinds = require('../../language/kinds.js');
  9. var _definition = require('../../type/definition.js');
  10. var _typeComparators = require('../../utilities/typeComparators.js');
  11. var _typeFromAST = require('../../utilities/typeFromAST.js');
  12. /**
  13. * Variables in allowed position
  14. *
  15. * Variable usages must be compatible with the arguments they are passed to.
  16. *
  17. * See https://spec.graphql.org/draft/#sec-All-Variable-Usages-are-Allowed
  18. */
  19. function VariablesInAllowedPositionRule(context) {
  20. let varDefMap = Object.create(null);
  21. return {
  22. OperationDefinition: {
  23. enter() {
  24. varDefMap = Object.create(null);
  25. },
  26. leave(operation) {
  27. const usages = context.getRecursiveVariableUsages(operation);
  28. for (const { node, type, defaultValue } of usages) {
  29. const varName = node.name.value;
  30. const varDef = varDefMap[varName];
  31. if (varDef && type) {
  32. // A var type is allowed if it is the same or more strict (e.g. is
  33. // a subtype of) than the expected type. It can be more strict if
  34. // the variable type is non-null when the expected type is nullable.
  35. // If both are list types, the variable item type can be more strict
  36. // than the expected item type (contravariant).
  37. const schema = context.getSchema();
  38. const varType = (0, _typeFromAST.typeFromAST)(schema, varDef.type);
  39. if (
  40. varType &&
  41. !allowedVariableUsage(
  42. schema,
  43. varType,
  44. varDef.defaultValue,
  45. type,
  46. defaultValue,
  47. )
  48. ) {
  49. const varTypeStr = (0, _inspect.inspect)(varType);
  50. const typeStr = (0, _inspect.inspect)(type);
  51. context.reportError(
  52. new _GraphQLError.GraphQLError(
  53. `Variable "$${varName}" of type "${varTypeStr}" used in position expecting type "${typeStr}".`,
  54. {
  55. nodes: [varDef, node],
  56. },
  57. ),
  58. );
  59. }
  60. }
  61. }
  62. },
  63. },
  64. VariableDefinition(node) {
  65. varDefMap[node.variable.name.value] = node;
  66. },
  67. };
  68. }
  69. /**
  70. * Returns true if the variable is allowed in the location it was found,
  71. * which includes considering if default values exist for either the variable
  72. * or the location at which it is located.
  73. */
  74. function allowedVariableUsage(
  75. schema,
  76. varType,
  77. varDefaultValue,
  78. locationType,
  79. locationDefaultValue,
  80. ) {
  81. if (
  82. (0, _definition.isNonNullType)(locationType) &&
  83. !(0, _definition.isNonNullType)(varType)
  84. ) {
  85. const hasNonNullVariableDefaultValue =
  86. varDefaultValue != null && varDefaultValue.kind !== _kinds.Kind.NULL;
  87. const hasLocationDefaultValue = locationDefaultValue !== undefined;
  88. if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) {
  89. return false;
  90. }
  91. const nullableLocationType = locationType.ofType;
  92. return (0, _typeComparators.isTypeSubTypeOf)(
  93. schema,
  94. varType,
  95. nullableLocationType,
  96. );
  97. }
  98. return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, locationType);
  99. }