-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Query data and error are undefined #8063
Comments
I'm having the same bug of
|
I’ve also run into this situation when implementing defer for Apollo Client. It seems like if the server responds with data which doesn’t match the query, in that it’s missing fields, the result provided by the cache has /*** SCHEMA ***/
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLList,
} from 'graphql';
const PersonType = new GraphQLObjectType({
name: 'Person',
fields: {
id: {type: GraphQLID},
name: {type: GraphQLString},
},
});
const peopleData = [
{id: 1, name: 'John Smith'},
{id: 2, name: 'Sara Smith'},
{id: 3, name: 'Budd Deey'},
];
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
people: {
type: new GraphQLList(PersonType),
resolve: () => peopleData,
},
},
});
const schema = new GraphQLSchema({query: QueryType});
/*** LINK ***/
import {graphql, print} from "graphql";
import {ApolloLink, Observable} from "@apollo/client";
function delay(wait) {
return new Promise(resolve => setTimeout(resolve, wait));
}
const link = new ApolloLink(operation => {
return new Observable(async observer => {
const {query, operationName, variables} = operation;
await delay(300);
try {
const result = await graphql(
schema,
print(query),
null,
null,
variables,
operationName,
);
// Emulate producing a result which is missing a field from the server.
delete result.data.people[2].name;
observer.next(result);
observer.complete();
} catch (err) {
observer.error(err);
}
});
});
/*** APP ***/
import React from "react";
import {render} from "react-dom";
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
gql,
useQuery,
} from "@apollo/client";
import "./index.css";
const ALL_PEOPLE = gql`
query AllPeople {
people {
id
name
}
}
`;
function App() {
const {loading, data, error} = useQuery(ALL_PEOPLE);
if (loading) {
return <div>Loading...</div>;
} else if (error) {
return <div>Error: {error.toString()}</div>;
} else if (!data) {
return <div>What the heck</div>;
}
return (
<ul>
{data.people.map(person => (
<li key={person.id}>{person.name}</li>
))}
</ul>
);
}
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});
render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById("root"),
); |
@benjamn Do you know the original behavior for when a server responds with less data than the query expects? My sense is that this is a regression of some kind. |
I’m discussing this with the team, and one thing y’all should try is adding |
Just noticed this bug today. Can also confirm that setting a What's really bizarre is that I've only noticed this error recently. Yet, when I downgrade to
@brainkim I'm working on a new project with my company that involves a very simple query that reproduces this error consistently. I can say safely that the fields my mock server is returning are the exact same as the fields my client is expecting. Sadly, the code is private so I can't give you the repo. Basically I'm reproducing this error when I run 2 different queries that have the same cache key. I have a
called earlier, which returns an array of all users including one with ID
called later. The second |
I was able to reproduce this issue on a separate repository. See below @brainkim |
@diracs-delta This is a beautiful example. I’m dropping everything to investigate. |
@diracs-delta Ah I get it. It’s the same problem as I think everyone in this issue is having. In the mock graphql server you need to put the const handlers = [
graphql.query('getUsers', (_, res, ctx) => {
return res(ctx.data({ users: userStore }))
}),
graphql.query('getSpecificUser', (req, res, ctx) => {
const user = userStore.find(user => user.id === req.variables.id);
if (!user) {
return res(ctx.errors([{ message: `Cannot find user ${user}` }]));
}
return res(ctx.data({user}));
}),
]; |
@brainkim I believe you're right. The It would be nice to produce some kind of console warning or emit an error when this happens to prevent future confusion. I can take a further look at this later tonight. |
Hello, I'm experiencing this same issue, however I am not mocking anything. As @brainkim suggested above, adding Is there somewhere in the cache setup where this can be addressed? Or what long term solution could I implement that doesn't involve adding the |
If you are both the provider and consumer of the GraphQL API, this issue is occurring because the field structure of the query and the response do not match, which is usually due to the backend omitting a null field entirely instead of returning
Otherwise, if you are only the consumer of the GraphQL API, you're unfortunately out of other options until this issue is resolved. |
Where is the requirement to return "null" instead of "undefined" documented? Is that a graphql spec kind of thing? I hit this hard when I tried to change my Maybe type to |
@macrael Although I truthfully haven't read the spec top-to-bottom, I believe that to be the case. I think the provider of a GraphQL API should always explicitly return The reason codegen defines Since keeping the default definition of |
Same problem 🥲 |
Same issue happening at 3.6.1. |
3.5.10 works for me 🔥 In addition, my snapshot tests using
|
same problem here. fixed by downgrading to 3.2.5 |
downgrading to 3.2.5 works for me |
Tried a few suggestions here, I can confirm that both 3.2.5 and 3.5.10 are working for me and decided to stay with 3.5.10. I'm really curious also as to why no errors are being reported and we end up with a silent fail, this has been very time-consuming to debug. Maybe this suggests a regression, let us know if there is anything else we can do to test or provide further debug info. |
3.5.10 working for me. No joy with 3.6+ |
3.6.0 also failing for me, sticking to 3.5.10 |
3.3.7 was failing for me. Fixed by upgrading to 3.5.10. - "@apollo/client": "3.3.7",
+ "@apollo/client": "3.5.10", |
Why is this closed? |
Still encountering this problem. |
+1 |
it seems I was able to solve the problem! If your server responds with a status other than 200, then "data" becomes "undefined"
data (client) = undefined
data (client) = data{message: "not authorized"} I hope this helps someone, good luck! |
I'm a little shocked this is still a thing, it only worked on 3.3.7 for me, not even 3.5.10. |
getting undefined for data and error |
Happening on 3.3.1. |
Same issue for me on |
Running into this problem on First query gives undefined for data and error. Response of 200 with expected data can be seen in the network. |
I've tried every suggestion in this post and many other sources and most of the apollo client versions that people said worked and the problem is present 100% of the time. My schema and query are as simple as possible. The data shape matches perfectly. ** The correct data is clearly being returned during the fetch. ** I've tried everything possible. The client is getting good data back from the server. I tried the no-cache setting and the partial data setting and saw no improvement. |
@jimmygizmo can you provide a small runnable reproduction that shows this happening? |
Happening at 3.7.17, tried downgrading to 3.5.10 to test and because I'm using React 18.2 it wouldn't let me downgrade. Not sure what to do, any suggestions? |
Confirmed the same on version 3.80 and 3.8.1. Downgrading is also not an option for me. |
Upgrading to 3.9.11 fixed this for me. |
For anyone using Hasura, I solved this issue (in |
Intended outcome:
I'm using
useQuery
to query and render some data. Theloading
prop works as expected, the value istrue
and then changes tofalse
. When loading is false,data
anderror
are bothundefined
.I checked the network tab and the data is being received (I see the
data
prop)I also checked what's going on with the Apollo chrome extension and I can see the data
I also was able to verify the correct result from the BE using the
fetch
APIActual outcome:
data
anderror
props fromuseQuery
areundefined
.How to reproduce the issue:
Here's the component that uses
useQuery
andfetch
And this is the Apollo Client
Versions
System:
OS: macOS 11.2.3
Binaries:
Node: 14.8.0 - ~/n/bin/node
npm: 6.14.7 - ~/n/bin/npm
Browsers:
Chrome: 90.0.4430.72
Safari: 14.0.3
npmPackages:
@apollo/client: ^3.3.15 => 3.3.15
The text was updated successfully, but these errors were encountered: