Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

If initial Query errors, data is undefined instead of empty object #1977

Closed
TLadd opened this issue May 12, 2018 · 2 comments · Fixed by #1983
Closed

If initial Query errors, data is undefined instead of empty object #1977

TLadd opened this issue May 12, 2018 · 2 comments · Fixed by #1983
Labels
good first issue Issues that are suitable for first-time contributors.

Comments

@TLadd
Copy link
Contributor

TLadd commented May 12, 2018

Intended outcome:
The data prop passed to the Query component is never undefined. That's my understanding anyways, from reading https://github.com/apollographql/react-apollo/blob/master/src/Query.tsx#L89

Actual outcome:
If your first request for a Query errors, data ends up being null. It ends up hitting this line, https://github.com/apollographql/react-apollo/blob/master/src/Query.tsx#L378, which overwrites the default-created empty object and replaces it with (this.queryObservable!.getLastResult() || {}).data,, which will be undefined if the query doesn't have a result yet

How to reproduce the issue:
On any react-apollo application with a Query, force an error to occur and log the data passed to children. I personally encountered this when I had accidentally not had my graphql server running. As a result, the first Query request failed due to a network error, and I got an exception because I was assuming data would be an empty object and not undefined.

Version

  • apollo-client@2.3.0
  • react-apollo@2.1.4
@ghost ghost added the good first issue Issues that are suitable for first-time contributors. label May 12, 2018
@Wizyma
Copy link

Wizyma commented May 31, 2018

As a result, the first Query request failed due to a network error, and I got an exception because I was assuming data would be an empty object and not undefined.

@TLadd you are not the only one !
I'll be glad to look into it !

@alamothe
Copy link

The typings say data is either TData or undefined - never empty object:

data: TData | undefined;

So which one is it?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants