neo4j-graphql / neo4j-graphql-java

Neo4j Labs Project: Pure JVM translation for GraphQL queries and mutations to Neo4j's Cypher
Apache License 2.0
105 stars 49 forks source link

Getting wrong Result with outgoing/incoming relationship with same type #293

Closed rajankit296 closed 1 year ago

rajankit296 commented 1 year ago

We have added a Schema where we have incoming/outgoing relationship with same type. When we try to fetch result from graphql query, we get opposite result. Instead of outgoing node details, we are getting incoming node details.

GraphQL schema

type ActionType{
    id: String!
    outgoingActionTypeRel: [OutGoingActionTypeRelationship]
}

type OutGoingActionTypeRelationship @relation(name:"HAS", from:"actionType", to:"actionType" direction: OUT){
    id: String
    actionType: ActionType
}

GraphQL request

query{
  actionType{
    id
    outgoingActionTypeRel{
      id
      actionType{
        id
      }
    }
  }
}

Generated Cypher

MATCH (actionType:ActionType)
CALL {
    WITH actionType
    MATCH (actionType)<-[actionTypeOutgoingActionTypeRel:HAS]-(actionTypeOutgoingActionTypeRelActionType:ActionType)
    RETURN collect(actionTypeOutgoingActionTypeRel {
        .id,
        actionType: actionTypeOutgoingActionTypeRelActionType {
            .id
        }
    }) AS actionTypeOutgoingActionTypeRel
}
RETURN actionType {
    .id,
    outgoingActionTypeRel: actionTypeOutgoingActionTypeRel
} AS actionType

Here, the direction of "actionTypeOutgoingActionTypeRel" should be from "actionType" to "actionTypeOutgoingActionTypeRelActionType"

but it is generating opposite.

rajankit296 commented 1 year ago

@Andy2003 Here, we want to fetch outgoing nodes but we are not able to proceed due to this issue. Can you please help here? Thanks

Andy2003 commented 1 year ago

You should define the rich-relation type OutGoingActionTypeRelationship with two different named fields, one for the start-node and one for the end-node of the relation, e.g.:

type OutGoingActionTypeRelationship @relation(name:"HAS", from:"from", to:"to" direction: OUT){
    id: String
    from: ActionType
    to: ActionType
}

having just one field used both, for from and to is a misconfiguration.

If you don't need rich-relations, you can also define the relation directly on the field. Please take a look at the examples.

rajankit296 commented 1 year ago

Thanks @Andy2003 for the suggestion.

As suggested above, we changed the schema like this:

We are using neo4j-graphql-java 1.6.0

Graphql Schema

type ActionType{
    id: String!
    outgoingActionTypeRel: [OutGoingActionTypeRelationship]
}

type OutGoingActionTypeRelationship @relation(name:"HAS", from:"from", to:"to", direction: OUT){
    id: String
    from: ActionType
    to: ActionType
}

Graphql Query

query{
  actionType{
    id
    outgoingActionTypeRel{
      id
      to{
        id
      }
    }
  }
}

Generated Cypher

MATCH (actionType:ActionType)
CALL {
    WITH actionType
    MATCH (actionType)<-[actionTypeOutgoingActionTypeRel:HAS]-(actionTypeOutgoingActionTypeRelFrom:ActionType)
    RETURN collect(actionTypeOutgoingActionTypeRel {
        .id,
        to: actionTypeOutgoingActionTypeRelFrom {
            .id
        }
    }) AS actionTypeOutgoingActionTypeRel
}
RETURN actionType {
    .id,
    outgoingActionTypeRel: actionTypeOutgoingActionTypeRel
} AS actionType

There is no change in the direction.

Andy2003 commented 1 year ago

Take a look at this example on how to configure the relation correctly!