I’m working on updating a Nextjs project that uses Apollo GraphQL (@apollo/client@npm:^3.6.6") and am trying to transition it to use fragments. I have the following query that works fine:
query GetTeamCurrentWeek($teamId: ID!) {
team(id: $teamId, idType: DATABASE_ID) {
teamMeta {
currentWeek
}
}
}
If I change it to:
fragment teamCurrentWeek on RootQuery {
team(id: $teamId, idType: DATABASE_ID) {
teamMeta {
currentWeek
}
}
}
query GetTeamCurrentWeek($teamId: ID!) {
...teamCurrentWeek
}
useQuery returns data undefined.
If I open Apollo Devtools on the page, I can see the following query there:
fragment teamCurrentWeek on RootQuery {
team(id: $teamId, idType: DATABASE_ID) {
teamMeta {
currentWeek
__typename
}
__typename
}
__typename
}
query GetTeamCurrentWeek($teamId: ID!) {
...teamCurrentWeek
}
and if I run the query in the explorer, it runs and returns:
{
"data": {
"team": {
"teamMeta": {
"currentWeek": "1",
"__typename": "TeamMeta"
},
"__typename": "Team"
},
"__typename": "RootQuery"
}
}
It seems so strange that the query is displayed in the Apollo Devtools and runs there fine, but not from within the app.
I’m querying against Wordpress using WPGraphQL , and if I run the fragment query in the wordpress graphiql-ide, the query works fine and returns:
{
"data": {
"team": {
"teamMeta": {
"currentWeek": "1"
}
}
}
So it looks like there isn’t an issue with the fragment query itself, it something about the context that the query is being executed in. However, the context seems fine, because if I revert back to the non-fragment query, everything works as expected.
I’m utterly baffled and would really appreciate any help debugging this.
Just as an aside, I should also say that debugging this has been made more difficult by the fact that Apollo Devtools in Chrome is really unstable. It appears maybe 50% of the time, sometimes disappears on a reload and only reappears when I open a fresh tab - and this is with the latest versions of Apollo Devtools (4.8.1), Apollo Client (3.6.6) and Chrome (122).