values.mjs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import find from '../polyfills/find';
  2. import keyMap from '../jsutils/keyMap';
  3. import inspect from '../jsutils/inspect';
  4. import printPathArray from '../jsutils/printPathArray';
  5. import { GraphQLError } from '../error/GraphQLError';
  6. import { Kind } from '../language/kinds';
  7. import { print } from '../language/printer';
  8. import { isInputType, isNonNullType } from '../type/definition';
  9. import { typeFromAST } from '../utilities/typeFromAST';
  10. import { valueFromAST } from '../utilities/valueFromAST';
  11. import { coerceInputValue } from '../utilities/coerceInputValue';
  12. /**
  13. * Prepares an object map of variableValues of the correct type based on the
  14. * provided variable definitions and arbitrary input. If the input cannot be
  15. * parsed to match the variable definitions, a GraphQLError will be thrown.
  16. *
  17. * Note: The returned value is a plain Object with a prototype, since it is
  18. * exposed to user code. Care should be taken to not pull values from the
  19. * Object prototype.
  20. */
  21. export function getVariableValues(schema, varDefNodes, inputs, options) {
  22. var maxErrors = options && options.maxErrors;
  23. var errors = [];
  24. try {
  25. var coerced = coerceVariableValues(schema, varDefNodes, inputs, function (error) {
  26. if (maxErrors != null && errors.length >= maxErrors) {
  27. throw new GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
  28. }
  29. errors.push(error);
  30. });
  31. if (errors.length === 0) {
  32. return {
  33. coerced: coerced
  34. };
  35. }
  36. } catch (error) {
  37. errors.push(error);
  38. }
  39. return {
  40. errors: errors
  41. };
  42. }
  43. function coerceVariableValues(schema, varDefNodes, inputs, onError) {
  44. var coercedValues = {};
  45. var _loop = function _loop(_i2) {
  46. var varDefNode = varDefNodes[_i2];
  47. var varName = varDefNode.variable.name.value;
  48. var varType = typeFromAST(schema, varDefNode.type);
  49. if (!isInputType(varType)) {
  50. // Must use input types for variables. This should be caught during
  51. // validation, however is checked again here for safety.
  52. var varTypeStr = print(varDefNode.type);
  53. onError(new GraphQLError("Variable \"$".concat(varName, "\" expected value of type \"").concat(varTypeStr, "\" which cannot be used as an input type."), varDefNode.type));
  54. return "continue";
  55. }
  56. if (!hasOwnProperty(inputs, varName)) {
  57. if (varDefNode.defaultValue) {
  58. coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
  59. } else if (isNonNullType(varType)) {
  60. var _varTypeStr = inspect(varType);
  61. onError(new GraphQLError("Variable \"$".concat(varName, "\" of required type \"").concat(_varTypeStr, "\" was not provided."), varDefNode));
  62. }
  63. return "continue";
  64. }
  65. var value = inputs[varName];
  66. if (value === null && isNonNullType(varType)) {
  67. var _varTypeStr2 = inspect(varType);
  68. onError(new GraphQLError("Variable \"$".concat(varName, "\" of non-null type \"").concat(_varTypeStr2, "\" must not be null."), varDefNode));
  69. return "continue";
  70. }
  71. coercedValues[varName] = coerceInputValue(value, varType, function (path, invalidValue, error) {
  72. var prefix = "Variable \"$".concat(varName, "\" got invalid value ") + inspect(invalidValue);
  73. if (path.length > 0) {
  74. prefix += " at \"".concat(varName).concat(printPathArray(path), "\"");
  75. }
  76. onError(new GraphQLError(prefix + '; ' + error.message, varDefNode, undefined, undefined, undefined, error.originalError));
  77. });
  78. };
  79. for (var _i2 = 0; _i2 < varDefNodes.length; _i2++) {
  80. var _ret = _loop(_i2);
  81. if (_ret === "continue") continue;
  82. }
  83. return coercedValues;
  84. }
  85. /**
  86. * Prepares an object map of argument values given a list of argument
  87. * definitions and list of argument AST nodes.
  88. *
  89. * Note: The returned value is a plain Object with a prototype, since it is
  90. * exposed to user code. Care should be taken to not pull values from the
  91. * Object prototype.
  92. */
  93. export function getArgumentValues(def, node, variableValues) {
  94. var coercedValues = {};
  95. var argNodeMap = keyMap(node.arguments || [], function (arg) {
  96. return arg.name.value;
  97. });
  98. for (var _i4 = 0, _def$args2 = def.args; _i4 < _def$args2.length; _i4++) {
  99. var argDef = _def$args2[_i4];
  100. var name = argDef.name;
  101. var argType = argDef.type;
  102. var argumentNode = argNodeMap[name];
  103. if (!argumentNode) {
  104. if (argDef.defaultValue !== undefined) {
  105. coercedValues[name] = argDef.defaultValue;
  106. } else if (isNonNullType(argType)) {
  107. throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + 'was not provided.', node);
  108. }
  109. continue;
  110. }
  111. var valueNode = argumentNode.value;
  112. var isNull = valueNode.kind === Kind.NULL;
  113. if (valueNode.kind === Kind.VARIABLE) {
  114. var variableName = valueNode.name.value;
  115. if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
  116. if (argDef.defaultValue !== undefined) {
  117. coercedValues[name] = argDef.defaultValue;
  118. } else if (isNonNullType(argType)) {
  119. throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + "was provided the variable \"$".concat(variableName, "\" which was not provided a runtime value."), valueNode);
  120. }
  121. continue;
  122. }
  123. isNull = variableValues[variableName] == null;
  124. }
  125. if (isNull && isNonNullType(argType)) {
  126. throw new GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat(inspect(argType), "\" ") + 'must not be null.', valueNode);
  127. }
  128. var coercedValue = valueFromAST(valueNode, argType, variableValues);
  129. if (coercedValue === undefined) {
  130. // Note: ValuesOfCorrectType validation should catch this before
  131. // execution. This is a runtime check to ensure execution does not
  132. // continue with an invalid argument value.
  133. throw new GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat(print(valueNode), "."), valueNode);
  134. }
  135. coercedValues[name] = coercedValue;
  136. }
  137. return coercedValues;
  138. }
  139. /**
  140. * Prepares an object map of argument values given a directive definition
  141. * and a AST node which may contain directives. Optionally also accepts a map
  142. * of variable values.
  143. *
  144. * If the directive does not exist on the node, returns undefined.
  145. *
  146. * Note: The returned value is a plain Object with a prototype, since it is
  147. * exposed to user code. Care should be taken to not pull values from the
  148. * Object prototype.
  149. */
  150. export function getDirectiveValues(directiveDef, node, variableValues) {
  151. var directiveNode = node.directives && find(node.directives, function (directive) {
  152. return directive.name.value === directiveDef.name;
  153. });
  154. if (directiveNode) {
  155. return getArgumentValues(directiveDef, directiveNode, variableValues);
  156. }
  157. }
  158. function hasOwnProperty(obj, prop) {
  159. return Object.prototype.hasOwnProperty.call(obj, prop);
  160. }