Python Operations

Document-driven GraphQL Code Generator plugin that emits Python operation types (per-operation result + variables). This plugin is based on the `python` plugin, but it generates operations instead of schema types.

Version
0.1.0
Weekly downloads
291
License
MIT
Updated
Nov 11, 2026

Installation

          npm i -D @graphql-codegen/python-operations
        

Config API Reference

namingConvention

type: string

Controls how GraphQL response keys (field names / aliases) are turned into Python BaseModel result-class attributes. Variable keys are unaffected — they always keep the wire (GraphQL) name.

  • 'snakeCase' (default): keys are converted from camelCase to snake_case and Python reserved keywords are suffixed with _. Each renamed key is pinned with Field(alias=...) so pydantic hydrates the original wire key onto the attribute.
  • 'keep': response keys are used verbatim; a keyword / non-identifier key falls back to a trailing _ for just that field. Default value: “snakeCase”

baseModel

type: string

The base class that generated operation result types extend. Default value: “BaseModel”

runtimeImport

type: string

The module the base model is imported from. Emitted as from {runtimeImport} import {baseModel} (with ConfigDict / Field appended when the generated types use aliases). Defaults to pydantic. Default value: “pydantic”

typesImport

type: string

Optional module to star-import base schema type names from.

Generated field annotations reference base type names (enums, input types) as quoted forward references, e.g. role: 'Role'. By default no import for those names is emitted (the plugin assumes the base types live in the same file or are provided elsewhere). If set, the plugin prepends from {typesImport} import * so those names resolve at runtime.

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  generates: {
    'operations.py': {
      plugins: ['@graphql-codegen/python-operations'],
      config: {
        typesImport: 'my_package.schema_types',
      },
    },
  },
};
export default config;

avoidOptionals

type: object

This will cause the generator to avoid using TypeScript optionals (?) on types, so the following definition: type A { myField: String } will output myField: Maybe<string> instead of myField?: Maybe<string>. Default value: “false”

Usage Examples

Override all definition types
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript-operations'],
      config: {
        avoidOptionals: true
      },
    },
  },
};
export default config;
Override only specific definition types
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript-operations'],
      config: {
        avoidOptionals: {
          variableValue: true,
          inputValue: true,
          defaultValue: true,
        }
      },
    },
  },
};
export default config;

skipTypeNameForRoot

type: boolean

Avoid adding __typename for root types. This is ignored when a selection explicitly specifies __typename. Default value: “false”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        skipTypeNameForRoot: true
      },
    },
  },
};
export default config;

nonOptionalTypename

type: boolean

Automatically adds __typename field to the generated types, even when they are not specified in the selection set, and makes it non-optional Default value: “false”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        nonOptionalTypename: true
      },
    },
  },
};
export default config;

globalNamespace

type: boolean

Puts all generated code under global namespace. Useful for Stencil integration. Default value: “false”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        globalNamespace: true
      },
    },
  },
};
export default config;

operationResultSuffix

type: string

Adds a suffix to generated operation result type names Default value: ""

dedupeOperationSuffix

type: boolean

Set this configuration to true if you wish to make sure to remove duplicate operation name suffix. Default value: “false”

omitOperationSuffix

type: boolean

Set this configuration to true if you wish to disable auto add suffix of operation name, like Query, Mutation, Subscription, Fragment. Default value: “false”

exportFragmentSpreadSubTypes

type: boolean

If set to true, it will export the sub-types created in order to make it easier to access fields declared under fragment spread. Default value: “false”

experimentalFragmentVariables

type: boolean

If set to true, it will enable support for parsing variables on fragments. Default value: “false”

mergeFragmentTypes

type: boolean

If set to true, merge equal fragment interfaces. Default value: “false”

namespacedImportName

type: string

Prefixes all GraphQL related generated types with that value, as namespaces import. You can use this feature to allow separation of plugins to different files (See importSchemaTypesFrom) Default value: “‘Types’ (if importSchemaTypesFrom is set)”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript-operations'],
      config: {
        importSchemaTypesFrom: './path/to/shared-types.ts',
        namespacedImportName: 'Types'
      },
    },
  },
};
export default config;

customDirectives

type: object

Configures behavior for use with custom directives from various GraphQL libraries.

generateOperationTypes

type: boolean

Whether to generate operation types such as Variables, Query/Mutation/Subscription selection set, and Fragment types This can be used with importSchemaTypesFrom to generate shared used Enums and Input. Default value: “true”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript-operations'],
      config: {
        generateOperationTypes: false,
      },
    },
  },
};
export default config;

importSchemaTypesFrom

type: string

The absolute (prefixed with ~) or relative path from cwd to the shared used Enums and Input (See generateOperationTypes). Default value: “true”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file.ts': {
      plugins: ['typescript-operations'],
      config: {
        importSchemaTypesFrom: './path/to/shared-types.ts', // relative
        importSchemaTypesFrom: '~@my-org/package' // absolute
      },
    },
  },
};
export default config;

extractAllFieldsToTypes

type: boolean

Extract all field types to their own types, instead of inlining them. This helps to reduce type duplication, and makes type errors more readable. It can also significantly reduce the size of the generated code, the generation time, and the typechecking time. Default value: “false”

extractAllFieldsToTypesCompact

type: boolean

Generates type names using only field names, omitting GraphQL type names. This matches the naming convention used by Apollo Tooling. For example, instead of Query_company_Company_office_Office_location_Location, it generates Query_company_office_location.

When this option is enabled, extractAllFieldsToTypes is automatically enabled as well. Default value: “false”

declarationKind

type: object

Overrides the default output for various GraphQL types. Default value: “‘type’”

Usage Examples

Override all declarations
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        declarationKind: 'interface'
      },
    },
  },
};
export default config;
Override only specific declarations
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        declarationKind: {
          input: 'interface',
          result: 'type'
        }
      },
    },
  },
};
export default config;

strictScalars

type: boolean

Makes scalars strict.

If scalars are found in the schema that are not defined in scalars an error will be thrown during codegen. Default value: “false”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        strictScalars: true,
      },
    },
  },
};
export default config;

defaultScalarType

type: string

Allows you to override the type that unknown scalars will have. Default value: “unknown”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        defaultScalarType: 'any'
      },
    },
  },
};
export default config;

scalars

type: object

Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.

typesPrefix

type: string

Prefixes all the generated types. Default value: ""

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        typesPrefix: 'I',
      },
    },
  },
};
export default config;

typesSuffix

type: string

Suffixes all the generated types. Default value: ""

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        typesSuffix: 'I',
      },
    },
  },
};
export default config;

enumPrefix

type: boolean

Allow you to disable prefixing for generated enums, works in combination with typesPrefix. Default value: “true”

Usage Examples

Disable enum prefixes
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        typesPrefix: 'I',
        enumPrefix: false
      },
    },
  },
};
export default config;

enumSuffix

type: boolean

Allow you to disable suffixing for generated enums, works in combination with typesSuffix. Default value: “true”

Usage Examples

Disable enum suffixes
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        typesSuffix: 'I',
        enumSuffix: false
      },
    },
  },
};
export default config;

useTypeImports

type: boolean

Will use import type {} rather than import {} when importing only types. This gives compatibility with TypeScript’s “importsNotUsedAsValues”: “error” option Default value: “false”

Usage Examples

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  generates: {
    'path/to/file': {
      // plugins...
      config: {
        useTypeImports: true
      },
    },
  },
};
export default config;

inlineFragmentTypes

type: string

Whether fragment types should be inlined into other operations. “inline” is the default behavior and will perform deep inlining fragment types within operation type definitions. “combine” is the previous behavior that uses fragment type references without inlining the types (and might cause issues with deeply nested fragment that uses list types). “mask” transforms the types for use with fragment masking. Useful when masked types are needed when not using the “client” preset e.g. such as combining it with Apollo Client’s data masking feature. Default value: “inline”

importExtension

type: object

Append this extension to all imports. Useful for ESM environments that require file extensions in import statements.

printFieldsOnNewLines

type: boolean

If you prefer to have each field in generated types printed on a new line, set this to true. This can be useful for improving readability of the resulting types, without resorting to running tools like Prettier on the output. Default value: “false”

includeExternalFragments

type: boolean

Whether to include external fragments in the generated code. External fragments are not defined in the same location as the operation definition. Default value: “false”

For each .graphql operation document, python-operations generates:

  • a per-operation result type (nested BaseModel classes, snake_case fields, reserved-keyword handling e.g. from → from_)
  • variables as a TypedDict

Type references are generated to line up exactly with python, so run the two plugins together.