Skip to content

Allow for type to be omitted when calculating Cache keys #102

@x-sheep

Description

@x-sheep

Use case

Given an endpoint that returns an interface type, it's currently not possible to set a fieldPolicy that will resolve the various implementation types.

interface Node @typePolicy(keyFields: "id") {
    id: ID!
}

type Actual implements Node {
    id: ID!
    name: String
}

type Query @fieldPolicy(forField: "node", keyArgs: "id") {
    node(id: ID!): Node # Cache will never find an Actual object before sending a request
}

I'm trying to use the node field to get a variety of types that have most fields in common. However, since those types are cached using their full type name, it isn't possible to retrieve the object from cache using the node field because only the Node interface is known.

Describe the solution you'd like

Inspired by the rejected RFC apollographql/apollo-ios-dev#549, I want to suggest @typePolicy and @fieldPolicy gain a new field named keyScope, with one of the following possible values:

  1. TYPE: The identifier is unique for the type, e.g. an auto-incrementing column from a database table.
  2. SERVICE: The identifier is unique across the current GraphQL Service, e.g. a type name combined with an auto-increnting column.
  3. UNIVERSAL: The identifier is unique across all GraphQL services, e.g. UUIDs generated through RFC 9562. Exact serialization details are unspecified, as long as all UUID information is present. Appending more information is allowed.

TYPE is the default. If the scope is not TYPE, cache keys should not contain the type name but only use the actual id as a cache key (possibly with a constant prefix).

The GraphQL best practice known as Global Object Identification requires that all ids are unique across the service. This is also a required part of the Relay spec.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions