11package org.neo4j.graphql
22
33import graphql.Scalars
4+ import graphql.introspection.Introspection
45import graphql.language.*
56import graphql.parser.Parser
67import graphql.schema.*
78import graphql.schema.idl.RuntimeWiring
89import graphql.schema.idl.SchemaGenerator
910import graphql.schema.idl.SchemaParser
1011import org.antlr.v4.runtime.misc.ParseCancellationException
12+ import java.util.*
1113
1214class Translator (val schema : GraphQLSchema ) {
1315 data class Context (val topLevelWhere : Boolean = true , val fragments : Map <String ,FragmentDefinition > = emptyMap())
@@ -173,18 +175,22 @@ class Translator(val schema: GraphQLSchema) {
173175 }
174176 }
175177
178+ private fun Directive.argumentString (name : String ) : String {
179+ return this .getArgument(name)?.value?.toJavaValue()?.toString()
180+ ? : schema.getDirective(this .name).getArgument(name)?.defaultValue?.toString()
181+ ? : throw IllegalStateException (" No default value for ${this .name} .${name} " )
182+ }
183+
176184 private fun relDetails (relDirective : Directive ): Triple <String ,Boolean ?,String > {
177- val arguments = relDirective.argumentsByName
178- val relType = arguments.getValue(" name" ).value.toJavaValue().toString()
179- val relDirection = arguments.get(" direction" )?.value?.toJavaValue()
180- ? : schema.getDirective(" relation" ).getArgument(" direction" ).defaultValue
181- val outgoing = when (relDirection.toString()) {
185+ val relType = relDirective.argumentString(" name" )
186+ val outgoing = when (relDirective.argumentString(" direction" )) {
182187 " IN" -> false
183188 " BOTH" -> null
184189 " OUT" -> true
185- else -> throw IllegalStateException (" Unknown direction $relDirection " )
190+ else -> throw IllegalStateException (" Unknown direction ${relDirective.argumentString( " direction " )} " )
186191 }
187- val endField = if (outgoing == true ) " end" else " start"
192+ val endField = if (outgoing == true ) relDirective.argumentString(" end" )
193+ else relDirective.argumentString(" end" )
188194 return Triple (relType, outgoing, endField)
189195 }
190196
@@ -280,7 +286,12 @@ object SchemaBuilder {
280286 .build()
281287
282288 val schemaGenerator = SchemaGenerator ()
283- val directives = setOf (GraphQLDirective .newDirective().name(" relation" ).argument { it.name(" name" ).type(Scalars .GraphQLString ).also { it.name(" direction" ).type(Scalars .GraphQLString ).defaultValue(" OUT" ) } }.build())
289+ val directives = setOf (GraphQLDirective (" relation" , " relation directive" ,
290+ EnumSet .of(Introspection .DirectiveLocation .FIELD ,Introspection .DirectiveLocation .OBJECT ),
291+ listOf (GraphQLArgument (" name" ,Scalars .GraphQLString ),
292+ GraphQLArgument (" direction" ," relationship direction" ,Scalars .GraphQLString ," OUT" ),
293+ GraphQLArgument (" start" ," start field name" ,Scalars .GraphQLString ," start" ),
294+ GraphQLArgument (" end" ," end field name" ,Scalars .GraphQLString ," end" )),false ,false ,true ))
284295 return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring).transform { bc -> bc.additionalDirectives(directives).build() }
285296 }
286297}
0 commit comments