【发布时间】:2021-10-08 04:17:08
【问题描述】:
我的更新突变请求有一个输入架构类型,但它仅在我发送输入类型中提到的所有字段时才有效。
extend type Mutation {
createUser(payload: UserInput!): UserResult!
updateUser(id: String!, payload: UpdateUserInput): UserResult!
deleteUser(id: String!): UserResult!
}
input UpdateUserInput {
id: ID,
first_name: String,
last_name: String,
email: String,
role: String,
password: String
}
但我可能只需要更新其中的一些字段,如果我发送所有这些字段,他们肯定会更改为新的输入值。
return {
type: 'UserSuccess',
code: 200,
success: true,
user: await this.update(id, { ...payload, password: hashSync(payload.password, HASH_ROUNDS) }),
message: `User updated successfully`
}
我对前端部分的要求是:
mutation updateUser($id: String!, $payload: UpdateUserInput!) {
updateUser(id: $id, payload: $payload) {
__typename
... on UserSuccess {
...Response
user {
id
first_name
last_name
email
role
}
}
... on UserError {
...Response
}
}
}
fragment Response on Response {
code
success
message
}
我得到的错误是:
GraphQL error: Illegal arguments: undefined, string
我怎样才能让我的架构只接收那里的一些字段?因为我无法预测用户会更新哪些字段
【问题讨论】:
标签: api graphql apollo-client