Error - Invalid Key: The GraphQL Query at the Field at Route:X

2 replies
Last updated: Jun 23, 2021
I’m using a graphql query to get pages, which include
slug
type objects - but I am seeing this warning:

Invalid key: The GraphQL query at the field at `Route:86842a74-fd68-4fcc-a9ad-2071a1b53714.slug` has a selection set, but no key could be generated for the data at this field.
You have to request `id` or `_id` fields for all selection sets or create a custom `keys` config for `Slug`.
Entities without keys will be embedded directly on the parent entity. If this is intentional, create a `keys` config for `Slug` that always returns null.
Can anyone help me with how to fix that? The
slug
model doesn’t seem to have an
_id
field
AI Update

This warning is coming from urql's graphcache (a GraphQL caching library), not from Sanity itself. The issue is that graphcache needs a way to uniquely identify entities in its cache, and it's seeing your slug field as an object (selection set) without a unique identifier.

In Sanity, the slug type is indeed an object with a current property (like { current: "my-page" }), not a simple string. This is why GraphQL sees it as a selection set. And you're correct - the slug object itself doesn't have an _id field because it's not a document, it's just a field type.

Here are your solutions:

Add a custom keys config to tell graphcache that Slug objects shouldn't be cached separately:

import { cacheExchange } from '@urql/exchange-graphcache';

const cache = cacheExchange({
  keys: {
    Slug: () => null, // Tell graphcache not to cache Slug as a separate entity
  }
});

This tells graphcache to embed the slug data directly on the parent entity (your Route document), which is exactly what you want since slug is just a field, not a standalone entity.

Solution 2: Request _id on the parent Route document

Make sure you're requesting _id on your main Route document:

query {
  allRoute {
    _id
    slug {
      current
    }
  }
}

This ensures the parent entity can be properly cached, though you'll still want Solution 1 for the Slug object itself.

Solution 3: Configure keys for all Sanity object types

If you have other Sanity object types (not documents) causing similar warnings, you can configure them all at once:

const cache = cacheExchange({
  keys: {
    Slug: () => null,
    Geopoint: () => null,
    Block: () => null,
    Span: () => null,
    // Add any other object types you use
  }
});

The first solution is what you need - Sanity's slug type is an embedded object, not a keyable entity, so telling urql to treat it as such resolves the warning correctly.

can you post the query?
Actually
user X
I think it’s fine, the
_key
is null on all slugs but they have a
current
value which is basically unique, so I think I can just use that as the custom key for gql caching

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?