The codegen has also a complete programmatic API. You can use it if you need to customize the execution flow, or if you are writing a tool that uses the codegen.
In order to use the programmatic API, start by importing
Then, create a configuration object (complete signature):
schemafield be a valid
GraphQLSchemaobject. If you need to load your GraphQL schema from a external source (file, url), you can use
Notice that a plugin name key in
plugins must match to identify a plugin and its configuration.
You need to import the plugin in your favorite way, you can also use
await importto lazy load it.
Then, provide the config object to
We are using this API in the live demo in GraphQL Code Generator website. The code is here.
Loading schema and documents
You can use one of the tools from
@graphql-tools for file loading, schema merging, transformations and more.
Using the CLI instead of
If you wish to have the benefits that
cli package has (like loading schema and document files, parsing endpoints and more), you can use
@graphql-codegen/cli directly with Node.JS:
The return value should be of type
This usage will not work in a browser environment, because the
cli package depends on NodeJS internals and the file system.