简体   繁体   English

GraphQL、React、Apollo - 预期可迭代,但没有找到字段“...”

[英]GraphQL, React, Apollo - Expected Iterable, but did not find one for field "..."

I'm a student beginner working on a personal project.我是一名从事个人项目的学生初学者。 I'm trying to build a simple web application that requests a list of heroes from an API ( https://docs.stratz.com/index.html ) and displays their ID and displayname on the webpage.我正在尝试构建一个简单的 Web 应用程序,该应用程序从 API ( https://docs.stratz.com/index.html ) 请求英雄列表并在网页上显示他们的 ID 和显示名称。 I've successfully done something similar with SpaceX launches, but I can't seem to get this to work.我已经成功地完成了与 SpaceX 发射类似的事情,但我似乎无法让它发挥作用。

After setting up my server and running on a local port, I get an error "Expected Iterable, but did not find one for field RootQueryType.Heroes".设置我的服务器并在本地端口上运行后,我收到错误“预期可迭代,但没有找到字段 RootQueryType.Heroes”。 I'm pretty sure this is because in my schema, I've defined my RootQuery as follows:我很确定这是因为在我的架构中,我已经定义了我的 RootQuery 如下:

const HeroType = new GraphQLObjectType({
  name: 'Hero',
  fields: () => ({
    id: {type: GraphQLInt },
    displayName: {type: GraphQLString },
  })
});


// Root Query
const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields:{
    //list of heroes
    Heroes: {
      type: new GraphQLList(HeroType),
      // This is where we get data
      resolve(parent, args){
        return axios
          .get('https://api.stratz.com/api/v1/Hero')
          .then(res => res.data);
      }
    }
  },
});

I think that since I've defined Heroes as a GraphQLList , I get an error because I'm not receiving an iterable or array back from the server.我认为因为我已经将 Heroes 定义为GraphQLList ,所以我收到一个错误,因为我没有从服务器接收到一个可迭代或数组。 In their documents, their server returns a sample that looks like this:在他们的文档中,他们的服务器返回一个如下所示的样本:

{
  "additionalProp1": {
    "id": 0,
    "name": "string",
    "displayName": "string",
    "shortName": "string",
    "abilities": [
      {
        "heroId": 0,
        "gameVersionId": 0,
        "slot": 0,
        "abilityId": 0
      }
    ],
    "roles": [
      {
        "heroId": 0,
        "roleId": 0,
        "gameVersionId": 0,
        "level": 0
      }
    ],
    "talents": [
...
// the list goes on and on with all sorts of info

Please correct me if I'm wrong, but I believe that my issue is that my schema.js file does not deal with "additionalProp1" as listed in their example.如果我错了,请纠正我,但我相信我的问题是我的 schema.js 文件没有处理他们示例中列出的“additionalProp1”。 Could someone point me in the right direction of correcting my schema to deal with this?有人能指出我正确的方向来纠正我的模式来处理这个问题吗?

The API you are hitting appears to returns an object that is a map of hero IDs to hero objects.您正在使用的 API 似乎返回一个对象,该对象是英雄 ID 到英雄对象的映射 It looks like this:它看起来像这样:

{
  "1": {
    "id": 1,
    "name": "npc_dota_hero_antimage",
    ...
  },
  "2": {
    "id": 2,
    "name": "npc_dota_hero_axe",
    ...
  },
  ...
}

What we want is an array of those objects instead.我们想要的是这些对象的数组。 One way to do that would be something like:一种方法是:

.then(res => {
  const heroesById = res.data
  // get an array of the keys of the object
  const ids = Object.keys(heroesById) 
  // map over the array
  return ids.map(id => heroesById[id])
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM