bsr bsr - 26 days ago 7
Javascript Question

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

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. 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 ?

bsr bsr
Answer

@josephsavona of the relay core team commented one way to do this. at 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.

Edit: to elaborate based on the comment.

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