Apollo GraphQL 变量参数名称
Apollo GraphQL variable argument name
背景: 我在前端构建了一个带有单元格编辑的数据网格。每次您编辑单元格中的字段时,它都会立即在服务器上更新。所以我认为在我的突变中只提交那个单一字段是一个很好的做法。这将减少网络流量大小,后端将更快地处理更新(当然,这都是非常高水平的优化)。
这意味着我使用 GraphQL 突变发送的参数是可变的。您可以很好地通过 GraphQL 变量注入参数值,但是键的好方法是什么?
为了使问题形象化,这是我想要的:
mutation($id: 1, $field: "first_name", $value: "John") {
updateClient(
id: $id,
$field: $value
) {
id
}
}
不,不幸的是参数名称不能在查询中可变。惯例是使用输入对象类型进行突变来规避这个问题:
type Mutation {
updateClient(id: ID!, input: ClientInput!): Client
}
input ClientInput {
a: String
b: Number
…
}
(而不是 updateClient(id: ID!, a: String, b: Number, …)
)
使用此模式,您可以将 ClientInput
类型的对象作为参数传递给您的突变:
query(`mutation($id: ID!, $input: ClientInput!) {
updateClient(id: $id, input: $input) {
id
}
}`, {id: 1, input: {["first_name"]: "John"}})
我真的希望 GraphQL 中有某种参数扩展语法来使这种嵌套变得不必要。
背景: 我在前端构建了一个带有单元格编辑的数据网格。每次您编辑单元格中的字段时,它都会立即在服务器上更新。所以我认为在我的突变中只提交那个单一字段是一个很好的做法。这将减少网络流量大小,后端将更快地处理更新(当然,这都是非常高水平的优化)。
这意味着我使用 GraphQL 突变发送的参数是可变的。您可以很好地通过 GraphQL 变量注入参数值,但是键的好方法是什么?
为了使问题形象化,这是我想要的:
mutation($id: 1, $field: "first_name", $value: "John") {
updateClient(
id: $id,
$field: $value
) {
id
}
}
不,不幸的是参数名称不能在查询中可变。惯例是使用输入对象类型进行突变来规避这个问题:
type Mutation {
updateClient(id: ID!, input: ClientInput!): Client
}
input ClientInput {
a: String
b: Number
…
}
(而不是 updateClient(id: ID!, a: String, b: Number, …)
)
使用此模式,您可以将 ClientInput
类型的对象作为参数传递给您的突变:
query(`mutation($id: ID!, $input: ClientInput!) {
updateClient(id: $id, input: $input) {
id
}
}`, {id: 1, input: {["first_name"]: "John"}})
我真的希望 GraphQL 中有某种参数扩展语法来使这种嵌套变得不必要。