Skip to content

Commit acdca95

Browse files
committed
add custom field names for relationship directives on object type
1 parent 6609a8a commit acdca95

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/kotlin/org/neo4j/graphql/Translator.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.neo4j.graphql
22

33
import graphql.Scalars
4+
import graphql.introspection.Introspection
45
import graphql.language.*
56
import graphql.parser.Parser
67
import graphql.schema.*
78
import graphql.schema.idl.RuntimeWiring
89
import graphql.schema.idl.SchemaGenerator
910
import graphql.schema.idl.SchemaParser
1011
import org.antlr.v4.runtime.misc.ParseCancellationException
12+
import java.util.*
1113

1214
class 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
}

src/test/kotlin/org/neo4j/graphql/TranslatorTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ class TranslatorTest {
1515
livesIn : Location @relation(name:"LIVES_IN", direction:"OUT")
1616
livedIn : [Location] @relation(name:"LIVED_IN", direction:"OUT")
1717
born : Birth
18+
died : Death
1819
}
1920
type Birth @relation(name:"BORN") {
2021
start: Person
2122
end: Location
2223
date: String
2324
}
25+
type Death @relation(name:"DIED",start:"who",end:"where") {
26+
who: Person
27+
where: Location
28+
date: String
29+
}
2430
type Location {
2531
name: String
2632
founded: Person @relation(name:"FOUNDED", direction:"IN")
@@ -67,6 +73,12 @@ class TranslatorTest {
6773
assertQuery(query, "MATCH (person:Person) RETURN person { .name,born:[(person)-[personBorn:BORN]->(personBornEnd:Location) | personBorn { .date,end:personBornEnd { .name } }][0] } AS person")
6874
}
6975

76+
@Test
77+
fun richRelationshipCustomFieldNames() {
78+
val query = " { person { name died { date where { name } } } } "
79+
assertQuery(query, "MATCH (person:Person) RETURN person { .name,died:[(person)-[personDied:DIED]->(personDiedWhere:Location) | personDied { .date,where:personDiedWhere { .name } }][0] } AS person")
80+
}
81+
7082
@Test
7183
fun richRelationship2ndHop() {
7284
val query = " { person { name born { date end { name founded { name } } } } } "

0 commit comments

Comments
 (0)