react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

loading is false, but no data is present (and no error)

Open adamdonahue opened this issue 9 years ago • 132 comments

Just upgraded to Apollo 2.0, using react-apollo 2.0.0 -- it now appears that the graphql props option for a query is passed a data argument that can have loading set to false but no data.

That is, given a query, say:

query X($id: ID!) {
  myItem(id: $id) {
    id
    field
  }
}

and a graphql call:

graphql(query, {
  props({ data: { loading, error, myItem } }) {
    console.log('loading =', loading);
    console.log('error =', error);
    console.log('myItem =', myItem);
    return { ... }
 }
})(MyComponent)

We're seeing the output:

loading = true
error = undefined
myItem = undefined
loading = false
error = undefined
myItem = undefined
loading = false
error = undefined
myItem = {<data>}

That is, there is an interim state between loading having been set to false but myItem yet available on the data argument.

Is this expected, as it appears to differ from the behavior of Apollo 1.0. And if so, what is the reliable way to check that data was loaded and is ready for use in the component?

We're now using (loading || !myItem) to check whether something is loading.

adamdonahue avatar Nov 01 '17 18:11 adamdonahue

I can confirm this issue exists.

loading false

no data

Offending code:

 graphql(
    gql`
      query localise($locale: LocaleType!, $resourceIds: [String]) {
        l10nSlugs(locale: $locale, resourceIds: $resourceIds) {
          id
          resourceId
          slug
        }
      }
    `,
    {
      name,
      options: ({ locale }) => ({
        variables: {
          locale,
          resourceIds
        },
      }),
      props: props => ({
        l10n: {
          /**
           * Whether or not the graphql resource is loading.
           */
          loading: props[name].loading,
          /**
           * Conveniently offer the locale.
           */
          locale: props.ownProps.locale,
          /**
           * And the BCP47 normalise version.
           */
          bcp47Locale: normaliseBCP47Tag(props.ownProps.locale),
          /**
           * Localise a slug with insertions.
           * @param {string} resourceId Resource ID of the slug to localise.
           * @param {string} slug Default slug in case loading.
           * @param {Array<string> | Object: []} insertions Dynamic content for the slugs.
           * @returns {string} Localised slug.
           */
          localise: (resourceId: string, slug: string, insertions: [string] | Object = []): string => {
            if (!props[name].loading && !props[name].error) {
                           ^^^^^^^^^^ Checked here...
              for (let s of props[name].l10nSlugs) {
                                        ^^^^^^^^^^^ BAM!
                if (s.resourceId === resourceId) {
                  return insert(s.slug, insertions)
                }
              }
            }
            return insert(slug, insertions)
          },
          /**
           * Locale aware ordinals (e.g. 1st, 2番目)
           * @param {Number} ord The numerical ord to convert.
           * @returns {string} The locale-specific ordinal
           */
          ord2str: (ord: Number): string => props.ownProps.l10nShared(`${ord}ord`),
          /**
           * Get whether or not the current locale arranges the name Last, First.
           * @returns {boolean} True if the locale requires a Last, First name arrangement.
           */
          nameLastFirst: (): boolean => NameLastFirstLocales.includes(props.ownProps.locale),

          /**
           * Gets teh minimum number of characters allowed in the locale's name part.
           * @returns {number} The minimum number of characters in the current locale.
           */
          minCharsInNamePart: (): number => MinCharsInNamePart[props.ownProps.locale]
        }
      })
    }
  )

Panoplos avatar Nov 09 '17 02:11 Panoplos

Yeah, got the same error when trying to pass my company project to the last version, was previously in 1.4.16.

malexandre avatar Nov 13 '17 10:11 malexandre

Is anyone looking into this? This is a serious bug that is killing quite a bit of my code, now.

Panoplos avatar Nov 13 '17 10:11 Panoplos

Can confirm this happens now after i updated today to [email protected] and [email protected]

UPD: some investigation/debugging led to this line in apollo-client package, giving null in the result,

https://github.com/apollographql/apollo-client/blob/f172ee6b37ab66d627b317d9b9f6ac1b8a739e00/packages/apollo-client/src/core/QueryManager.ts#L418

screen shot 2017-11-13 at 14 51 46 screen shot 2017-11-13 at 14 51 25

