GraphQL Code Generator plugin for adding custom content to generated output files at configurable positions
npx @tessl/cli install tessl/npm-graphql-codegen--add@5.0.0The GraphQL Codegen Add plugin allows developers to inject custom content into generated GraphQL code files at configurable positions (prepend, content, or append). This plugin is ideal for adding custom headers, footers, imports, license notices, or any other static content to generated files.
npm install @graphql-codegen/addimport { plugin, AddPluginConfig, VALID_PLACEMENTS } from "@graphql-codegen/add";For CommonJS:
const { plugin } = require("@graphql-codegen/add");Default import (exports plugin object):
import addPlugin from "@graphql-codegen/add";
// addPlugin.plugin is the plugin functionType-only import:
import type { AddPluginConfig } from "@graphql-codegen/add";// codegen.ts configuration
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './schema.graphql',
generates: {
'./src/generated/types.ts': {
plugins: [
'typescript',
{
add: {
content: '/* eslint-disable */',
placement: 'prepend'
}
}
]
}
}
};
export default config;The main plugin function that processes GraphQL schema and documents to generate custom content output.
/**
* Main plugin function for adding custom content to generated files
* @param schema - GraphQL schema object
* @param documents - Array of GraphQL document files
* @param config - Plugin configuration object
* @returns Promise resolving to plugin output with content placement
*/
export const plugin: PluginFunction<AddPluginConfig>;
type PluginFunction<TConfig> = (
schema: GraphQLSchema,
documents: Types.DocumentFile[],
config: TConfig
) => Promise<Types.PluginOutput>;Configuration options for controlling content addition behavior.
/**
* Configuration interface for the Add plugin
*/
export interface AddPluginConfig {
/**
* Position where content should be added in the generated file
* @default "prepend"
*/
placement?: 'prepend' | 'content' | 'append';
/**
* The actual content to add - can be a single string or array of strings.
* You can also specify a path to a local file and the content will be loaded by codegen.
* @required
*/
content: string | string[];
}Array of valid placement options for configuration validation.
/**
* Array of valid placement values for the plugin configuration
*/
export const VALID_PLACEMENTS: AddPluginConfig['placement'][];
// Value: ['prepend', 'content', 'append']// Add a single line at the top of generated files
{
add: {
content: '/* This file is auto-generated */',
placement: 'prepend'
}
}// Add multiple lines using array
{
add: {
content: [
'/* eslint-disable */',
'// @ts-nocheck',
''
],
placement: 'prepend'
}
}// Prepend content (default)
{
add: {
content: 'import "./custom-types";'
}
}
// Append content at the end
{
add: {
content: 'export * from "./additional-exports";',
placement: 'append'
}
}
// Insert content in the middle
{
add: {
content: '// Custom content inserted here',
placement: 'content'
}
}The plugin validates configuration and throws errors for invalid settings:
placement is not one of 'prepend', 'content', or 'append'content field is not provided in configuration// Error examples:
// Error: Configuration provided for 'add' plugin is invalid: value of 'placement' field is not valid (valid values are: prepend, content, append)
// Error: Configuration provided for 'add' plugin is invalid: "content" is missing!The package also exports a default object containing the plugin:
/**
* Default export containing the plugin function
*/
export default {
plugin: PluginFunction<AddPluginConfig>;
};// From '@graphql-codegen/plugin-helpers'
interface Types {
DocumentFile: {
document: DocumentNode;
location: string;
rawSDL?: string;
};
PluginOutput: {
content?: string | string[] | null;
prepend?: string[];
append?: string[];
};
}
// From 'graphql'
class GraphQLSchema {
// GraphQL schema representation
}The plugin returns a Types.PluginOutput object with the specified placement:
// Example output for 'prepend' placement:
{
content: null,
prepend: ['/* Custom content */', '// Another line']
}
// Example output for 'append' placement:
{
content: null,
append: ['export default generated;']
}
// Example output for 'content' placement:
// Note: This placement overrides the content property directly
{
content: ['// Inserted content']
}