or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-graphql-codegen--add

GraphQL Code Generator plugin for adding custom content to generated output files at configurable positions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@graphql-codegen/add@5.0.x

To install, run

npx @tessl/cli install tessl/npm-graphql-codegen--add@5.0.0

index.mddocs/

GraphQL Codegen Add Plugin

The 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.

Package Information

  • Package Name: @graphql-codegen/add
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @graphql-codegen/add

Core Imports

import { 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 function

Type-only import:

import type { AddPluginConfig } from "@graphql-codegen/add";

Basic Usage

// 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;

Capabilities

Plugin Function

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 Interface

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[];
}

Valid Placement Values

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']

Configuration Examples

Single String Content

// Add a single line at the top of generated files
{
  add: {
    content: '/* This file is auto-generated */',
    placement: 'prepend'
  }
}

Multiple Lines Content

// Add multiple lines using array
{
  add: {
    content: [
      '/* eslint-disable */',
      '// @ts-nocheck',
      ''
    ],
    placement: 'prepend'
  }
}

Content at Different Positions

// 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'
  }
}

Error Handling

The plugin validates configuration and throws errors for invalid settings:

  • Invalid placement: Throws error if placement is not one of 'prepend', 'content', or 'append'
  • Missing content: Throws error if 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!

Default Export

The package also exports a default object containing the plugin:

/**
 * Default export containing the plugin function
 */
export default {
  plugin: PluginFunction<AddPluginConfig>;
};

Types

External Dependencies

// 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
}

Plugin Output Structure

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']
}