import { ListValueNode , ValueNode } from "graphql/language" ; import { VariableNode } from "graphql/language/ast" ; import { IConvertContext } from "../../IConvertContext" ; import { IConvertSettings } from "../../IConvertSettings" ; import { Util } from "../../Util" ; import { IValueNodeHandlerOutput , NodeValueHandlerAdapter } from "./NodeValueHandlerAdapter" ; /**
 * Converts GraphQL variables to terms based on the contents of the variablesDict.
 */ export class NodeValueHandlerVariable extends NodeValueHandlerAdapter < VariableNode > { constructor ( util , settings ) { super ( 'Variable' , util , settings ) ; } public handle ( valueNode , fieldName , convertContext ) { const id = valueNode . name . value ; const value = convertContext . variablesDict [ id ] ; const meta = convertContext . variablesMetaDict [ id ] ; if ( ! value ) { if ( ! convertContext . ignoreUnknownVariables && ( ! meta || meta . mandatory ) ) { throw new Error ( `Undefined variable: ${ id }` ) ; } else { const variable = this . util . dataFactory . variable ! ( id ) ; if ( convertContext . terminalVariables . map ( ( v ) => v . value ) . indexOf ( id ) < 0 ) { convertContext . terminalVariables . push ( variable ) ; } return { terms : [ variable ] } ; } } if ( value . kind === 'Variable' ) { throw new Error ( `Variable refers to another variable: ${ id }` ) ; } if ( meta ) { if ( meta . list ) { if ( value . kind !== 'ListValue' ) { throw new Error ( `Expected a list, but got ${ value . kind } for ${ id }` ) ; } if ( meta . type ) { const listValue = < ListValueNode > value ; for ( const v of listValue . values ) { if ( v . kind !== meta . type ) { throw new Error ( `Expected ${ meta . type }, but got ${ v . kind } for ${ id }` ) ; } } } } else if ( meta . type ) { } } return this . util . handleNodeValue ( value , fieldName , convertContext ) ; } }