【发布时间】:2019-06-08 13:46:34
【问题描述】:
我的 GraphQL 架构定义为:
type Query {
getEntity(id: Int!): Entity
getEntityUsers(entityId: Int!, statusId: Int): [User]
}
type Entity {
id: Int!
name: String!
email: String!
logo: String
createdAt: DateTime!
updatedAt: DateTime!
users(statusId: Int): [User]
}
如您所见,我有两种方法可以为Entity 对象获取用户。目前适用于我的查询的是getEntityUsers root resolver 方法。此查询如下所示:
query getEntityUsers($entityId: Int!, $statusId: Int) {
users: getEntityUsers(entityId: $entityId, statusId: $statusId) {
...
}
}
.. 使用变量:
{
entityId: 1,
statusId: 2
}
有没有办法让我通过statusId 使其他方式工作?现在查询看起来像这样:
query getEntity($id: Int!) {
entity: getEntity(id: $id) {
...
users (statusId: 2) {
...
}
}
}
这显然适用于变量:
{
id: 1
}
但是,如果我想使用第二种方法并更改statusId,该怎么办?如果没有在根解析器上定义statusId,是否还有传递?
我已经尝试过查询:
query getEntity($id: Int!) {
entity: getEntity(id: $id) {
...
users (statusId: $statusId) {
...
}
}
}
.. 使用变量:
{
id: 1,
statusId: 2
}
但我只是得到错误:Variable "$statusId" is not defined by operation "getEntity". 无论如何要这样做吗?
【问题讨论】:
标签: graphql apollo-client