New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typings do not indicate that data can be {} in some cases #6548
Comments
Does this issue persist with the latest release? During upgrading my own codebase I've noticed that |
There were already cases where data can be undefined before version 3.0. The graphql spec itself defines that data can be omitted if for example an error occured. To better indicate that, data was set to being optional in version 3.0 I think. This is how ApolloQueryResult currently looks like: type ApolloQueryResult<T> = {
data?: T;
errors?: ReadonlyArray<GraphQLError>;
loading: boolean;
networkStatus: NetworkStatus;
}; The issue I am describing above is that apollo client 3.0 will return |
This should no longer be an issue in |
I am still getting Nowadays I use const {data} = await client.query<Partial<Entries>, EntriesVariables>({
query: entriesQuery,
variables,
fetchPolicy: 'cache-only'
}); |
I'm not sure if the exact same issue, but I'm running into a similar issue with v3.8.8. I'm using cache.modify to update a list in the root query. The first time through, the existingRefs is an array. The second time through the existingRefs is an object with numeric keys. My code is identical to the example found at https://www.apollographql.com/docs/react/caching/cache-interaction/#example-adding-an-item-to-a-list. If this isn't the same issue, I'll create another issue with a reproducible repo. |
@jantoine1 that seems like another issue, please open a new issue with a code example :) |
I noticed that in some cases
client.query
andclient.watchQuery
will returndata: {}
client.query
with fetch-policycache-only
, but cache does not contain the requested queryclient.watchQuery
with fetch-policycache-only
orcache-and-network
, but cache does not contain the requested queryclient.watchQuery
and then removing the requested query from cache usingcache.modify
.When using the schema and query below,
client:codegen
will create the following types:schema.graphql
query.gql
query.ts
Using
client.query<entries, entriesVariables>
orclient.watchQuery<entries, entriesVariables>
the returned type indicates that the fielddata
has the typedata?: entries
, but not that it could also be{}
. This can lead to some unexpected behavior if the user is not aware that data will be{}
in the above mentioned cases.This could be fixed for example by changing the signature of
client.watchQuery
toAs for
client.query
, it could be overloaded in a way that it returnsPromise<ApolloQueryResult<T|{}>>
instead ofPromise<ApolloQueryResult<T>>
when fetch-policy is set tocache-only
.An alternative solution would be to leave the types unchanged and return
data: undefined
instead ofdata: {}
.Versions
The text was updated successfully, but these errors were encountered: