GraphQL-操作未定义的变量

GraphQL-操作未定义的变量,graphql,apollo-client,Graphql,Apollo Client,我的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: DateTim

我的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]
}
如您所见,我有两种方法获取
实体
对象的用户。当前用于我的查询的是
getEntityUsers
root解析器方法。此查询如下所示:

query getEntityUsers($entityId: Int!, $statusId: Int) {
        users: getEntityUsers(entityId: $entityId, statusId: $statusId) {
            ...
        }
    }
query getEntity($id: Int!) {
        entity: getEntity(id: $id) {
            ...
            users (statusId: 2) {
                ... 
            }
        }
    }
。。使用以下变量:

{
    entityId: 1,
    statusId: 2
}
{
    id: 1
}
{
    id: 1,
    statusId: 2
}
通过允许我传入
statusId
,是否还有其他方法可以起作用?现在查询如下所示:

query getEntityUsers($entityId: Int!, $statusId: Int) {
        users: getEntityUsers(entityId: $entityId, statusId: $statusId) {
            ...
        }
    }
query getEntity($id: Int!) {
        entity: getEntity(id: $id) {
            ...
            users (statusId: 2) {
                ... 
            }
        }
    }
这显然适用于变量:

{
    entityId: 1,
    statusId: 2
}
{
    id: 1
}
{
    id: 1,
    statusId: 2
}
但是,如果我想使用第二种方法并更改
statusId
,该怎么办?如果未在根解析器上定义,是否仍要传入
状态ID

我尝试了以下查询:

query getEntity($id: Int!) {
        entity: getEntity(id: $id) {
            ...
            users (statusId: $statusId) {
                ... 
            }
        }
    }
。。使用以下变量:

{
    entityId: 1,
    statusId: 2
}
{
    id: 1
}
{
    id: 1,
    statusId: 2
}
但我只是得到了一个错误:
变量“$statusId”不是由操作“getEntity”定义的。
是否有这样做的方法?

每个操作(查询或变异)都必须明确定义在该操作中使用的任何变量。因此,如果您有一个名为
$statusId
的变量,则必须将此变量的类型指定为操作定义的一部分:

query getEntity($id: Int!, $statusId: Int) {
  # your selection set here
}

在查询中使用这些变量的位置(无论是在根级别还是在其他位置)是不相关的——它们必须始终作为操作定义的一部分进行定义。

您可以在
Int上展开这里的内容吗vs
Int
?@Elijahlyn请参见