import { visit , Kind , isEqualType , isAbstractType , SchemaMetaFieldDef , TypeMetaFieldDef , TypeNameMetaFieldDef , GraphQLCompositeType , GraphQLObjectType , GraphQLInterfaceType , GraphQLUnionType , GraphQLEnumValue , GraphQLError , GraphQLSchema , GraphQLType , ASTNode , Location , ValueNode , OperationDefinitionNode , SelectionSetNode , FieldNode , GraphQLField , DocumentNode , DirectiveNode , isListType , isNonNullType , } from 'graphql' ; declare module 'graphql/utilities/buildASTSchema' { function buildASTSchema ( ast , options ? ) ; } export function sortEnumValues ( values ) { return values . sort ( ( a , b ) => ( a . value < b . value ? - 1 : a . value > b . value ? 1 : 0 ) ) ; } export function isList ( type ) { return isListType ( type ) || ( isNonNullType ( type ) && isListType ( type . ofType ) ) ; } export function isMetaFieldName ( name ) { return name . startsWith ( '__' ) ; } export function removeConnectionDirectives ( ast ) { return visit ( ast , { Directive ( node ) { switch ( node . name . value ) { case 'connection' : return null ; case 'hasOne' : return null ; case 'belongsTo' : return null ; case 'hasMany' : return null ; case 'manyToMany' : return null ; default : return node ; } } , } ) ; } export function removeClientDirectives ( ast ) { return visit ( ast , { Field ( node ) { if ( node . directives && node . directives . find ( directive => directive . name . value === 'client' ) ) return null ; return node ; } , OperationDefinition : { leave ( node ) { if ( ! node . selectionSet . selections . length ) return null ; return node ; } , } , } ) ; } const typenameField = { kind : Kind . FIELD , name : { kind : Kind . NAME , value : '__typename' } , } ; export function withTypenameFieldAddedWhereNeeded ( ast ) { return visit ( ast , { enter : { SelectionSet ( node ) { return { ... node , selections : node . selections . filter ( selection => ! ( selection . kind === 'Field' && ( selection as FieldNode ) . name . value === '__typename' ) ) , } ; } , } , leave ( node ) { if ( ! ( node . kind === 'Field' || node . kind === 'FragmentDefinition' ) ) return undefined ; if ( ! node . selectionSet ) return undefined ; if ( true ) { return { ... node , selectionSet : { ... node . selectionSet , selections : [ typenameField , ... node . selectionSet . selections ] , } , } ; } else { return undefined ; } } , } ) ; } export function sourceAt ( location ) { return location . source . body . slice ( location . start , location . end ) ; } export function filePathForNode ( node ) { const name = node . loc && node . loc . source && node . loc . source . name ; if ( ! name || name === 'GraphQL' ) { throw new Error ( 'Node does not seem to have a file path' ) ; } return name ; } export function valueFromValueNode ( valueNode ) { switch ( valueNode . kind ) { case 'IntValue' : case 'FloatValue' : return Number ( valueNode . value ) ; case 'NullValue' : return null ; case 'ListValue' : return valueNode . values . map ( valueFromValueNode ) ; case 'ObjectValue' : return valueNode . fields . reduce ( ( object , field ) => { object [ field . name . value ] = valueFromValueNode ( field . value ) ; return object ; } , { } as any ) ; case 'Variable' : return { kind : 'Variable' , variableName : valueNode . name . value } ; default : return valueNode . value ; } } export function isTypeProperSuperTypeOf ( schema , maybeSuperType , subType ) { return ( isEqualType ( maybeSuperType , subType ) || ( subType instanceof GraphQLObjectType && ( isAbstractType ( maybeSuperType ) && schema . isPossibleType ( maybeSuperType , subType ) ) ) ) ; } /**
 * Extracts the root type of the operation from the schema.
 */ export function getOperationRootType ( schema , operation ) { switch ( operation . operation ) { case 'query' : return schema . getQueryType ( ) ; case 'mutation' : const mutationType = schema . getMutationType ( ) ; if ( ! mutationType ) { throw new GraphQLError ( 'Schema is not configured for mutations' , [ operation ] ) ; } return mutationType ; case 'subscription' : const subscriptionType = schema . getSubscriptionType ( ) ; if ( ! subscriptionType ) { throw new GraphQLError ( 'Schema is not configured for subscriptions' , [ operation ] ) ; } return subscriptionType ; default : throw new GraphQLError ( 'Can only compile queries, mutations and subscriptions' , [ operation ] ) ; } } /**
 * Not exactly the same as the executor's definition of getFieldDef, in this
 * statically evaluated environment we do not always have an Object type,
 * and need to handle Interface and Union types.
 */ export function getFieldDef ( schema , parentType , fieldAST ) { const name = fieldAST . name . value ; if ( name === SchemaMetaFieldDef . name && schema . getQueryType ( ) === parentType ) { return SchemaMetaFieldDef ; } if ( name === TypeMetaFieldDef . name && schema . getQueryType ( ) === parentType ) { return TypeMetaFieldDef ; } if ( name === TypeNameMetaFieldDef . name && ( parentType instanceof GraphQLObjectType || parentType instanceof GraphQLInterfaceType || parentType instanceof GraphQLUnionType ) ) { return TypeNameMetaFieldDef ; } if ( parentType instanceof GraphQLObjectType || parentType instanceof GraphQLInterfaceType ) { return parentType . getFields ( ) [ name ] ; } return undefined ; }