@@ -49,9 +49,13 @@ fun GraphQLFieldsContainer.relationshipFor(name: String): RelationshipInfo? {
4949 ? : throw IllegalArgumentException (" $name is not defined on ${this .name} " )
5050 val fieldObjectType = field.type.inner() as ? GraphQLFieldsContainer ? : return null
5151
52- val (relDirective, isRelFromType) = if (isRelationType()) {
52+ val (relDirective, inverse) = if (isRelationType()) {
53+ val typeName = this .name
5354 (this as ? GraphQLDirectiveContainer )
54- ?.getDirective(DirectiveConstants .RELATION )?.let { it to false }
55+ ?.getDirective(DirectiveConstants .RELATION )?.let {
56+ // do inverse mapping, if the current type is the `to` mapping of the relation
57+ it to (fieldObjectType.getFieldDefinition(it.getArgument(RELATION_TO , null ))?.name == typeName)
58+ }
5559 ? : throw IllegalStateException (" Type ${this .name} needs an @relation directive" )
5660 } else {
5761 (fieldObjectType as ? GraphQLDirectiveContainer )
@@ -60,11 +64,8 @@ fun GraphQLFieldsContainer.relationshipFor(name: String): RelationshipInfo? {
6064 ? : throw IllegalStateException (" Field $field needs an @relation directive" )
6165 }
6266
63- // TODO direction is depending on source/target type
64-
65- val relInfo = relDetails(fieldObjectType) { argName, defaultValue -> relDirective.getArgument(argName, defaultValue) }
67+ val relInfo = relDetails(fieldObjectType, relDirective)
6668
67- val inverse = isRelFromType && fieldObjectType.getFieldDefinition(relInfo.startField)?.name != this .name
6869 return if (inverse) relInfo.copy(out = relInfo.out ?.let { ! it }, startField = relInfo.endField, endField = relInfo.startField) else relInfo
6970}
7071
@@ -121,21 +122,19 @@ fun GraphQLType.ref(): GraphQLType = when (this) {
121122 else -> GraphQLTypeReference (name)
122123}
123124
124- fun relDetails (type : GraphQLFieldsContainer ,
125- // TODO simplify uasage (no more callback)
126- directiveResolver : (name: String , defaultValue: String? ) -> String? ): RelationshipInfo {
127- val relType = directiveResolver(RELATION_NAME , " " )!!
128- val outgoing = when (directiveResolver(RELATION_DIRECTION , null )) {
125+ fun relDetails (type : GraphQLFieldsContainer , relDirective : GraphQLDirective ): RelationshipInfo {
126+ val relType = relDirective.getArgument(RELATION_NAME , " " )!!
127+ val outgoing = when (relDirective.getArgument<String >(RELATION_DIRECTION , null )) {
129128 RELATION_DIRECTION_IN -> false
130129 RELATION_DIRECTION_BOTH -> null
131130 RELATION_DIRECTION_OUT -> true
132- else -> throw IllegalStateException (" Unknown direction ${directiveResolver (RELATION_DIRECTION , null )} " )
131+ else -> throw IllegalStateException (" Unknown direction ${relDirective.getArgument< String > (RELATION_DIRECTION , null )} " )
133132 }
134133 return RelationshipInfo (type,
135134 relType,
136135 outgoing,
137- directiveResolver (RELATION_FROM , null ),
138- directiveResolver (RELATION_TO , null ))
136+ relDirective.getArgument< String > (RELATION_FROM , null ),
137+ relDirective.getArgument< String > (RELATION_TO , null ))
139138}
140139
141140data class RelationshipInfo (
0 commit comments