If you with to write a custom template, you can either use one of the following options:
To write you own template package using Handlebars, you can use codegen-templates-scripts
.
This code generator also supports a custom output generators.
You can either do it by writing your own JavaScript file that exports the processing function, or put it inside NPM package.
To start writing your custom processor, create a JavaScript (or other language, but compile it or use --require
flag) under you project.
Your file must export a function, and need to return FileOutput[]
(or a Promise<FileOutput[]>
):
interface FileOutput {
filename: string;
content: string;
}
Your function need to match the following signature:
(templateContext: SchemaTemplateContext, mergedDocuments: Document, settings: any) => FileOutput[] | Promise<FileOutput[]>;
templateContext
is the GraphQL Codegen context object - which is yourGraphQLSchema
in an easy-to-use structure (seetypes.ts
file for more info)mergedDocuments
is all the GraphQL documents (query/mutation/subscription/fragment) that the codegen could findsettings
is an object of settings - these are matching to the cli options.
This is an example for a custom function written in pure JS:
module.exports = function(context, documents, settings) {
return [{ filename: 'a.ts', content: '1' }];
};
You can also integrate the GraphQL Code Generator into your project's code and generate multiple files using templates that are part of your project.
You can read more about it here.