简体   繁体   English

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

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

In relay/graphql, how to express a query where the response can be empty. 在relay / graphql中,如何表达查询可以为空的查询。 I have a dilemma now that, I can't respond with an empty/null response because id field is required by relay (and possibly other non nullable fields in graphql schema), and I can't send error as it stops my component being rendered. 我现在有一个难题,我无法用空/空响应来响应,因为中继需要id字段(以及graphql模式中的其他非空字段),并且由于它停止我的组件,所以我无法发送错误呈现。

For example, say I am modelling a relationship hierarchy and has a query like 例如,假设我正在建模一个关系层次结构,并且有一个类似的查询

getSpouse(partnerID: string): Person

and this may be empty for some. 对于某些人来说这可能是空的。 So, I do either respond with a null Person object (which I think is not quite possible with relay as Person might have non nullable fields including a globalID), or send an error. 因此,我确实以一个空的Person对象作为响应(我认为中继不太可能,因为Person可能具有包括globalID的不可为空的字段),或者发送错误。 It is ok to send error, but I am not sure how to catch this error and continue with rendering relay container. 可以发送错误,但是我不确定如何捕获此错误并继续渲染中继容器。 I know hot to get errors in case of mutation but query is processed by relay container and couldn't see an interface to get error and continue loading the component. 我知道在发生突变的情况下很容易出错,但是查询是由中继容器处理的,无法看到获取错误并继续加载组件的接口。

Is there a way to catch the query error at the Relay container or pass it down to my component ? 有没有办法在Relay容器中捕获查询错误或将其传递给我的组件?

@josephsavona of the relay core team commented one way to do this. 中继核心团队的@josephsavona评论了一种方法。 at https://github.com/facebook/relay/issues/487#issuecomment-232102389 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. 一种解决方法是使用自定义网络层,如果有任何数据(无论是否有错误),它将解析RelayQueryRequest,并且仅在没有数据和错误时拒绝请求。

Edit: to elaborate based on the comment. 编辑:根据评论进行详细说明。

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

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

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