简体   繁体   English

将多个参数传递给 GraphQL 查询

[英]Passing Multiple Arguments to GraphQL Query

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.感谢这可能是一个有点愚蠢的问题,但我正在使用来自 RDF/Linked Data 世界的 GraphQL,并且在弄清楚如何返回集合时遇到了很多麻烦。 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 .基本上我想要一些我可以选择的东西,让我们说一个Characters列表(使用 GraphQL 文档中的例子)通过他们的id In SPARQL I'd be using the VALUES clause and then binding, something like:在 SPARQL 中,我将使用VALUES子句然后进行绑定,例如:

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:别名可以做我想做的事,但我不想事先或手动指定不同的命名返回值是什么 - 我想在我的代码中说传递一个 ID 列表:

[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. ...并有一个可以接受该 ID 数组的查询,并根据上面的伪查询返回一个可预测的非标量形状的结果。

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?我还假设实际上不可能将查询解析为Human[Human] - 它必须是一个或另一个? 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: ID列表可以按如下方式传递: 传递ID列表

Whit Hasura you can do like this (IDK if that working only on Hasura) Whit Hasura你可以这样做(IDK如果仅适用于Hasura)

query getConfigurationList($ids: [String!]!) {
    configuration (where: {id:{_in: $ids}}){
        id
        value
    }
}

Here is the docs 这是文档

You can use GraphQL Aliases您可以使用GraphQL 别名

{
    first: human(id: "1") {
        name
        height
    }
    second: human(id: "2") {
        name
        height
    }
}

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

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