I am looking for a way to make the developer workflow more efficient while GraphQL.
Currently, using graphql-code-generator
to generate types from my GraphQL server on the frontend.
This is great, but this is only generating types. In order to generate methods for mutations, queries and subscriptions, I need to create a GraphQL document for each operation in my frontend project, for example:
file addPost.graphql
mutation addPost {
...
}
...
I find having to create an extra addPost.graphql
to generate the method a bit redundant as it is already declared on my GraphQL server.
Is there a plugin/configuration that will generate these methods that I can use in my project without having to manually create the additional GraphQL documents?
Here is my GraphQL generator yml file
# graphql-generator.yml
overwrite: true
schema: https://localhost:8088/query
documents: ./libs/**/*.graphql <----- generating these *.graphql files would be great!
generates:
libs/graphql/src/lib/generated/graphql.ts:
plugins:
- "typescript"
- "typescript-resolvers"
- "typescript-operations"
- "typescript-apollo-angular"
./graphql.schema.json:
plugins:
- "introspection"
One of the core concepts of GraphQL is the ability to allow components, or any other consumer, to choose the fields they need, instead of something else decide it for them (backend developer / server / code).
Also, generating operations based on GraphQL schema is tricky - because of the nature of the graph, you can't really generate it without a set of constraints (for example: what nesting level to reach? what to do with circular links? what do you in case of arguments? what to do if the schema changes? and much more)
GraphQL Code Generator isn't doing that because we believe that the developer consuming the GraphQL layer should decide on the fields.
In some other solutions we developed, we needed such tooling to generate selection sets, but with some pre-defined constraints. This comments sums it and provides a code example for generating those: https://github.com/dotansimha/graphql-code-generator/discussions/2349#discussioncomment-15754
If you wish, you can either generate those into a file and then feed it to codegen, or you can create a custom documents loader in your project to create it dynamically (https://graphql-code-generator.com/docs/getting-started/documents-field#custom-document-loader)