简体   繁体   English

React Relay更新根查询作为突变胖查询的一部分

[英]React Relay update a root query as part of a mutation fat query

I am trying to build an application using GraphQL and React Relay. 我正在尝试使用GraphQL和React Relay构建应用程序。

As part of this I have created a root query with the following specification: 为此,我创建了一个具有以下规范的根查询:

query {
  AllServices {
    edges {
      node {
        id
      }
    }
  }
}

So I have created a mutation - which works well - called CreateGithubService. 因此,我创建了一个叫做CreateGithubService的突变-效果很好。

Here is a snippet from the Relay mutation: 这是Relay突变的摘录:

getFatQuery() {
      return Relay.QL`
        fragment on CreateGithubServicePayload {
          createdService
        }
      `
  }

  getConfigs() {
      return [{
          type: "REQUIRED_CHILDREN",
          children: [
              Relay.QL`
                  fragment on CreateGithubServicePayload {
                      createdService {
                          id
                      }
                  }
              `
          ]
      }]
  }

The problem I have is that this is not causing the views which rely on the information to update. 我的问题是,这不会导致依赖信息的视图更新。

My AllServices query is not refetched and I am unable to specify it in the Fat Query. 无法重新获取我的AllServices查询,并且无法在Fat查询中指定它。

How can I setup my mutation to add the element to the all services query? 如何设置我的变体以将元素添加到所有服务查询中?

Thanks! 谢谢!

REQUIRED_CHILDREN will not update the local graph; REQUIRED_CHILDREN将不会更新本地图; it's strictly for fetching extra data that is only used in the success callback of the mutation update. 它仅用于获取仅在变异更新的成功回调中使用的额外数据。 In fact, it's not even documented outside of the source code, so I'm not sure how you decided to use it like this... 实际上,它甚至没有在源代码之外进行记录,因此我不确定您是如何决定像这样使用它的。

Although you haven't said so, I assume you want this new node ( createdService ) to be added to the AllServices connection? 尽管您没有这么说,但我假设您想将此新节点( createdService )添加到AllServices连接中? If so, you need to tell Relay that your mutation affects that connection: 如果是这样,您需要告诉中继您的突变会影响该连接:

  1. At the moment, Relay basically assumes all mutations affect nodes, not arbitrary queries. 目前,Relay基本上假设所有变异都影响节点,而不是任意查询。 To my knowledge, you won't be able to configure a mutation to update a non-node root query. 据我所知,您将无法配置一个变异来更新非节点根查询。 Therefore, you should add a node at the root that acts as the parent of your AllServices connection (the convention for this is typically viewer ). 因此,您应该在根节点处添加一个节点,该节点充当AllServices连接的父节点(此约定通常是viewer )。 In other words, make it work like this: query { viewer { AllServices { ... } } } 换句话说,使它像这样工作: query { viewer { AllServices { ... } } }

  2. Your mutation payload should always return everything that has been changed, not just the new data. 您的突变有效载荷应始终返回已更改的所有内容,而不仅仅是新数据。 That means you need a way to refetch the AllServices connection from the payload. 这意味着您需要一种方法来从有效负载中重新获取AllServices连接。 Once you add the viewer node to your schema, you can return that node in the mutation payload and change your fat query to specify that the connection has changed, eg: fragment on CreateGithubServicePayload { viewer { AllServices } } viewer节点添加到架构后,您可以在突变有效负载中返回该节点,并更改胖查询以指定连接已更改,例如: fragment on CreateGithubServicePayload { viewer { AllServices } }

  3. Given those two schema changes, you can then configure your mutation using FIELDS_CHANGE and specify the fieldIDs like { viewer: this.props.viewer.id } 给定这两个架构更改,您就可以使用FIELDS_CHANGE配置您的变异,并指定字段ID,例如{ viewer: this.props.viewer.id }

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

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