来自 GraphQL 突变的动态响应 [英] Dynamic response from GraphQL mutation
问题描述
我正在使用 Apollo-Client 来处理 GraphQL 后端 (PHP),并且遇到了一些突变问题.
I'm using Apollo-Client to work with a GraphQL back-end (PHP) and have been having some issues with mutations.
我正在处理的当前组件集是用于用户详细信息的表单.使用查询获取和显示详细信息,并且可以编辑和保存.当用户保存时,会进行检查以确定哪些值已更改,并通过更改发送这些值以进行更新.
The current set of components I'm working on are a form for user details. The details are fetched and displayed with use of a query and can be edited and saved. When a user saves a check is made to decide which values have been changed and these are sent via a mutation to be updated.
我遇到的问题是我只希望从突变返回已更新的数据,因为返回可能已更新的每个字段都会为响应增加很多时间.有没有办法动态构建mutation来决定返回哪些字段?
The issue I'm having is I'd only like the data which has been updated to be returned from the mutation, as returning every field that could have been updated adds a lot to the response time. Is there a way to dynamically build the mutation to decide which fields are returned?
这是当前状态的突变:
const UPDATE_CLIENT = gql`
mutation updateClient( $data: [ClientInput]! ) {
add_update_clients( data: $data ) {
id
ref
name {
title
firstname
surname
preferred_name
}
gender
dob
nhs_number
email
telephone {
number
}
mobile {
number
}
region {
id
name
}
referral_received
user_aware_of_referral
referred_for {
id
name
}
weekly_hours
contract_date
service_start_date
expected_end_date
service_end_date
reason_left
po_number
accounting_ref
country_of_birth {
id
name
}
nationality {
id
name
}
ni_number
place_of_birth
ethnicity {
id
name
}
first_language {
id
name
}
religion {
id
name
}
marital_status
dependants
sexual_orientation
height
weight
hair_colour
eye_colour
}
}
`;
还有一小段代码提交
const mutation = await client.mutate({
mutation: UPDATE_CLIENT,
variables: { data },
errorPolicy: 'all'
});
但本质上,如果我更新 dob
那么这是我在响应中唯一想要的数据
But essentially, if I update dob
then that is the only piece of data I'd like in the response
推荐答案
您可以使用 @skip
或 @include
指令来指定应从选择集.如果您要跟踪哪些字段已在客户端更新,您可以使用这些值来创建一组变量以传递给您的查询.
You can use either @skip
or @include
directives to specify fields that should be omitted from the selection set. If you're keeping track of which fields have been updated client-side, you can use those values to create a set of variables to pass to your query.
mutation updateClient(
$data: [ClientInput]!
$includeGender: Boolean = false
$includeDob: Boolean = false
$includeEmail: Boolean = false
# ... and so on
) {
add_update_clients( data: $data ) {
gender @include(if: $includeGender)
dob @include(if: $includeDob)
email @include(if: $includeEmail)
# ... and so on
}
}
这篇关于来自 GraphQL 突变的动态响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!