import { DirectiveNode , FieldNode , SelectionSetNode } from "graphql/language" ; import * as RDF from "@rdfjs/types" ; import { Algebra } from "sparqlalgebrajs" ; import { IConvertContext } from "../IConvertContext" ; import { IConvertSettings } from "../IConvertSettings" ; import { Util } from "../Util" ; import { IDirectiveNodeHandlerOutput } from "./directivenode/DirectiveNodeHandlerAdapter" ; /**
 * A handler for converting GraphQL nodes to operations.
 */ export abstract class NodeHandlerAdapter < T extends { kind : string ; } > { public readonly targetKind : string ; protected readonly util : Util ; protected readonly settings : IConvertSettings ; constructor ( targetKind , util , settings ) { this . targetKind = targetKind ; this . util = util ; this . settings = settings ; } param { T } node param { IConvertContext } convertContext return { Operation } public abstract handle ( node , convertContext ) ; param { SelectionSetNode } selectionSet param { string } fieldLabel param { IConvertContext } convertContext return { INodeQuadContext } public getNodeQuadContextSelectionSet ( selectionSet , fieldLabel , convertContext ) { const nodeQuadContext = { } ; if ( selectionSet ) { for ( const selectionNode of selectionSet . selections ) { if ( selectionNode . kind === 'Field' ) { const fieldNode = selectionNode ; this . handleNodeQuadContextField ( fieldNode , convertContext , nodeQuadContext , 'id' , 'subject' ) ; this . handleNodeQuadContextField ( fieldNode , convertContext , nodeQuadContext , 'graph' , 'graph' ) ; } } } return nodeQuadContext ; } param { FieldNode } fieldNode param { IConvertContext } convertContext param { INodeQuadContext } nodeQuadContext param { string } fieldName param { 'subject' | 'graph' } nodeQuadContextKey public handleNodeQuadContextField ( fieldNode , convertContext , nodeQuadContext , fieldName , nodeQuadContextKey ) { if ( ! nodeQuadContext [ nodeQuadContextKey ] && fieldNode . name . value === fieldName ) { if ( ! nodeQuadContext [ nodeQuadContextKey ] ) { const argument = this . util . getArgument ( fieldNode . arguments , '_' ) ; if ( argument ) { const valueOutput = this . util . handleNodeValue ( argument . value , fieldNode . name . value , convertContext ) ; if ( valueOutput . terms . length !== 1 ) { throw new Error ( `Only single values can be set as ${ fieldName }, but got ${ valueOutput . terms . length } at ${ fieldNode . name . value }` ) ; } nodeQuadContext [ nodeQuadContextKey ] = valueOutput . terms [ 0 ] ; if ( valueOutput . auxiliaryPatterns ) { if ( ! nodeQuadContext . auxiliaryPatterns ) { nodeQuadContext . auxiliaryPatterns = [ ] ; } nodeQuadContext . auxiliaryPatterns . concat ( valueOutput . auxiliaryPatterns ) ; } } } if ( ! nodeQuadContext [ nodeQuadContextKey ] ) { const term = this . util . nameToVariable ( this . util . getFieldLabel ( fieldNode ) , convertContext ) ; convertContext . terminalVariables . push ( term ) ; nodeQuadContext [ nodeQuadContextKey ] = term ; } } } param { ReadonlyArray < DirectiveNode > } directives param { string } fieldLabel param { IConvertContext } convertContext return { IDirectiveNodeHandlerOutput [ ] } public getDirectiveOutputs ( directives , fieldLabel , convertContext ) { const outputs = [ ] ; if ( directives ) { for ( const directive of directives ) { const output = this . util . handleDirectiveNode ( { directive , fieldLabel } , convertContext ) ; if ( output ) { if ( output . ignore ) { return null ; } outputs . push ( output ) ; } } } return outputs ; } param { IDirectiveNodeHandlerOutput [ ] } directiveOutputs param { Operation } operation return { Operation } public handleDirectiveOutputs ( directiveOutputs , operation ) { for ( const directiveOutput of directiveOutputs ) { if ( directiveOutput . ignore ) { return this . util . operationFactory . createBgp ( [ ] ) ; } if ( directiveOutput . operationOverrider ) { operation = directiveOutput . operationOverrider ( operation ) ; } } return operation ; } } /**
 * The output of getting a node's quad context.
 */ export interface INodeQuadContext { subject ? : RDF . Term ; graph ? : RDF . Term ; auxiliaryPatterns ? : Algebra . Pattern [ ] ; }