简体   繁体   English

如何在GraphQL解析器中使用多个API调用来构造承诺数组,以返回单个对象类型列表

[英]How to architect array of promises in GraphQL resolver with multiple API calls to return a single object type list

I'm stuck in my GraphQL resolver fetching todo-lists for a particular user belonging to a company. 我被困在我的GraphQL解析器中,它为公司的特定用户获取待办事项列表。 According to whether or not they have access to all todo-lists or a certain few, it will fetch for groups the user registered to that have belonging todo-lists, and those should be fetched. 根据他们是否有权访问所有待办事项列表或少数几个待办事项列表,它将为已注册的用户拥有属于待办事项列表的组取回信息,并应将其取回。

The code so far is capable of logging the requested todo-lists on the query but I have yet to come to the solution on how to actually return data of all of the user's registered groups's todo-lists. 到目前为止的代码能够记录查询中所请求的待办事项列表,但是我还没有找到有关如何实际返回用户注册组的所有待办事项列表数据的解决方案。

I chose to export the actual logic into a separate function 我选择将实际逻辑导出到单独的函数中

The Resolver: 解析器:

allowedListItems: {
  type: new GraphQLList(TodoItem),
  resolve(parentValue, args) {
    return Promise.all([fetchAllowedItems(parentValue._id)]);
  }
},

The Promise Function 承诺功能

function fetchAllowedItems(userId) {
  return User.findOne({ _id: userId }).then((user) => {
    if (user.todoGroups) {
      return user.todoGroups.map((groupId) => {
        return TodoGroup.findOne({ _id: groupId }).then(group => {
          return group.todoLists.map((listId) => {
            return TodoList.findOne({ _id: listId })
          })
        })
      })
    } else {
      return TodoList.find({ company: parentValue.company }).exec();
    }
  })
}

I am not getting any errors from GraphQL so I guess it's about the way I make the promisses return to the resolver, I'd appreciate a lot if you can help me out! 我没有从GraphQL收到任何错误,所以我想这是我让答辩者返回解析器的方式,如果您能帮助我,我将不胜感激!

Update: 更新:

I should wrap the maps with a Promise.all, as the mapping returns an array. 我应该用Promise.all包装地图,因为映射返回一个数组。 Though the updated code brings no improvement in the returned data. 尽管更新后的代码对返回的数据没有任何改善。

async resolve(parentValue, args) {
    let user = await User.findOne({ _id: parentValue._id })

    if (user.todoGroups) {
      return Promise.all(user.todoGroups.map((groupId) => {
        return TodoGroup.findOne({ _id: groupId }).then(group => {
          return Promise.all(group.todoLists.map((listId) => {
            return TodoList.findOne({ _id: listId });
          }))
        })
      }))
    } else {
      return TodoList.find({ company: parentValue.company }).exec();
    }
  }
},

Current query result: 当前查询结果:

{
  "data": {
    "user": {
      "_id": "5ba11690ad7a93d2b34d21a9",
      "allowedTodos": [
        {
          "_id": null,
          "title": null
        }
      ]
    }
  }
}

You need to call Promise.all on an array of promises, not a promise for that. 您需要在一个Promise.all数组上调用Promise.all ,而不是Promise.all Also you'll have to call it on each level: 另外,您还必须在每个级别上调用它​​:

allowedListItems: {
  type: new GraphQLList(TodoItem),
  resolve(parentValue, args) {
    return User.findOne({ _id: parentValue._id }).then(user => {
      if (user.todoGroups) {
        return Promise.all(user.todoGroups.map(groupId => {
//             ^^^^^^^^^^^^
          return TodoGroup.findOne({ _id: groupId }).then(group => {
            return Promise.all(group.todoLists.map(listId => {
//                 ^^^^^^^^^^^^
              return TodoList.findOne({ _id: listId })
            }));
          });
        }));
      } else {
        return TodoList.find({ company: parentValue.company }).exec();
      }
    });
  }
}

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

相关问题 返回自定义 object 到 GraphQL 解析器 - Return custom object to GraphQL resolver (Graphql) 解析器值不是对象类型 - (Graphql) The resolver value isn't of type object GraphQL列表或单个对象 - GraphQL List or single object 如何进行多个API调用node.js承诺 - how to make multiple api calls node.js promises 如何在GraphQL中返回一个对象数组,可能使用与返回单个对象的端点相同的端点? - How to return an array of objects in GraphQL, possibly using the same endpoint as the one that returns a single object? 呈现页面需要多个rest api调用。 如何进行单个API调用,例如graphql-Facebook? 有没有像graphql这样的开放源代码? - Multiple rest api calls needed to render a page. How to make single api call like graphql - facebook? Any open source available like graphql? 使用嵌套的 API 调用处理一系列承诺 - Handling an array of promises with nested API calls 如何处理单个数组上的多个异步调用? - How to approach multiple async calls on single array? 如何修改ajax调用或promise链,以便可以从单独的ajax调用中使用多个变量(来自数组)? - How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls? 如何使用Promise等待异步API调用 - How to use promises to wait for async API calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM