Batching type resolution #1869
Replies: 3 comments 2 replies
-
Hello, thank you for reaching out, as you well mentioned, we decided to refactor the federated type resolution to match with other federation implementations, you can refer to the federation section of the documentation to check how to use DataLoaders, the only constrain is that you will have to use the FederatedTypePromiseResolver, |
Beta Was this translation helpful? Give feedback.
-
Hey @samuelAndalon, thanks for the response. I'm not sure I fully understood. How would I use |
Beta Was this translation helpful? Give feedback.
-
// This service does not own the "Product" type but is extending it with new fields
@KeyDirective(fields = FieldSet("id"))
@ExtendsDirective
class Product(@ExternalDirective val id: String) {
fun newField(): String = getNewFieldByProductId(id)
}
// This is how the "Product" class is created from the "_entities" query using promise resolver
class ProductResolver : FederatedTypePromiseResolver<Product> {
override val typeName: String = "Product"
override fun resolve(
environment: DataFetchingEnvironment,
representation: Map<String, Any>
): CompletableFuture<Product?> {
val id = representation["id"]?.toString()
// use dataloader to resolve Product by id
return environment.getDataLoader<String, Product?>("ProductDataLoader").load(id)
}
}
@Service
class ProductDataLoader : KotlinDataLoader<ID, User> {
override val dataLoaderName = "ProductDataLoader"
override fun getDataLoader(graphQLContext: GraphQLContext) =
DataLoaderFactory.newDataLoader<ID, User> { ids ->
// call your DB here
}
}
|
Beta Was this translation helpful? Give feedback.
-
With #1514 the interface for type resolvers has changed to pass a single
representation
instead of multiplerepresentations
. This was done to be more streamlined with other subgraph libraries which makes sense.However, it comes with the disadvantage that type resolution can not be batched anymore. In our case, we would do a batch load from DynamoDB when receiving
representations
as that is more efficient than running a query per requested type. With the current interface this is not possible.What would be the way to reproduce such a logic? I have asked on Slack and a data loader was suggested. From my understanding that does not mean the data loader will automatically be integrated with the type resolver, so I'm guessing some other gluing logic is needed.
Maybe some documentation or helpers in
graphql-kotlin
could be helpful to make the migration to v7 a little easier?Beta Was this translation helpful? Give feedback.
All reactions