Key Field(s) '[id]' are not queried when introducing declarative cache id for a type

New Apollo 3.6.2 project. When I introduce a declarative cache id for my SnowZone type one of the queries using this type produces an error at compile time:

query snowZonesByRouteId($filter: SnowRouteFilter) {
  snowRoutes(filter: $filter) {
    snowAreas {
      ... on SnowRegion {
        zones {
          id
        }
      }
    }
  }
}

Adding to extra.graphqls:

extend type SnowZone @typePolicy(keyFields: "id")

Produces:

Key Field(s) '[id]' are not queried on SnowZone at Operation(snowZonesByRouteId).snowRoutes.snowAreas

The abreviated type:

type SnowZone implements SnowArea & Place & Thing & Node {
  """The region that the zone is a part of."""
  region: SnowRegion

  """Uniquely identifies the node in the graph."""
  id: ID!

How do I fix this error?

Hi :wave:

That’s unexpected. Looks like you are querying “id” :thinking:. I’ll try to reproduce quickly. Can you share your schema by any chance? That could speed things up.

I think the problem lies at snowAreas. Since SnowArea is an interface, it might be resolved to SnowZone and therefore need an id, even if SnowArea doesn’t contain one. Can you try the following?

query snowZonesByRouteId($filter: SnowRouteFilter) {
  snowRoutes(filter: $filter) {
    snowAreas {
      # add this
      ... on Node {
        id
      }
      ... on SnowRegion {
        zones {
          id
        }
      }
    }
  }
}

That worked - thanks Martin!

1 Like