How can I tell my Query resolver that the type it is returning does not need to match my GraphQL schema type since the data is going to be formatted by my type's custom resolvers?
I am using codegen with GraphQL to generate my schema and type interfaces.
I rely on my resolvers to transform the data into the required shape to match my GraphQL schema but because of this, my Query resolver thinks it's returning the wrong value.
For example, I have simple schema like so:
type Basket {
items: [String!]
}
type Query {
getBasket(clientId: String!): Basket
}
So when I get my basket items back, I should get an array of strings.
const Query: Resolvers<Context>["Query"] = {
async getBasket(_, args, ctx) {
return ctx.models.Basket.get(args.clientId); // returns {items: [{name: "Item Name"}]}
}
};
My models.Basket.get
here, returns an array of objects (because that's how they come from the datasource which I don't have access to to change).
I then have a Basket resolver which transforms the basket.items
into an array to match my GraphQL schema response:
const Basket: BasketResolvers<Context> = {
items(root) {
return root?.items?.map(item => {
return item.name;
}); // returns ["Item Name"]
}
};
The issue I run into is that my Query thinks it's returning the wrong type because it doesn't match the response for Resolvers<Context>["Query"]
.
Is there a way I can tell codegen to not worry about the return type here and to use the custom resolvers instead?
Some solutions I have looked into are to handle the data transformation in models.Basket.get
but I would rather use resolvers for this kind of thing if possible. I could also override the Resolvers<Context>["Query"]
type but extending it and omitting it becomes very convoluted very quickly and is not really scalable as I add more queries and mutations.
Is there a solution to handle this in codegens config?