return dynamic keys in GraphQL mutation response

Viewed 90

I saw this answer in the question posted here: Dynamic response from GraphQL mutation

But I am wondering if since this answer, I can somehow return a dynamic key based on the parameter i am sending to the server. For example if I have in the client (React + apollo-client) a place to update a User specific key: And I use only 1 mutation to update the user instead of writing each mutation for each user property.

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
    ...
  } 
}

So i want to do something like:

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

This way I only need uuid + the dynamic key to update the local apollo cache with the returned dynamic value. mainly to reduce the server time to run the deep nested query unnecessarily, instead of having multiple mutations for updating the same entity type.

0 Answers
Related