import { ValueNode , ObjectValueNode , ListValueNode , TypeNode , GraphQLSchema , typeFromAST , GraphQLType } from 'graphql' ; import { LookupASTType } from './type-lookup' ; interface StringedValue { value : string ; } export function astValueToJs ( node ) { let sv = node as any ; switch ( node . kind ) { case 'FloatValue' : case 'IntValue' : return + sv . value ; case 'EnumValue' : case 'StringValue' : case 'BooleanValue' : return sv . value ; case 'NullValue' : return null ; case 'ListValue' : let lv = node ; let resa = [ ] ; for ( let subv of lv . values ) { resa . push ( astValueToJs ( subv ) ) ; } return resa ; case 'ObjectValue' : let ov = node ; let reso = { } ; for ( let field of ov . fields ) { reso [ field . name . value ] = astValueToJs ( field . value ) ; } return reso ; default : break ; } return undefined ; } export function validAstKinds ( value ? ) { let isString = typeof value === 'string' ; return { EnumValue : isString && value === value . toUpperCase ( ) , StringValue : isString , BooleanValue : typeof value === 'boolean' , IntValue : typeof value === 'number' && value % 1 === 0 , FloatValue : typeof value === 'number' , ListValue : ! ! value && typeof value === 'object' && value . constructor === Array , ObjectValue : ! ! value && typeof value === 'object' && value . constructor !== Array , NullValue : typeof value === 'object' && ! value } ; } export function isAstKind ( kind , value ? ) { let vac = validAstKinds ( value ) ; return vac [ kind ] || false ; } export function unwrapAstType ( typ ) { switch ( typ . kind ) { case 'ListType' : return unwrapAstType ( typ . type ) ; case 'NonNullType' : return unwrapAstType ( typ . type ) ; default : return typ ; } } export function isPrimitiveKind ( kind ) { switch ( kind ) { case 'String' : case 'Int' : case 'Float' : case 'Boolean' : case 'ID' : return true ; default : return false ; } } export function isAstPrimitive ( typ ) { if ( ! typ ) { return false ; } if ( typ . kind === 'NamedType' ) { if ( typ . name && typ . name . value ) { return isPrimitiveKind ( typ . name . value ) ; } } return false ; } export function getLookupType ( schema ) { return ( astType ) : GraphQLType | null => { if ( astType . kind !== 'NamedType' ) { return null ; } return typeFromAST ( schema , astType ) || null ; } ; }