Search code examples
graphqlapolloapollo-clientapollo-cache-inmemorygraphql-schema

Graphql type with id property that can have different values for same id


I was wondering if an object type that has an id property has to have the same content given the same id. At the moment the same id can have different content.

The following query:

const query = gql`
  query products(
    $priceSelector: PriceSelectorInput!
  ) {
    productProjectionSearch(
      priceSelector: $priceSelector
    ) {
      total
      results {
        masterVariant {
          # If you do the following it will work
          # anythingButId: id
          id
          scopedPrice {
            country
          }
        }
      }
    }
  }
`;

If the PriceSelectorInput is {currency: "USD", country: "US"} then the result is:

{
  "productProjectionSearch": {
    "total": 2702,
    "results": [
      {
        "name": "Sweater Pinko white",
        "masterVariant": {
          "id": 1,
          "scopedPrice": {
            "country": "US",
            "__typename": "ScopedPrice"
          },
          "__typename": "ProductSearchVariant"
        },
        "__typename": "ProductProjection"
      }
    ],
    "__typename": "ProductProjectionSearchResult"
  }
}

If the PriceSelectorInput is {currency: "EUR", country: "DE"} then the result is:

{
  "productProjectionSearch": {
    "total": 2702,
    "results": [
      {
        "name": "Sweater Pinko white",
        "masterVariant": {
          "id": 1,
          "scopedPrice": {
            "country": "DE",
            "__typename": "ScopedPrice"
          },
          "__typename": "ProductSearchVariant"
        },
        "__typename": "ProductProjection"
      }
    ],
    "__typename": "ProductProjectionSearchResult"
  }
}

My question is that masterVariant of type ProductSearchVariant has id of 1 in both cases but different values for scopedPrice. This breaks apollo cache defaultDataIdFromObject function as demonstrated in this repo. My question is; is this a bug in apollo or would this be a violation of a graphql standard in the type definition of ProductSearchVariant?


Solution

  • TLDR

    No it does not break the spec. The spec forces absolutely nothing in regards caching.

    Literature for people that may be interested

    From the end of the overview section

    Because of these principles [... one] can quickly become productive without reading extensive documentation and with little or no formal training. To enable that experience, there must be those that build those servers and tools.

    The following formal specification serves as a reference for those builders. It describes the language and its grammar, the type system and the introspection system used to query it, and the execution and validation engines with the algorithms to power them. The goal of this specification is to provide a foundation and framework for an ecosystem of GraphQL tools, client libraries, and server implementations -- spanning both organizations and platforms -- that has yet to be built. We look forward to working with the community in order to do that.

    As we just saw the spec says nothing about caching or implementation details, that's left out to the community. The rest of the paper proceeds to give details on how the type-system, the language, requests and responses should be handled.

    Also note that the document does not mention which underlying protocol is being used (although commonly it's HTTP). You could effectively run GraphQL communication over a USB device or over infra-red light.

    We hosted an interesting talk at our tech conferences which you might find interesting. Here's a link:

    GraphQL Anywhere - Our Journey With GraphQL Mesh & Schema Stitching • Uri Goldshtein • GOTO 2021

    If we "Ctrl+F" ourselves to look for things as "Cache" or "ID" we can find the following section which I think would help get to a conclusion here:

    ID

    The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, it is not intended to be human‐readable. While it is often numeric, it should always serialize as a String.

    Result Coercion

    GraphQL is agnostic to ID format, and serializes to string to ensure consistency across many formats ID could represent, from small auto‐increment numbers, to large 128‐bit random numbers, to base64 encoded values, or string values of a format like GUID.

    GraphQL servers should coerce as appropriate given the ID formats they expect. When coercion is not possible they must raise a field error.

    Input Coercion

    When expected as an input type, any string (such as "4") or integer (such as 4) input value should be coerced to ID as appropriate for the ID formats a given GraphQL server expects. Any other input value, including float input values (such as 4.0), must raise a query error indicating an incorrect type.

    It mentions that such field it is commonly used as a cache key (and that's the default cache key for the Apollo collection of GraphQL implementations) but it doesn't tell us anything about "consistency of the returned data".

    Here's the link for the full specification document for GraphQL

    Warning! Opinionated - My take on ID's

    Of course all I am about to say has nothing to do with the GraphQL specification

    Sometimes an ID is not enough of a piece of information to decide whether to cache something. Let's think about user searches:

    If I have a FavouriteSearch entity that has an ID on my database and a field called textSearch. I'd commonly like to expose a property results: [Result!]! on my GraphQL specification referencing all the results that this specific text search yielded.

    These results are very likely to be different from the moment I make the search or five minutes later when I revisit my favourite search. (Thinking about a text-search on a platform such as TikTok where users may massively upload content).

    So based on this definition of the entity FavouriteSearch it makes sense that the caching behavior is rather unexpected.

    If we think of the problem from a different angle we might want a SearchResults entity which could have an ID and a timestamp and have a join-table where we reference all those posts that were related to the initial text-search and in that case it would make sense to return a consistent content for the property results on our GraphQL schema.

    Thing is that it depends on how we define our entities and it's ultimately not related to the GraphQL spec

    A solution for your problem

    You can specify how Apollo generates the key for later use as key on the cache as @Matt already pointed in the comments. You may want to tap into that and override that behavior for those entitites that have a __type equal to your masterVariant property type and return NO_KEY for all of them (or similar) in order to avoid caching from your ApolloClient on those specific fields.

    I hope this was helpful!