将多个参数传递给 GraphQL 查询 [英] Passing Multiple Arguments to GraphQL Query

查看:24
本文介绍了将多个参数传递给 GraphQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一件事

感谢这可能是一个有点愚蠢的问题,但我正在使用来自 RDF/链接数据世界的 GraphQL,并且在弄清楚如何返回集合时遇到了很多麻烦.基本上我想要一些我可以选择的东西,让我们说一个 Characters 列表(使用 GraphQL 文档中的例子)通过他们的 id.在 SPARQL 中,我将使用 VALUES 子句然后绑定,例如:

VALUES { <http://uri/id-2><http://uri/id-3>}

我假设这样的东西就是我想要的(伪代码)

<代码>{人类(ID:[1",2",3",4",5"]){名称高度}}

First thing

Appreciate this may be a bit of a stupid question, but I'm working with GraphQL having come from the RDF/Linked Data world and having a lot of trouble getting my head around how I would return a set. Essentially I want something where I could select, let's say a list of Characters (using the examples from the GraphQL docs) via their id. In SPARQL I'd be using the VALUES clause and then binding, something like:

VALUES { <http://uri/id-1> <http://uri/id-2> <http://uri/id-3> }

I'd assume something like this would be what I'd want (pseudocode)

{
  human(id: ["1", "2", "3", "4", "5"]) {
    name
    height
  }
}

Aliases kind of do what I want, but I don't want to have to specify in advance or manually what the different named return values are - I want to say in my code pass a list of IDs:

[1 2 3 4 5]

...and have a query that could accept that array of IDs and return me results in a predictable non-scalar shape as per the pseudo-query above.

Second thing

I'm also assuming that it's in fact not possible to have a query resolve to either a Human or [Human] - that it has to be one or the other? No biggie if so, I'd just settle for the latter... but I think I'm just generally quite confused over this now.

解决方案

First you need to extend you query by adding a "humans":

extend type Query {
  humans(listId: [String!]): [Human!]
  human(id: ObjID!): Human
}

Second - write a resolver for it.

Query: {
  humans(root, {listId}, { Human }) {
    return Human.fillAllByListId(listId);
  },
  ...
},

List of IDs could be passed as follows:

这篇关于将多个参数传递给 GraphQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