首页 > 解决方案 > 在 GraphQL 突变响应中返回动态键

问题描述

我在这里发布的问题中看到了这个答案:来自 GraphQL 突变的动态响应

但我想知道自从这个答案以来,我是否可以根据我发送到服务器的参数以某种方式返回一个动态密钥。例如,如果我在客户端(React + apollo-client)中有一个更新用户特定密钥的地方:并且我只使用 1 个突变来更新用户,而不是为每个用户属性编写每个突变。

mutation updateUser(
  $userUuid: String!
  $userName: String
  $userOtherDeepNestedObject: OtherDeepNestedObjectInput
  ...
) {
  updateUser(
   userUuid: $userUuid,
   userUpdate: {
     name: $userName,
     otherDeepNestedObject: $userOtherDeepNestedObject
     ...
   }
) {
    uuid
    name
    otherDeepNestedObject {...} # this refetch will take long. i want to skip it, and only refetch if updated
    ...
  } 
}

所以我想做类似的事情:

mutation updateUser(
  $userUuid: String!
  $userName: String
  $userOtherDeepNestedObject: OtherDeepNestedObject
) {
  updateUser(
   userUuid: $userUuid,
   userUpdate: {
     name: $userName,
     otherDeepNestedObject: $otherDeepNestedObject
   }
) {
    uuid
    [dynamicKey]
  } 
}

这样我只需要 uuid + 动态键就可以用返回的动态值更新本地 apollo 缓存。主要是为了减少服务器不必要地运行深度嵌套查询的时间,而不是为更新相同的实体类型而进行多个突变。

标签: reactjsgraphql

解决方案


推荐阅读