import { DefinitionNode , DocumentNode , FragmentDefinitionNode , parse } from "graphql/language" ; import { JsonLdContextNormalized , JsonLdContext } from "jsonld-context-parser" ; import { Algebra } from "sparqlalgebrajs" ; import { NodeHandlerDefinitionFragment , NodeHandlerDefinitionOperation , NodeHandlerDocument , NodeHandlerSelectionField , NodeHandlerSelectionFragmentSpread , NodeHandlerSelectionInlineFragment , NodeValueHandlerBoolean , NodeValueHandlerEnum , NodeValueHandlerFloat , NodeValueHandlerInt , NodeValueHandlerList , NodeValueHandlerNull , NodeValueHandlerObject , NodeValueHandlerString , NodeValueHandlerVariable , } from "./handler" ; import { DirectiveNodeHandlerInclude , DirectiveNodeHandlerOptional , DirectiveNodeHandlerPlural , DirectiveNodeHandlerSingle , DirectiveNodeHandlerSkip , } from "./handler/directivenode" ; import { IConvertContext , SingularizeState } from "./IConvertContext" ; import { IConvertOptions } from "./IConvertOptions" ; import { IConvertSettings } from "./IConvertSettings" ; import { Util } from "./Util" ; /**
 * Translate GraphQL queries into SPARQL algebra.
 */ export class Converter { private readonly util : Util ; constructor ( settings ? ) { settings = settings || { } ; settings . variableDelimiter = settings . variableDelimiter || '_' ; settings . expressionVariableCounter = settings . expressionVariableCounter || 0 ; this . util = new Util ( settings ) ; this . initializeNodeHandlers ( settings ) ; } public static registerNodeHandlers ( util , settings ) { util . registerNodeHandler ( new NodeHandlerDocument ( util , settings ) ) ; util . registerNodeHandler ( new NodeHandlerDefinitionOperation ( util , settings ) ) ; util . registerNodeHandler ( new NodeHandlerDefinitionFragment ( util , settings ) ) ; util . registerNodeHandler ( new NodeHandlerSelectionFragmentSpread ( util , settings ) ) ; util . registerNodeHandler ( new NodeHandlerSelectionInlineFragment ( util , settings ) ) ; util . registerNodeHandler ( new NodeHandlerSelectionField ( util , settings ) ) ; } public static registerNodeValueHandlers ( util , settings ) { util . registerNodeValueHandler ( new NodeValueHandlerVariable ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerInt ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerFloat ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerString ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerBoolean ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerNull ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerEnum ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerList ( util , settings ) ) ; util . registerNodeValueHandler ( new NodeValueHandlerObject ( util , settings ) ) ; } public static registerDirectiveNodeHandlers ( util , settings ) { util . registerDirectiveNodeHandler ( new DirectiveNodeHandlerInclude ( util , settings ) ) ; util . registerDirectiveNodeHandler ( new DirectiveNodeHandlerOptional ( util , settings ) ) ; util . registerDirectiveNodeHandler ( new DirectiveNodeHandlerPlural ( util , settings ) ) ; util . registerDirectiveNodeHandler ( new DirectiveNodeHandlerSingle ( util , settings ) ) ; util . registerDirectiveNodeHandler ( new DirectiveNodeHandlerSkip ( util , settings ) ) ; } param { string | DocumentNode } graphqlQuery param { IContext } context param { IConvertOptions } options return { Promise < Operation > } public async graphqlToSparqlAlgebra ( graphqlQuery , context , options ? ) { return this . graphqlToSparqlAlgebraRawContext ( graphqlQuery , await this . util . contextParser . parse ( context ) , options ) ; } param { string | DocumentNode } graphqlQuery param { IContext } context param { IConvertOptions } options return { Operation } public graphqlToSparqlAlgebraRawContext ( graphqlQuery , context , options ? ) { options = options || { } ; const document = typeof graphqlQuery === 'string' ? parse ( graphqlQuery ) : graphqlQuery ; const fragmentDefinitions = this . indexFragments ( document ) ; const convertContext = { context , fragmentDefinitions , graph : this . util . dataFactory . defaultGraph ( ) , path : [ ] , singularizeState : SingularizeState . PLURAL , singularizeVariables : options . singularizeVariables || { } , subject : null ! , terminalVariables : [ ] , variablesDict : options . variablesDict || { } , variablesMetaDict : { } , } ; return this . util . handleNode ( document , convertContext ) ; } param { DocumentNode } document return { { [ p : string ] : FragmentDefinitionNode } } public indexFragments ( document ) { const fragmentDefinitions = { } ; const newDefinitions = [ ] ; for ( const definition of document . definitions ) { if ( definition . kind === 'FragmentDefinition' ) { fragmentDefinitions [ definition . name . value ] = definition ; } else { newDefinitions . push ( definition ) ; } } ( < any > document ) . definitions = newDefinitions ; return fragmentDefinitions ; } private initializeNodeHandlers ( settings ) { Converter . registerNodeHandlers ( this . util , settings ) ; Converter . registerNodeValueHandlers ( this . util , settings ) ; Converter . registerDirectiveNodeHandlers ( this . util , settings ) ; } }