loading is false, but no data is present (and no error)
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.
I can confirm this issue exists.


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]
}
})
}
)
Yeah, got the same error when trying to pass my company project to the last version, was previously in 1.4.16.
Is anyone looking into this? This is a serious bug that is killing quite a bit of my code, now.
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

@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!
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 Actually, the issue exists with the default setting, too.
Any workaround that doesn't require adding a check on data for all loading queries ?
@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.
I just tested with the 1.0.3 of apollo-client, and everything's work for me. Thanks a lot @jbaxleyiii.
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!
Confirmed fixed in 2.0.1.
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

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!

still have this issue with version '2.0.4' here 😢 . Sometimes it will work with safari but it constantly failed with chrome.
@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?
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.
What are those extra 2 renderers for?
@j4mesjung yes~
@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:
- loading: true, no data
- loading: false, data is available and good
- loading: false, no data
Hope this helps!
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 do you have any reproduce repo? I can't reproduce this in my branch: https://github.com/xcv58/react-apollo-error-template/tree/2591adeeb9401fb90dcb28ed84c44d53ed58285a