Apollo Client query fails with Cannot query field on type Query
Cannot query field "xx" on type "Query"Short answer
The server validated your query against its own schema and found a field that the type does not declare. Only fields declared on a type can be queried. So check the field name against the schema, make sure Apollo Client points at the right server, and use an inline fragment when the field lives on a concrete type of an interface.
Fix it fast
1Find out which cause you have
Ask the server which fields exist. The introspection meta-fields describe the schema, so list the fields of the Query type and compare them to the one in your error.
query { __type(name: "Query") { fields { name } } }2Match what you see, then jump to its fix
- 1
The error names a field and a type, such as xx on QueryThe field name does not exist on that typeFix - 2
The same query works in one environment and fails in anotherApollo Client points at a different server than the one you editedFix - 3
The type in the error is an interface, such as Character or Node, and the field exists on one of its implementationsThe field belongs to a concrete type of an interfaceFix
- 1
What does the error look like?
Cannot query field "xx" on type "Query"Cannot query field "primaryFunction" on type "Character"
Cause 1 of 3
The field name does not exist on that type
1Check
The GraphQL specification says only fields declared on the object type may validly be queried on it. A typo, a field that was renamed on the server or a field you assumed will fail validation.
# run the introspection query above and compare the names, including upper and lower case2Fix
Use a name from the field list, or ask the schema owner to add the field.
query {
products { id name }
}3Verify
Run the corrected query in your server's playground or in Apollo Client.
# the response has data and no errors keyConfirmed by spec.graphql.org
Cause 2 of 3
Apollo Client points at a different server than the one you edited
1Check
Apollo's getting started guide passes the GraphQL endpoint to ApolloClient as the uri. If that value is a stale, staging or demo address, your query is checked against that server's schema.
grep -rn "uri\|HttpLink" src/2Fix
Set uri to the endpoint of the server that has your schema, preferably from an environment variable.
const client = new ApolloClient({
uri: import.meta.env.VITE_GRAPHQL_URL,
cache: new InMemoryCache(),
});3Verify
Reload the app and check that the request goes to the intended URL.
# browser devtools > Network > the graphql request URLConfirmed by apollographql.com
Cause 3 of 3
The field belongs to a concrete type of an interface
1Check
GraphQL's query guide explains that in a direct selection you can only ask for fields on the interface itself; a field of a concrete type needs an inline fragment with a type condition.
# in introspection, look at possibleTypes of the interface2Fix
Wrap the field in a fragment on the type that has it.
query {
hero {
name
... on Droid { primaryFunction }
}
}3Verify
The query runs without the validation error.
# the response has data and no errors keyConfirmed by graphql.org
How do I stop it coming back?
- Generate types from the schema so a wrong field name fails in your editor, not at runtime.
- Keep the GraphQL endpoint in environment configuration, one per environment.
- Read the schema, not old queries, when the backend changes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix