Having a config file fits well when we have a large amount of options to provide in order to generate some code. This can happen mostly in large scale projects where the GraphQL schema is pretty complex and we would like to generate a lot of different formats.
To generate code from a config file, you can simply create a
codegen.json file and run
$ gql-gen. The CLI will automatically detect the defined config file and will generate code accordingly. In addition, you can also define a path to your config file with the
--config options, like so:
$ gql-gen --config ./path/to/config.yml
Here's an example for a possible config file:
schema: http://localhost:3000/graphql documents: ./src/**/*.graphql overwrite: true generates: ./src/types.ts: plugins: - typescript-common - typescript-client
A more robust config file can be seen here.
Here are the supported options that you can define in the config file (see source code):
schema(required) - A URL to your GraphQL endpoint, a local path to
documents- Array of paths or glob patterns for files which export GraphQL documents using a
gqltag or a plain string; for example:
./src/**/*.graphql. You can also provide this options with a string instead of an array, in case you're dealing with a single document. You can read more about the supported formats here
generates(required) - A map where the key represents an output path for the generated code and the value represents a set of options which are relevant for that specific file. Below are the possible options that can be specified:
generates.plugins(required) - A list of plug-ins to use when generating the file. Templates are also considered as plug-ins and they can be specified in this section. A full list of supported plugins can be found here. You can also point to a custom plugin in a local file (see Custom Plugins section)
generates.documents- Same as root
documents, but applies only for the specific output file.
generates.schema- Same as root
schema, but applies only for the specific output file.
generates.config- Same as root
config, but applies only for the specific output file.
require- A path to a file which defines custom Node.JS
require()handlers for custom file extensions. This is essential if the code generator has to go through files which require other files in an unsupported format (by default). See more information.
mergeSchemaFiles- A name of a modules along with its exported merge function name. Use the following pattern:
moduleName#mergeFunctionExportedFromThisModule. This will be used to merge and build your GraphQL schema when you specify a glob to multiple
config- Options that we would like to provide to the specified plug-ins. The options may vary depends on what plug-ins you specified. Read the documentation of that specific plug-in for more information. You can read more about how to pass configuration to plugins here
overwrite- A flag to overwrite files in case they're already exist when generating code.
watch- A flag to watch for changes in the specified GraphQL schemas and re-generate code any that happens.
silent- A flag to not print errors in case they occur.