0
votes

I am using neo4j(3.1), GraphQL and Nodejs. For now, I have 3 graphql types namely Country, State and City and these types have following relation in Neo4j dB.

(City)-[:PRESENT_IN]->(State)-[:PRESENT_IN]->(Country)

My graphql Type is like below:

Type Country{
name: String,
states: [State]
}

Type State{
name: String,
cities: [City]
}

Type City{
name: String,
id: Int
}

And my Query schema is:

type Query{
 countries(limit: Int): [Country]
 states(limit: Int): [State]
}

So , when I ran the query "countries" in GraphiQl, I was hoping to get the cities too available in a particular state present in one country i.e. traversing to depth 2 , but I am getting null when it comes to array of "cities" field, Although the data comes for field "states".

Here is the query execution done in GraphiQL:

 countries(limit:1) {
   name
    states{
      name    
       cities{
         name
         id
       }
    }
 }

and Here is the execution result:

"countries": {
  "name": "Country 1",
  "states": [
    {
      "name": "State A",
      "cities": null ---> Here it is coming null
    },
  ]
}

What I wanted to return was:

"countries": {
  "name": "Country 1",
  "states": [
    {
      "name": "State A",
        "cities": [
          {
           name: City 1
           id: 1
          },
        ]
    },
  ]
}

For my cypher query, I have used pattern comprehension as in this link: https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/

My cypher query is:

// For Query "countries"
    MATCH (c:Country)
    RETURN c{
      .name,
      states: [(s:State)-[:PRESENT_IN]->(c) | s{.*}]
    } LIMIT $limit

//For Query "states"
    MATCH (s:State)
    RETURN s{
      .name,
      cities: [(ct:City)-[:PRESENT_IN]->(s) | ct{.*}]
    } LIMIT $limit

So, could anyone tell me what I am doing wrong here ? I looked into the neo4j docs and other blogs, but I am unable to figure it out. Any help or insight would be really helpful !

1

1 Answers

1
votes

AFAIK, you have to query for those cities in the countries query. Something like this:

MATCH (c:Country)
RETURN c {
  .name,
  states: [(s:State)-[:PRESENT_IN]->(c) | s {
    .*,
    cities: [(ct:City)-[:PRESENT_IN]->(s) | ct {.*}]
  }]
} LIMIT $limit