Mirror: The spec-compliant minimum of client-side GraphQL.
1import { TypeNode, ValueNode } from './ast'; 2import { Kind } from './kind'; 3import { Maybe } from './types'; 4 5export function valueFromASTUntyped( 6 node: ValueNode, 7 variables?: Maybe<Record<string, any>>, 8): unknown { 9 switch (node.kind) { 10 case Kind.NULL: 11 return null; 12 case Kind.INT: 13 return parseInt(node.value, 10); 14 case Kind.FLOAT: 15 return parseFloat(node.value); 16 case Kind.STRING: 17 case Kind.ENUM: 18 case Kind.BOOLEAN: 19 return node.value; 20 case Kind.LIST: { 21 const values: unknown[] = []; 22 for (const value of node.values) 23 values.push(valueFromASTUntyped(value, variables)); 24 return values; 25 } 26 case Kind.OBJECT: { 27 const obj = Object.create(null); 28 for (const field of node.fields) 29 obj[field.name.value] = valueFromASTUntyped(field.value, variables); 30 return obj; 31 } 32 case Kind.VARIABLE: 33 return variables && variables[node.name.value]; 34 } 35} 36 37export function valueFromTypeNode( 38 node: ValueNode, 39 type: TypeNode, 40 variables?: Maybe<Record<string, any>>, 41): unknown { 42 if (node.kind === Kind.VARIABLE) { 43 const variableName = node.name.value; 44 return variables 45 ? valueFromTypeNode(variables[variableName], type, variables) 46 : undefined; 47 } else if (type.kind === Kind.NON_NULL_TYPE) { 48 return node.kind !== Kind.NULL 49 ? valueFromTypeNode(node, type, variables) 50 : undefined 51 } else if (node.kind === Kind.NULL) { 52 return null; 53 } else if (type.kind === Kind.LIST_TYPE) { 54 if (node.kind === Kind.LIST) { 55 const values: unknown[] = []; 56 for (const value of node.values) { 57 const coerced = valueFromTypeNode(value, type.type, variables); 58 if (coerced === undefined) { 59 return undefined; 60 } else { 61 values.push(coerced); 62 } 63 } 64 return values; 65 } 66 } else if (type.kind === Kind.NAMED_TYPE) { 67 switch (type.name.value) { 68 case 'Int': 69 case 'Float': 70 case 'String': 71 case 'Bool': 72 return type.name.value + 'Value' === node.kind 73 ? valueFromASTUntyped(node, variables) 74 : undefined; 75 default: 76 return valueFromASTUntyped(node, variables); 77 } 78 } 79}