javascriptlove avatar Nov 13 '17 12:11 javascriptlove

@adamdonahue I'll take a look at this! Still working through issues after launch + took a week off last week! Thanks all for the issue report!

jbaxleyiii avatar Nov 13 '17 16:11 jbaxleyiii

Btw, i've resolved that on my side by setting fetchPolicy to cache-and-network, no more errors after that. So seems like the error is there only with network-only

javascriptlove avatar Nov 14 '17 08:11 javascriptlove

@javascriptlove Actually, the issue exists with the default setting, too.

Panoplos avatar Nov 14 '17 11:11 Panoplos

Any workaround that doesn't require adding a check on data for all loading queries ?

moimael avatar Nov 14 '17 15:11 moimael

@adamdonahue Can you please provide a stripped down reproduction for us to test? I've tried to reproduce with our test app Githunt, but haven't been able to.

peggyrayzis avatar Nov 15 '17 19:11 peggyrayzis

I just tested with the 1.0.3 of apollo-client, and everything's work for me. Thanks a lot @jbaxleyiii.

malexandre avatar Nov 16 '17 13:11 malexandre

This has been confirmed as fixed with 1.0.3 in apollo-client! Thank you all for the reporting and helping to diagnose and fix this one!

jbaxleyiii avatar Nov 16 '17 14:11 jbaxleyiii

Confirmed fixed in 2.0.1.

Panoplos avatar Nov 17 '17 05:11 Panoplos

EDIT: Fixed on my end by just adding an id to every query level. However, still unsure why some id's returned the data but some did not...might have bad data on my end. Will continue to debug.

hi @jbaxleyiii, @peggyrayzis i am using graphcool service to host my data and all my queries work in their playground when I pass in the given ids. However, on the client side, some queries will correctly return the data and some will not return any data even if loading: false. I am on apollo-client 2.0.4 screen shot 2017-12-15 at 11 24 19 am

after some digging around, the data is coming in correctly into newData.results. However, isMissing resolves to true from this line isMissing = !newData.complete ? !newData.complete : false; because newData.complete comes in as false. This then ultimately resolves into this line that then takes the data from lastResult which is undefined and then resultFromStore.data returns undefined. Is there something wrong in my query that makes complete resolve to false? Not exactly sure what is happening here and would like some input if possible. Thanks!

debug1

j4mesjung avatar Dec 13 '17 03:12 j4mesjung

still have this issue with version '2.0.4' here 😢 . Sometimes it will work with safari but it constantly failed with chrome.

crokobit avatar Dec 19 '17 07:12 crokobit

@crokobit what do you mean by working in safari and failing in chrome? Are you talking about the data.loading: false but not seeing the expected data?

j4mesjung avatar Dec 19 '17 21:12 j4mesjung

I'm on 2.0.4 and the render function on a component is called 3 times, 2 of which have loading = false and data = null and the last one has the data.

screen shot 2017-12-20 at 03 37 30

What are those extra 2 renderers for?

shoooe avatar Dec 20 '17 02:12 shoooe

@j4mesjung yes~

crokobit avatar Dec 20 '17 03:12 crokobit

@jbaxleyiii @peggyrayzis This issue still occurs for me.

I am using react native and

  • apollo-cache-inmemory: "^1.1.4"
  • apollo-client: "^2.0.4"
  • apollo-link-http: "^1.3.2"
  • react-apollo: "^2.0.4"

Interestingly enough, the issue only arises when I introduce 'id' as a field in my query. If I do not specify 'id' in my query, then everything works as expected.

Render flow goes like this:

  1. loading: true, no data
  2. loading: false, data is available and good
  3. loading: false, no data

Hope this helps!

ssahni avatar Dec 20 '17 04:12 ssahni

I'm seeing this too. If it helps, I know the data is already in the store and the request returns the correct data. It seems to happen when I render multiple components with queries at the same time?

  • "apollo-client": "2.1.0"
  • "react-apollo": "2.0.4"

denkristoffer avatar Dec 28 '17 10:12 denkristoffer

@denkristoffer do you have any reproduce repo? I can't reproduce this in my branch: https://github.com/xcv58/react-apollo-error-template/tree/2591adeeb9401fb90dcb28ed84c44d53ed58285a

xcv58 avatar Dec 29 '17 13:12 xcv58