Currently when using apollo-codegen
to generate TypeScript types for query definitions (ts-modern
target),
the generated types are called as the query name. So if you have a GetUserQuery
your basetype is called GetUserQuery
.
But in fact the type does not desribe the actual query, but the outcome, the result of the query, so it would be better
if the type would have been named like UserQueryResult
.
There is an issue in apollo-codegen where this issue is discussed: apollographql/apollo-tooling#315
But as there is currently no progress I wrote a little codemod (using jscodeshift)
that renames the types after they have been generated. You can find the transform.js
script below.
Please make sure that you have installed:
- jscodeshift
- [email protected] (the babylon version shipped with jscodeshift does not work with TypeScript correctly).
You can run the generator (in "dry" mode) like this:
yarn jscodeshift src --dry --extensions ts -t scripts/transform.js
It transforms all Query-files in all __generated__
folders.
So complete steps are:
- (optional) run
apollo-codegen introspect-schema . . .
to get the newest GraphQL Schema for your API - run
apollo-codegen generate . . . --target ts-modern
to generate the TypeScript types for your queries - run
yarn jscodeshift . . .
to modify the generated query types
Note: this works in my scenario, don't know if this is really a "general" solution. Please feel free to conact me with ideas etc