cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to define and handle parameter in GraphQL schema

I have a type in my schema and it looks like this (simplified):

type CompetencyCategory {
  _id: Long!
  id: String!
  label: String!
  competencys: [Competency] @relation(name: "IS_IN_GROUP", direction: "IN")
  sumOfChildComps: Int
    @cypher(
      statement: """
      MATCH (this)-[:CATEGORY_HAS_COMPETENCIES_OF]->(comp:Competency)-[up:HAS_USER_PROGRESS]-(p1:Progress)
      WHERE up.userId = '1'
      RETURN sum(p1.currentLevel)
      """
    )
}

When I query the data with GraphQL:

{
  CompetencyCategory {
    id
    label
    sumOfChildComps
  }
}

I get this as expected:

{
  "data": {
    "CompetencyCategory": [
      {
        "id": "LT-1932629939382148",
        "label": "Group1",
        "sumOfChildComps": 22
      }
}

Now I want to replace the hardcoded WHERE up.userId = '1' with a variable such as
WHERE up.userId = $targetUserId and provide a value for that variable on every query.

So, how do I define an arbitrary string variable (not an named object in the database) in the schema, and where do I pass a parameter on every query to replace the hardcoded "1" in WHERE up.userId = '1'?

I see that cypher params (for things like authentication) are defined when ApolloServer is created, so that's way too early. This value will change on every query.

I'm using Neo4j 3.5, and neo4j-graphql-js 2.12.1

~Matthew

3 REPLIES 3

MuddyBootsCode
Graph Steward

You would need to go ahead and write a custom cypher query and include it in your schema queries. At this point, you can only refer to the node your calling with cypher directives on your schema types. You could alternatively work backwards and create a cypher cirective field on your user that returns all of the users competencies and skill levels and include that in your over all query.

Thanks for the quick reply!

At this point, you can only refer to the node your calling with cypher directives on your schema types.

Ahh, That's a shame. As you suggest, I guess I will run one query to list the competencies (with an id), and another to get the user's per-competency scores (with the same id) and link them later in Javascript.
I was hoping to be able to use Neo4J's Aggregation functions to do the nested summing, because I have a fairly deep nested structure.

MuddyBootsCode
Graph Steward

Yeah it can occasionally be a pain but you can get a users competencies and scores and pass those along in context, etc. Not perfect by a long shot but it can cut down the other queries you have to write.