Is your feature request related to a problem? Please describe.
I'm trying to use Apollo from a Node server. I use it on the frontend with a React app and I love it together with the generated types but it's a little more problematic to use in a frontend-framework-agnostic way because all of the Apollo plugins are specific to React/Angular/etc.
Describe the solution you'd like
The React Apollo plugin is capable of generating useGetFooQuery() functions that are strongly typed. It'd be nice if there was a plugin to generate similar functions for the base Apollo client. Something like...
export function executeGetFooQuery(
client: ApolloClient,
// TVariables should be the specific type associated with GetFooQuery (in this case, just {})
// Omit query to avoid an end-user trying to override the query
options: Omit<QueryOptions<TVariables>, "query">
) {
// TResult should be the specific type associated with GetFooQuery
return client.query<TResult>({...options, query: GetFooDocument});
}
The options args should probably be optional if there are no required variables and non-optional otherwise. :^)
Describe alternatives you've considered
Currently, I can just do something like
import {GetFooQuery} from "./generated.ts";
client.query<GetFooQuery>({ query: gql`query GetFoo { ... }` });
but it requires me manually specifying the type parameter.
Additional context
If I should open this issue somewhere else, let me know! Thanks for all the hard work.
Sounds like a valid use-case :)
We'll maybe add this in the future. Feel free to send a PR if you wish :)
I'd also be interested to see something like this for graphql-request, and may take a stab at a PR.
thanks @dotansimha ;-)
As graphql-request has already been implemented, are there any news on the apollo-client implementation? @dotansimha
@P4sca1 Sorry, couldn't find the time to add it. If someone has the time, it should be pretty simple (most of is is copy/paste of the graphql-request plugin).
Maybe we should abstract over concrete client implementation? After all it is all about passing document and variables and returning some typed result wrapped in promise. I prepared a draft: #2978
@akozhemiakin implemented it in: https://github.com/dotansimha/graphql-code-generator/pull/2978
I'll get it merged and tested very soon.
@akozhemiakin implemented it, and it's available in 1.12.2. We'll add documentation and examples for requesters soon :)
Most helpful comment
@akozhemiakin implemented it, and it's available in 1.12.2. We'll add documentation and examples for
requesterssoon :)