将多个参数传递给 GraphQL 查询
Passing Multiple Arguments to GraphQL Query
第一件事
感谢这可能是一个有点愚蠢的问题,但我正在使用来自 RDF/Linked 数据世界的 GraphQL,并且很难理解我将如何 return一套。本质上我想要一些我可以 select 的东西,比方说 Characters
的列表(使用 GraphQL 文档中的示例)通过他们的 id
。在 SPARQL 中,我将使用 VALUES
子句然后进行绑定,例如:
VALUES { <http://uri/id-1> <http://uri/id-2> <http://uri/id-3> }
我想这就是我想要的(伪代码)
{
human(id: ["1", "2", "3", "4", "5"]) {
name
height
}
}
Aliases 有点像我想做的,但我不想提前或手动指定不同的 return 值是什么 - 我想在我的代码中说传递 ID 列表:
[1 2 3 4 5]
...并有一个可以接受该 ID 数组的查询,并且 return 我根据上面的伪查询生成可预测的非标量形状。
第二件事
我还假设实际上不可能将查询解析为 Human
或 [Human]
- 它必须是其中之一?如果是这样,那没什么大不了的,我只会满足于后者……但我想我现在对此一般都很困惑。
首先,您需要通过添加 "humans":
来扩展您的查询
extend type Query {
humans(listId: [String!]): [Human!]
human(id: ObjID!): Human
}
其次 - 为它写一个解析器。
Query: {
humans(root, {listId}, { Human }) {
return Human.fillAllByListId(listId);
},
...
},
可以按如下方式传递 ID 列表:
Whit Hasura 你可以这样做(如果只能在 Hasura 上工作,IDK)
query getConfigurationList($ids: [String!]!) {
configuration (where: {id:{_in: $ids}}){
id
value
}
}
这是docs
您可以使用GraphQL Aliases
{
first: human(id: "1") {
name
height
}
second: human(id: "2") {
name
height
}
}
第一件事
感谢这可能是一个有点愚蠢的问题,但我正在使用来自 RDF/Linked 数据世界的 GraphQL,并且很难理解我将如何 return一套。本质上我想要一些我可以 select 的东西,比方说 Characters
的列表(使用 GraphQL 文档中的示例)通过他们的 id
。在 SPARQL 中,我将使用 VALUES
子句然后进行绑定,例如:
VALUES { <http://uri/id-1> <http://uri/id-2> <http://uri/id-3> }
我想这就是我想要的(伪代码)
{
human(id: ["1", "2", "3", "4", "5"]) {
name
height
}
}
Aliases 有点像我想做的,但我不想提前或手动指定不同的 return 值是什么 - 我想在我的代码中说传递 ID 列表:
[1 2 3 4 5]
...并有一个可以接受该 ID 数组的查询,并且 return 我根据上面的伪查询生成可预测的非标量形状。
第二件事
我还假设实际上不可能将查询解析为 Human
或 [Human]
- 它必须是其中之一?如果是这样,那没什么大不了的,我只会满足于后者……但我想我现在对此一般都很困惑。
首先,您需要通过添加 "humans":
来扩展您的查询extend type Query {
humans(listId: [String!]): [Human!]
human(id: ObjID!): Human
}
其次 - 为它写一个解析器。
Query: {
humans(root, {listId}, { Human }) {
return Human.fillAllByListId(listId);
},
...
},
可以按如下方式传递 ID 列表:
Whit Hasura 你可以这样做(如果只能在 Hasura 上工作,IDK)
query getConfigurationList($ids: [String!]!) {
configuration (where: {id:{_in: $ids}}){
id
value
}
}
这是docs
您可以使用GraphQL Aliases
{
first: human(id: "1") {
name
height
}
second: human(id: "2") {
name
height
}
}