apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.73k stars 653 forks source link

Extend @typePolicy keyFields to accept non scalar types #5203

Open demoritas opened 1 year ago

demoritas commented 1 year ago

Use case

We where running into a case where an object has a objectId field which is only unique inside its parent. We can access the parent from the object so it would be neat to be able to define the keyFields using non scalar types

Example:

type Author implements Node {
  id: String!
}

type Book {
  bookId: String! // only valid in the scope of the author
  author: Author!
}

Describe the solution you'd like

have @typePolicy keyFields support non scalar types so we can define a scalar field in a non null non scalar field for the id:

extend type Book @typePolicy(keyFields: "bookNumber author.id")

this would add the following to ever query for Book

... on Book {
    bookId
    author {
        id
   }
}

and use this to build the cache key.

agrosner commented 10 months ago

👍 for it to resemble how servers define complex keys:

extend type SomeTypeWithComplexKeys @typePolicy(keyFields: "fieldA complexField { id }")