0

我有一个应用程序,它具有给定用户的多个配置文件。可以从应用程序的标题切换用户配置文件,以便可以从应用程序中的任何页面/路由进行切换。

因为切换可以发生在任何地方,我发现我需要为每个可能的页面检索片段,以便在突变成功后,无论哪个路由处于活动状态,页面都会更新。这既不是高性能的,也不是可扩展的。我当前的突变查询看起来像这样:

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

我可以为每个页面创建一个不同的突变查询,然后让突变函数根据路由查找查询……这似乎可行,但感觉很冗长,而且不是特别优雅。

有没有更简洁的方法可以动态指定我想要的片段?

4

1 回答 1

1

您可以有条件地运行或跳过片段。看看这个: 中继条件字段

因此,您可以将额外的参数传递给您的突变(可能是一种新类型的对象?),然后使用这些值来运行或不运行片段。例如:

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

希望能帮助到你!:)

于 2018-12-05T00:15:25.203 回答