首页 > 解决方案 > 如果每个子列表的至少一个元素包含查询的字段,graphiql 才允许查询

问题描述

我正在使用 graphiQL 来调试我的 react 项目的 graphql 查询。

这是我的查询:

{
  testdataJson{
    E{
      D{
        A
        B
        entries
      }
    }
  }
}

在数据集 testdata.json 上:

{
  "E": [
    {
      "D": [
        [
          {
            "A": "Y1",
            "entries": [
              "Z1"
            ]
          },
          {
            "A": "Y2",
            "entries": [
              "Z2"
            ]
          }
        ],
        [
          {
            "A": "Y3",
            "entries": [
              "Z3"
            ]
          },
          {
            "A": "Y4",
            "entries": [
              "Z4"
            ],
            "B": true
          }
        ],
        [
          {
            "A": "Y5",
            "entries": [
              "Z5"
            ],
            "B": true
          }
        ]
      ]
    }
  ]
}

这给出了错误:Cannot query field \"B\" on type \"TestdataJsonED\".

如果我更改片段:

[
      {
        "A": "Y1",
        "entries": [
          "Z1"
        ]
      },
      {
        "A": "Y2",
        "entries": [
          "Z2"
        ]
      }
],

[
      {
       "A": "Y1",
       "entries": [
          "Z1"
        ]
      },
      {
        "A": "Y2",
        "entries": [
          "Z2"
        ]
        "B": true
      }
],

那么一切都很好。

这似乎很奇怪,因为我希望能够拥有不一定包含至少一个具有“B”值的元素的列表。事实上,我的 JSON 数据(不是我创建的)有很多。或者,这不是原因,我做了一些愚蠢的事情!

干杯,

编辑:相关问题 - https://github.com/gatsbyjs/gatsby/issues/2392

标签: jsongraphql

解决方案


推荐阅读