relay/ graphql:可为空的响应或捕获查询错误的方法

relay/ graphql: nullable response or a way to catch query error

在 relay/graphql 中,如何表达响应可以为空的查询。我现在进退两难,我无法用 empty/null 响应来响应,因为中继需要 id 字段(以及 graphql 模式中可能的其他不可空字段),而且我无法发送错误因为它停止渲染我的组件。

例如,假设我正在为一个关系层次结构建模并且有一个类似

的查询

getSpouse(partnerID: string): Person

这对某些人来说可能是空的。因此,我要么用一个空的 Person 对象进行响应(我认为这不太可能通过中继实现,因为 Person 可能具有不可为 null 的字段,包括 globalID),或者发送一个错误。可以发送错误,但我不确定如何捕获此错误并继续渲染中继容器。我知道在发生突变的情况下很容易出错,但查询是由中继容器处理的,看不到获取错误并继续加载组件的接口。

有没有办法在中继容器中捕获查询错误或将其传递给我的组件?

中继核心团队的@josephsavona 评论了一种方法来做到这一点。在 https://github.com/facebook/relay/issues/487#issuecomment-232102389

One workaround is to use a custom network layer that resolves the RelayQueryRequest if there is any data (regardless of errors), and only rejects the request if there is no data and errors.

编辑:根据评论进行阐述。

import { DefaultNetworkLayer } from 'react-relay';
export default class RelayNetworkLayer extends DefaultNetworkLayer {
  // override whichever methods (like sendMutation, sendQueries)
}