Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Apollo Client query fails with Cannot query field on type Query

ErrorCannot 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.

  • Applies to Apollo Client 3, React 19, GraphQL spec 2021
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 }
      }
    }
  2. 2Match what you see, then jump to its fix

    1. 1The error names a field and a type, such as xx on QueryThe field name does not exist on that typeFix
    2. 2The same query works in one environment and fails in anotherApollo Client points at a different server than the one you editedFix
    3. 3The 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

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 case

2Fix

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 key

Confirmed by spec.graphql.org

Did this fix it?

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 URL

Confirmed by apollographql.com

Did this fix it?

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 interface

2Fix

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 key

Confirmed by graphql.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.