0

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 ?

1
  • How did you solve it? Commented Nov 6, 2016 at 20:20

1 Answer 1

3

@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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you elaborate how you extended the network layer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.