GraphQL查询从JSON数据源返回GraphiQL和App中的空对象

时间:2019-08-20 20:14:20

标签: json graphql gatsby graphiql

我正在尝试通过Gatsby中的GraphQL获取模拟的JSON数据。响应显示正确的数据,但也显示两个空对象。为什么会发生?

我正在使用gatsby-transformer.json插件查询我的数据,并使用gatsby-source-filesystem将转换器指向我的json文件。

categories.json 我要开始使用的模拟文件:)

{
  "categories": [
    {
      "title": "DEZERTY",
      "path": "/dezerty",
      "categoryItems": [
        {
          "categoryName": "CUKRIKY",
          "image": "../../../../static/img/dessertcategories/cukriky.jpg"
        },
        {
          "categoryName": "NAHODNE",
          "image": "../../../../static/img/dessertcategories/nahodne.jpg"
        },
      ]
    },
    {
      "title": "CANDY BAR",
      "path": "/candy-bar",
      "categoryItems": [
        {
          "categoryName": "CHEESECAKY",
          "image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
        },
        {
          "categoryName": "BEZLEPKOVÉ TORTY",
          "image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
        },
      ]
    }
  ]
}

GrapQL中的

GraphQL查询

query Collections {
  allMockJson {
    edges {
      node {
        categories {
          categoryItems {
            categoryName
            image
          }
          title
          path
        }
      }
    }
  }
}

然后GraphiQL给我的回复

{
  "data": {
    "allMockJson": {
      "edges": [
        {
          "node": {
            "categories": null
          }
        },
        {
          "node": {
            "categories": null
          }
        },
        {
          "node": {
            "categories": [
              {
                "categoryItems": [
                  {
                    "categoryName": "CHEESECAKY",
                    "image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
                  },
                  {
                    "categoryName": "BEZLEPKOVÉ TORTY",
                    "image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
                  }
                ],
                "title": "DEZERTY",
                "path": "/dezerty"
              },
              {
                "categoryItems": [
                  {
                    "categoryName": "CUKRIKY",
                    "image": "../../../../static/img/dessertcategories/CUKRIKY.jpg"
                  },
                  {
                    "categoryName": "NAHODNE",
                    "image": "../../../../static/img/dessertcategories/NAHODNE.jpg"
                  }
                ],
                "title": "CANDY BAR",
                "path": "/candy-bar"
              }
            ]
          }
        }
      ]
    }
  }
}

我只希望获得 DEZERTY CANDY BAR 部分。为什么会有空类别,该如何解决?

预先感谢

1 个答案:

答案 0 :(得分:0)

您的JSON在对象DEZERTY和CANDY BAR中包含语法错误。它无声地失败了,没有告诉你。试试这个json linter

Error: Parse error on line 12: }, Error: Parse error on line 25: },

再试一次。您的查询现在应该可以工作了。

您应该查看突出显示这些错误类型并节省时间和沮丧的IDE。