Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate GraphQL operations from GraphQL schema

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"

like image 404
Dave Avatar asked Aug 16 '20 11:08

Dave


People also ask

How do I use a GraphQL schema?

In case you just want to download the GraphQL schema, use the following approach: The easiest way to get a GraphQL schema is using the CLI tool get-graphql-schema. There are two ways to get your schema. 1) GraphQL IDL format or 2) JSON introspection query format.

What is GraphQL code generator?

GraphQL Code Generator is a tool that generates code out of your GraphQL schema. Whether you are developing a frontend or backend, you can utilize GraphQL Code Generator to generate output from your GraphQL Schema and GraphQL Documents (query/mutation/subscription/fragment).


1 Answers

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)

like image 103
Dotan Simha Avatar answered Sep 27 '22 16:09

Dotan Simha