简体   繁体   English

GraphQL突变的动态响应

[英]Dynamic response from GraphQL mutation

I'm using Apollo-Client to work with a GraphQL back-end (PHP) and have been having some issues with mutations. 我正在使用Apollo-Client与GraphQL后端(PHP)配合使用,并且遇到了一些突变问题。

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. 当用户保存时,将进行检查以确定哪些值已更改,并通过要更新的突变发送这些值。

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? 有没有一种方法可以动态构建突变以确定返回哪些字段?

Heres the mutation in it's current state: 这是当前状态下的突变:

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
    }
}
`;

And a small piece of code to submit it 并提交一小段代码

const mutation = await client.mutate({
            mutation: UPDATE_CLIENT,
            variables: { data },
            errorPolicy: 'all'
        });

But essentially, if I update dob then that is the only piece of data I'd like in the response 但是从本质dob ,如果我更新dob那么那是我在响应中唯一想要的数据

You can use either @skip or @include directives to specify fields that should be omitted from the selection set. 您可以使用@skip@include指令来指定应从选择集中省略的字段。 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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM