-
Notifications
You must be signed in to change notification settings - Fork 49
Fix for relationship mutations are not using the correct direction #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,9 +49,13 @@ fun GraphQLFieldsContainer.relationshipFor(name: String): RelationshipInfo? { | |
?: throw IllegalArgumentException("$name is not defined on ${this.name}") | ||
val fieldObjectType = field.type.inner() as? GraphQLFieldsContainer ?: return null | ||
|
||
val (relDirective, isRelFromType) = if (isRelationType()) { | ||
val (relDirective, inverse) = if (isRelationType()) { | ||
val typeName = this.name | ||
(this as? GraphQLDirectiveContainer) | ||
?.getDirective(DirectiveConstants.RELATION)?.let { it to false } | ||
?.getDirective(DirectiveConstants.RELATION)?.let { | ||
// do inverse mapping, if the current type is the `to` mapping of the relation | ||
it to (fieldObjectType.getFieldDefinition(it.getArgument(RELATION_TO, null))?.name == typeName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to be careful, this doesn't always work. if you have a rich relationship which has the same type in to/from then we need to use the explicit information from that type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need the information the rel-field then I mean that indicates the correct direction otherwise it's "random" if from/to is matching the type name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see: #45 |
||
} | ||
?: throw IllegalStateException("Type ${this.name} needs an @relation directive") | ||
} else { | ||
(fieldObjectType as? GraphQLDirectiveContainer) | ||
|
@@ -60,11 +64,8 @@ fun GraphQLFieldsContainer.relationshipFor(name: String): RelationshipInfo? { | |
?: throw IllegalStateException("Field $field needs an @relation directive") | ||
} | ||
|
||
// TODO direction is depending on source/target type | ||
|
||
val relInfo = relDetails(fieldObjectType) { argName, defaultValue -> relDirective.getArgument(argName, defaultValue) } | ||
val relInfo = relDetails(fieldObjectType, relDirective) | ||
|
||
val inverse = isRelFromType && fieldObjectType.getFieldDefinition(relInfo.startField)?.name != this.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where is this bit handled now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the fix for issue #45 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved it to https://github.com/Andy2003/neo4j-graphql-java/blob/bugfix/%2398-fix-relationship-mapping/src/main/kotlin/org/neo4j/graphql/GraphQLExtensions.kt#L57 since it is only relevant for rich relations |
||
return if (inverse) relInfo.copy(out = relInfo.out?.let { !it }, startField = relInfo.endField, endField = relInfo.startField) else relInfo | ||
} | ||
|
||
|
@@ -121,21 +122,19 @@ fun GraphQLType.ref(): GraphQLType = when (this) { | |
else -> GraphQLTypeReference(name) | ||
} | ||
|
||
fun relDetails(type: GraphQLFieldsContainer, | ||
// TODO simplify uasage (no more callback) | ||
directiveResolver: (name: String, defaultValue: String?) -> String?): RelationshipInfo { | ||
val relType = directiveResolver(RELATION_NAME, "")!! | ||
val outgoing = when (directiveResolver(RELATION_DIRECTION, null)) { | ||
fun relDetails(type: GraphQLFieldsContainer, relDirective: GraphQLDirective): RelationshipInfo { | ||
val relType = relDirective.getArgument(RELATION_NAME, "")!! | ||
val outgoing = when (relDirective.getArgument<String>(RELATION_DIRECTION, null)) { | ||
RELATION_DIRECTION_IN -> false | ||
RELATION_DIRECTION_BOTH -> null | ||
RELATION_DIRECTION_OUT -> true | ||
else -> throw IllegalStateException("Unknown direction ${directiveResolver(RELATION_DIRECTION, null)}") | ||
else -> throw IllegalStateException("Unknown direction ${relDirective.getArgument<String>(RELATION_DIRECTION, null)}") | ||
} | ||
return RelationshipInfo(type, | ||
relType, | ||
outgoing, | ||
directiveResolver(RELATION_FROM, null), | ||
directiveResolver(RELATION_TO, null)) | ||
relDirective.getArgument<String>(RELATION_FROM, null), | ||
relDirective.getArgument<String>(RELATION_TO, null)) | ||
} | ||
|
||
data class RelationshipInfo( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps we can simplify this code a bit so it gets easier to understand.