How to query relationships of neo4j with custom query with cypher using Grandstack?

363 Views Asked by At

I have these models :

type User {
  userId: ID!
  mail: String!
  password: String! @private
  meals: [Meal!] @relationship(type: "IN_MEALS", direction: OUT)
}

type Meal {
  name: String!
  createdBy: User! @relationship(type: "IN_MEALS", direction: IN)
}

The relationship is made in neo4j and when I query all users but when I use this custom query for the custom users, meals are defined as null I don't know why

type Query {
  currentUser: User
    @cypher(
      statement: """
      MATCH (u:User {userId: $auth.jwt.userId})
      OPTIONAL MATCH (u)-[r:IN_MEALS]->(m:Meal)
      RETURN u,r,m  
      """
    )
}

When I do this query in graphql

{
  currentUser {
    userId
    mail
    meals {
      name
    }
  }
}

It tells me that meals are null but they are not ...

{
  "data": {
    "currentUser": {
      "userId": "02e7b50a-1a51-4d3b-b26b-57ed08d89c5a",
      "mail": "[email protected]",
      "meals": null
    }
  }
}

I know that by looking into my neo4j database and also because when I query all users

{
  users {
    mail
    meals {
      name
    }
  }
}

I can see the values in meals :

{
  "data": {
    "users": [
      {
        "mail": "[email protected]",
        "meals": [
          {
            "name": "frites"
          },
          {
            "name": "mcdo"
          },
          {
            "name": "sushi"
          }
        ]
      }
    ]
  }
}
1

There are 1 best solutions below

5
Tomaž Bratanič On

From what I understand, you only need to provide the toplevel node and then the GRAND stack will handle fetching additional relationships etc. The documentation is not so clear unfortunately it seems. Try using:

type Query {
  currentUser: User
    @cypher(
      statement: """
      MATCH (u:User {userId: $auth.jwt.userId})
      RETURN u  
      """
    )
}