123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import objectValues from '../polyfills/objectValues';
- import type { ObjMap } from '../jsutils/ObjMap';
- import keyMap from '../jsutils/keyMap';
- import inspect from '../jsutils/inspect';
- import invariant from '../jsutils/invariant';
- import type { ValueNode } from '../language/ast';
- import { Kind } from '../language/kinds';
- import type { GraphQLInputType } from '../type/definition';
- import {
- isLeafType,
- isInputObjectType,
- isListType,
- isNonNullType,
- } from '../type/definition';
- export function valueFromAST(
- valueNode: ?ValueNode,
- type: GraphQLInputType,
- variables?: ?ObjMap<mixed>,
- ): mixed | void {
- if (!valueNode) {
-
-
- return;
- }
- if (valueNode.kind === Kind.VARIABLE) {
- const variableName = valueNode.name.value;
- if (variables == null || variables[variableName] === undefined) {
-
- return;
- }
- const variableValue = variables[variableName];
- if (variableValue === null && isNonNullType(type)) {
- return;
- }
-
-
-
- return variableValue;
- }
- if (isNonNullType(type)) {
- if (valueNode.kind === Kind.NULL) {
- return;
- }
- return valueFromAST(valueNode, type.ofType, variables);
- }
- if (valueNode.kind === Kind.NULL) {
-
- return null;
- }
- if (isListType(type)) {
- const itemType = type.ofType;
- if (valueNode.kind === Kind.LIST) {
- const coercedValues = [];
- for (const itemNode of valueNode.values) {
- if (isMissingVariable(itemNode, variables)) {
-
-
- if (isNonNullType(itemType)) {
- return;
- }
- coercedValues.push(null);
- } else {
- const itemValue = valueFromAST(itemNode, itemType, variables);
- if (itemValue === undefined) {
- return;
- }
- coercedValues.push(itemValue);
- }
- }
- return coercedValues;
- }
- const coercedValue = valueFromAST(valueNode, itemType, variables);
- if (coercedValue === undefined) {
- return;
- }
- return [coercedValue];
- }
- if (isInputObjectType(type)) {
- if (valueNode.kind !== Kind.OBJECT) {
- return;
- }
- const coercedObj = Object.create(null);
- const fieldNodes = keyMap(valueNode.fields, (field) => field.name.value);
- for (const field of objectValues(type.getFields())) {
- const fieldNode = fieldNodes[field.name];
- if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
- if (field.defaultValue !== undefined) {
- coercedObj[field.name] = field.defaultValue;
- } else if (isNonNullType(field.type)) {
- return;
- }
- continue;
- }
- const fieldValue = valueFromAST(fieldNode.value, field.type, variables);
- if (fieldValue === undefined) {
- return;
- }
- coercedObj[field.name] = fieldValue;
- }
- return coercedObj;
- }
-
- if (isLeafType(type)) {
-
-
-
- let result;
- try {
- result = type.parseLiteral(valueNode, variables);
- } catch (_error) {
- return;
- }
- if (result === undefined) {
- return;
- }
- return result;
- }
-
- invariant(false, 'Unexpected input type: ' + inspect((type: empty)));
- }
- function isMissingVariable(valueNode, variables) {
- return (
- valueNode.kind === Kind.VARIABLE &&
- (variables == null || variables[valueNode.name.value] === undefined)
- );
- }
|