import { ListTypeNode , NamedTypeNode , NonNullTypeNode , OperationDefinitionNode , TypeNode } from "graphql/language" ; import { Algebra } from "sparqlalgebrajs" ; import { IConvertContext } from "../IConvertContext" ; import { IConvertSettings } from "../IConvertSettings" ; import { Util } from "../Util" ; import { NodeHandlerDefinitionAdapter } from "./NodeHandlerDefinitionAdapter" ; /**
 * Converts GraphQL definitions to joined operations for all its selections.
 */ export class NodeHandlerDefinitionOperation extends NodeHandlerDefinitionAdapter < OperationDefinitionNode > { constructor ( util , settings ) { super ( 'OperationDefinition' , util , settings ) ; } public handle ( operationDefinition , convertContext ) { if ( operationDefinition . operation !== 'query' ) { throw new Error ( 'Unsupported definition operation: ' + operationDefinition . operation ) ; } if ( operationDefinition . variableDefinitions ) { for ( const variableDefinition of operationDefinition . variableDefinitions ) { const name = variableDefinition . variable . name . value ; if ( variableDefinition . defaultValue ) { if ( ! convertContext . variablesDict [ name ] ) { convertContext . variablesDict [ name ] = variableDefinition . defaultValue ; } } let typeNode = variableDefinition . type ; const mandatory = typeNode . kind === 'NonNullType' ; if ( mandatory ) { typeNode = ( < NonNullTypeNode > typeNode ) . type ; } const list = typeNode . kind === 'ListType' ; if ( list ) { typeNode = ( < ListTypeNode > typeNode ) . type ; } const type = ( < NamedTypeNode > typeNode ) . name . value ; convertContext . variablesMetaDict [ name ] = { mandatory , list , type } ; } } const directiveOutputs = this . getDirectiveOutputs ( operationDefinition . directives , operationDefinition . name ? operationDefinition . name . value : '' , convertContext ) ; if ( ! directiveOutputs ) { return this . util . operationFactory . createBgp ( [ ] ) ; } const operation = this . util . joinOperations ( operationDefinition . selectionSet . selections . map ( ( selectionNode ) => this . util . handleNode ( selectionNode , convertContext ) ) ) ; return this . handleDirectiveOutputs ( directiveOutputs , operation ) ; } }