简体   繁体   中英

Dynamic responses in relay modern mutations

I have an application that has multiple profiles for a given user. The user profile can be toggled from the application's header, so that toggle can happen from any page/route in the app.

Because toggling can happen anywhere, I'm finding that I need to retrieve the fragments for every possible page, so that after the mutation succeeds, the page is updated, regardless of which route is active. This is neither performant, nor scalable. My current mutation query looks something like this:

mutation UserProfile_Mutation($input: !UserProfileInput) {
 updateProfile(input: $input) {
   profile {
     ...Page1_profile
     ...Page2_profile
     ...etc
   }     
  }
}

I could create a different mutation query for each page, and then have the mutation function look up the query based on the route... That seems like it'd work but feels verbose and not particularly elegant.

Is there some cleaner way I can dynamically specify which fragments I want?

You could run or skip the fragments conditionally. Take a look at this: Relay Conditional Fields

So, you could pass extra arguments to your mutation (maybe a new type of object?), and then, use this values to run or not the fragments. For example:

mutation UserProfile_Mutation($input: !UserProfileInput, $extraArg: Boolean!) {
 updateProfile(input: $input) {
   profile {
     ...Page1_profile  @include(if: $extraArg)
     ...Page2_profile  @include(if: $extraArg) // or any other arg
     ...etc
   }     
  }
}

Hope it helps! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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