首页 文章

在nodejs环境中使用neo4j模式理解尝试遍历graphql类型的深度2时获取null

提问于
浏览
0

我正在使用neo4j(3.1),GraphQL和Nodejs . 目前,我有3种graphql类型,即Country,State和City,这些类型在Neo4j dB中具有以下关系 .

(市) - [:PRESENT_IN] - >(州) - [:PRESENT_IN] - >(国)

我的graphql类型如下:

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

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

Type City{
name: String,
id: Int
}

我的Query模式是:

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

所以,当我在GraphiQl中运行查询"countries"时,我希望 cities 在一个国家 i.e. traversing to depth 2 中的某个特定状态下也可用,但是当涉及"cities"字段的数组时,我得到null,尽管数据来自字段"states" .

以下是在GraphiQL中执行的查询:

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

这是执行结果:

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

我想要回归的是:

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

对于我的密码查询,我在此链接中使用了模式理解:https://neo4j.com/blog/cypher-graphql-neo4j-3-1-preview/

我的密码查询是:

// 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

所以,谁能告诉我这里做错了什么?我查看了neo4j文档和其他博客,但我无法弄清楚 . 任何帮助或见解都会非常有用!

1 回答

  • 1

    AFAIK,您必须在 countries 查询中查询这些城市 . 像这样的东西:

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

相关问题