@graphql-codegen/python 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@graphql-codegen/python",
3
+ "version": "0.0.1",
4
+ "description": "GraphQL Code Generator plugin that emits Python types",
5
+ "sideEffects": false,
6
+ "peerDependencies": {
7
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
8
+ },
9
+ "dependencies": {
10
+ "@graphql-codegen/plugin-helpers": "^6.3.0",
11
+ "@graphql-codegen/python-common": "0.0.1",
12
+ "@graphql-codegen/visitor-plugin-common": "^6.3.0",
13
+ "auto-bind": "~4.0.0",
14
+ "tslib": "^2.8.1"
15
+ },
16
+ "repository": "git@github.com:dotansimha/graphql-code-generator-community.git",
17
+ "license": "MIT",
18
+ "engines": {
19
+ "node": ">= 16.0.0"
20
+ },
21
+ "main": "cjs/index.js",
22
+ "module": "esm/index.js",
23
+ "typings": "typings/index.d.ts",
24
+ "typescript": {
25
+ "definition": "typings/index.d.ts"
26
+ },
27
+ "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "require": {
31
+ "types": "./typings/index.d.cts",
32
+ "default": "./cjs/index.js"
33
+ },
34
+ "import": {
35
+ "types": "./typings/index.d.ts",
36
+ "default": "./esm/index.js"
37
+ },
38
+ "default": {
39
+ "types": "./typings/index.d.ts",
40
+ "default": "./esm/index.js"
41
+ }
42
+ },
43
+ "./package.json": "./package.json"
44
+ }
45
+ }
@@ -0,0 +1,110 @@
1
+ import { RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
+ /**
3
+ * @description This plugin generates Python types based on your `GraphQLSchema`.
4
+ *
5
+ * It generates idiomatic Python for your entire schema:
6
+ * - object & interface types become [pydantic](https://docs.pydantic.dev)
7
+ * `BaseModel` classes with snake_case attributes; each field whose Python name
8
+ * differs from the GraphQL key is pinned with `Field(alias=...)` so pydantic
9
+ * hydrates the wire (camelCase) key onto the attribute,
10
+ * - input types become `TypedDict`s keyed by the WIRE (GraphQL) name — inputs
11
+ * are sent to the server as-is, so their keys are never snake-cased,
12
+ * - enums become `str, Enum` classes,
13
+ * - scalars are mapped to native Python types.
14
+ *
15
+ * By default object / interface field names are converted to snake_case (see
16
+ * `namingConvention`). Every annotation is quoted so forward / recursive
17
+ * references resolve without ordering constraints.
18
+ */
19
+ export interface PythonPluginConfig extends RawTypesConfig {
20
+ /**
21
+ * @description The base class that generated object and interface types extend.
22
+ * @default "BaseModel"
23
+ *
24
+ * @exampleMarkdown
25
+ * ```ts filename="codegen.ts"
26
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
27
+ *
28
+ * const config: CodegenConfig = {
29
+ * generates: {
30
+ * 'schema.py': {
31
+ * plugins: ['@graphql-codegen/python'],
32
+ * config: {
33
+ * baseModel: 'BaseModel',
34
+ * },
35
+ * },
36
+ * },
37
+ * };
38
+ * export default config;
39
+ * ```
40
+ */
41
+ baseModel?: string;
42
+ /**
43
+ * @description The module the base model is imported from. Emitted as
44
+ * `from {runtimeImport} import {baseModel}` (with `ConfigDict` / `Field`
45
+ * appended when the generated types use aliases). Defaults to `pydantic`; if
46
+ * you override it, point it at a module that re-exports a pydantic-compatible
47
+ * `BaseModel`, `ConfigDict`, and `Field`.
48
+ * @default "pydantic"
49
+ *
50
+ * @exampleMarkdown
51
+ * ```ts filename="codegen.ts"
52
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
53
+ *
54
+ * const config: CodegenConfig = {
55
+ * generates: {
56
+ * 'schema.py': {
57
+ * plugins: ['@graphql-codegen/python'],
58
+ * config: {
59
+ * runtimeImport: 'pydantic',
60
+ * },
61
+ * },
62
+ * },
63
+ * };
64
+ * export default config;
65
+ * ```
66
+ */
67
+ runtimeImport?: string;
68
+ /**
69
+ * @description Controls how GraphQL OBJECT / INTERFACE field names are turned
70
+ * into Python class attributes. Input-type keys are unaffected — they always
71
+ * keep the wire (GraphQL) name because the runtime forwards the input dict to
72
+ * the server as-is.
73
+ *
74
+ * - `'snakeCase'` (default): field names are converted from camelCase to
75
+ * snake_case and Python reserved keywords are suffixed with `_`. This is the
76
+ * idiomatic-Python choice; each renamed field is pinned with `Field(alias=...)`
77
+ * (e.g. `from` -> `from_` with `alias='from'`) so pydantic hydrates the
78
+ * original wire key onto the attribute.
79
+ * - `'keep'`: object / interface field names are used verbatim; a keyword /
80
+ * non-identifier field falls back to a trailing `_` for just that field.
81
+ *
82
+ * @default "snakeCase"
83
+ *
84
+ * @exampleMarkdown
85
+ * ```ts filename="codegen.ts"
86
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
87
+ *
88
+ * const config: CodegenConfig = {
89
+ * generates: {
90
+ * 'schema.py': {
91
+ * plugins: ['@graphql-codegen/python'],
92
+ * config: {
93
+ * namingConvention: 'snakeCase',
94
+ * },
95
+ * },
96
+ * },
97
+ * };
98
+ * export default config;
99
+ * ```
100
+ */
101
+ namingConvention?: 'keep' | 'snakeCase';
102
+ /**
103
+ * @description Does not add a `__typename` field to the generated types.
104
+ * For the Python types plugin `__typename` is not emitted by default, so this
105
+ * option is accepted for parity but has no additional effect unless you opt in
106
+ * elsewhere.
107
+ * @default true
108
+ */
109
+ skipTypename?: boolean;
110
+ }
@@ -0,0 +1,110 @@
1
+ import { RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
+ /**
3
+ * @description This plugin generates Python types based on your `GraphQLSchema`.
4
+ *
5
+ * It generates idiomatic Python for your entire schema:
6
+ * - object & interface types become [pydantic](https://docs.pydantic.dev)
7
+ * `BaseModel` classes with snake_case attributes; each field whose Python name
8
+ * differs from the GraphQL key is pinned with `Field(alias=...)` so pydantic
9
+ * hydrates the wire (camelCase) key onto the attribute,
10
+ * - input types become `TypedDict`s keyed by the WIRE (GraphQL) name — inputs
11
+ * are sent to the server as-is, so their keys are never snake-cased,
12
+ * - enums become `str, Enum` classes,
13
+ * - scalars are mapped to native Python types.
14
+ *
15
+ * By default object / interface field names are converted to snake_case (see
16
+ * `namingConvention`). Every annotation is quoted so forward / recursive
17
+ * references resolve without ordering constraints.
18
+ */
19
+ export interface PythonPluginConfig extends RawTypesConfig {
20
+ /**
21
+ * @description The base class that generated object and interface types extend.
22
+ * @default "BaseModel"
23
+ *
24
+ * @exampleMarkdown
25
+ * ```ts filename="codegen.ts"
26
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
27
+ *
28
+ * const config: CodegenConfig = {
29
+ * generates: {
30
+ * 'schema.py': {
31
+ * plugins: ['@graphql-codegen/python'],
32
+ * config: {
33
+ * baseModel: 'BaseModel',
34
+ * },
35
+ * },
36
+ * },
37
+ * };
38
+ * export default config;
39
+ * ```
40
+ */
41
+ baseModel?: string;
42
+ /**
43
+ * @description The module the base model is imported from. Emitted as
44
+ * `from {runtimeImport} import {baseModel}` (with `ConfigDict` / `Field`
45
+ * appended when the generated types use aliases). Defaults to `pydantic`; if
46
+ * you override it, point it at a module that re-exports a pydantic-compatible
47
+ * `BaseModel`, `ConfigDict`, and `Field`.
48
+ * @default "pydantic"
49
+ *
50
+ * @exampleMarkdown
51
+ * ```ts filename="codegen.ts"
52
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
53
+ *
54
+ * const config: CodegenConfig = {
55
+ * generates: {
56
+ * 'schema.py': {
57
+ * plugins: ['@graphql-codegen/python'],
58
+ * config: {
59
+ * runtimeImport: 'pydantic',
60
+ * },
61
+ * },
62
+ * },
63
+ * };
64
+ * export default config;
65
+ * ```
66
+ */
67
+ runtimeImport?: string;
68
+ /**
69
+ * @description Controls how GraphQL OBJECT / INTERFACE field names are turned
70
+ * into Python class attributes. Input-type keys are unaffected — they always
71
+ * keep the wire (GraphQL) name because the runtime forwards the input dict to
72
+ * the server as-is.
73
+ *
74
+ * - `'snakeCase'` (default): field names are converted from camelCase to
75
+ * snake_case and Python reserved keywords are suffixed with `_`. This is the
76
+ * idiomatic-Python choice; each renamed field is pinned with `Field(alias=...)`
77
+ * (e.g. `from` -> `from_` with `alias='from'`) so pydantic hydrates the
78
+ * original wire key onto the attribute.
79
+ * - `'keep'`: object / interface field names are used verbatim; a keyword /
80
+ * non-identifier field falls back to a trailing `_` for just that field.
81
+ *
82
+ * @default "snakeCase"
83
+ *
84
+ * @exampleMarkdown
85
+ * ```ts filename="codegen.ts"
86
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
87
+ *
88
+ * const config: CodegenConfig = {
89
+ * generates: {
90
+ * 'schema.py': {
91
+ * plugins: ['@graphql-codegen/python'],
92
+ * config: {
93
+ * namingConvention: 'snakeCase',
94
+ * },
95
+ * },
96
+ * },
97
+ * };
98
+ * export default config;
99
+ * ```
100
+ */
101
+ namingConvention?: 'keep' | 'snakeCase';
102
+ /**
103
+ * @description Does not add a `__typename` field to the generated types.
104
+ * For the Python types plugin `__typename` is not emitted by default, so this
105
+ * option is accepted for parity but has no additional effect unless you opt in
106
+ * elsewhere.
107
+ * @default true
108
+ */
109
+ skipTypename?: boolean;
110
+ }
@@ -0,0 +1,5 @@
1
+ import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
2
+ import { PythonPluginConfig } from './config.cjs';
3
+ export * from './config.cjs';
4
+ export * from './visitor.cjs';
5
+ export declare const plugin: PluginFunction<PythonPluginConfig, Types.ComplexPluginOutput>;
@@ -0,0 +1,5 @@
1
+ import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
2
+ import { PythonPluginConfig } from './config.js';
3
+ export * from './config.js';
4
+ export * from './visitor.js';
5
+ export declare const plugin: PluginFunction<PythonPluginConfig, Types.ComplexPluginOutput>;
@@ -0,0 +1,96 @@
1
+ import { EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, ObjectTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
2
+ import { PYTHON_SCALARS } from '@graphql-codegen/python-common';
3
+ import { BaseTypesVisitor, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
4
+ import { PythonPluginConfig } from './config.cjs';
5
+ export { PYTHON_SCALARS };
6
+ export type PythonNamingConvention = 'keep' | 'snakeCase';
7
+ export interface PythonPluginParsedConfig extends ParsedTypesConfig {
8
+ baseModel: string;
9
+ runtimeImport: string;
10
+ pyNamingConvention: PythonNamingConvention;
11
+ }
12
+ export declare class PythonVisitor<TRawConfig extends PythonPluginConfig = PythonPluginConfig, TParsedConfig extends PythonPluginParsedConfig = PythonPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> {
13
+ private _usesEnum;
14
+ private _usesTypedDict;
15
+ private _usesBaseModel;
16
+ private _usesUnion;
17
+ constructor(schema: GraphQLSchema, pluginConfig: TRawConfig);
18
+ /** Python has no `export` keyword; disable the C-family export prefix. */
19
+ protected getExportPrefix(): string;
20
+ protected _getScalar(name: string, type: 'input' | 'output'): string;
21
+ NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
22
+ ListType(node: ListTypeNode, key: any, parent: any, path: any, ancestors: any): string;
23
+ protected wrapWithListType(str: string): string;
24
+ NonNullType(node: NonNullTypeNode): string;
25
+ private snakeFieldName;
26
+ /**
27
+ * Field key for a `TypedDict` (input type). Input types are what the caller
28
+ * SENDS — the runtime forwards the dict to the server as-is — so they ALWAYS
29
+ * keep the wire (GraphQL) name regardless of `namingConvention`. If the name
30
+ * is a keyword / non-identifier the containing `TypedDict` is later emitted
31
+ * with functional syntax (see `buildTypedDict`).
32
+ */
33
+ private dictFieldName;
34
+ /**
35
+ * Field key for an object / interface type (a class attribute). Under `'keep'`
36
+ * a keyword / non-identifier name cannot be a class attribute and BaseModel
37
+ * cannot use functional `TypedDict` syntax, so we fall back to appending `_`
38
+ * for just that field.
39
+ */
40
+ private objectFieldName;
41
+ FieldDefinition(node: FieldDefinitionNode): string;
42
+ InputValueDefinition(node: InputValueDefinitionNode): string;
43
+ private buildClass;
44
+ /**
45
+ * Build a base-model subclass. Fields are class-level annotations; if any field
46
+ * pins a wire key with `Field(alias=...)`, a
47
+ * `model_config = ConfigDict(populate_by_name=True)` line is prepended so the
48
+ * class can be constructed by attribute name as well as by alias.
49
+ */
50
+ private buildBaseModelClass;
51
+ /**
52
+ * Split a rendered `key: 'annotation'` field back into its parts. The key is
53
+ * always a GraphQL Name (no `: '`) and the annotation is always single-quoted,
54
+ * so splitting on the first `: '` is unambiguous.
55
+ */
56
+ private splitField;
57
+ /**
58
+ * Build a `TypedDict`. If every key is a valid Python attribute, the readable
59
+ * class syntax is used; otherwise (a key is a keyword / non-identifier under
60
+ * `'keep'`) the functional `TypedDict('Name', { 'key': 'annotation' })` syntax
61
+ * is emitted so the offending keys remain valid.
62
+ */
63
+ private buildTypedDict;
64
+ ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string;
65
+ InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string;
66
+ InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string;
67
+ EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
68
+ /** Scalars are handled inline via the scalar map; no standalone class. */
69
+ ScalarTypeDefinition(): string;
70
+ /**
71
+ * `union Foo = A | B` -> `Foo = Union['A', 'B']`.
72
+ *
73
+ * Must be overridden: `BaseTypesVisitor`'s implementation emits TypeScript
74
+ * (`export type Foo = A | B;`), and it dereferences the node description
75
+ * unguarded, so an undocumented union crashes the generator outright. A schema
76
+ * merely *containing* a union is enough to hit this, even when no operation
77
+ * selects one.
78
+ *
79
+ * Member names come from `parent[key]` — the original, un-visited node — the
80
+ * same way `typescript`'s visitor reads them. By `leave` time `node.types`
81
+ * has already been rendered into annotations (`Optional[Foo]`) and would
82
+ * otherwise be mangled. Members are quoted so a union declared before its
83
+ * members still resolves, matching how every other annotation in this plugin
84
+ * is emitted.
85
+ *
86
+ * Note this only makes a union *declaration* valid Python. Actually selecting
87
+ * union members in an operation needs inline-fragment support, which
88
+ * `python-operations` does not implement yet (it throws).
89
+ */
90
+ UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string;
91
+ /**
92
+ * Build the `prepend` import lines, emitting only what the generated
93
+ * `content` actually references.
94
+ */
95
+ getImports(content: string): string[];
96
+ }
@@ -0,0 +1,96 @@
1
+ import { EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, ObjectTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql';
2
+ import { PYTHON_SCALARS } from '@graphql-codegen/python-common';
3
+ import { BaseTypesVisitor, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common';
4
+ import { PythonPluginConfig } from './config.js';
5
+ export { PYTHON_SCALARS };
6
+ export type PythonNamingConvention = 'keep' | 'snakeCase';
7
+ export interface PythonPluginParsedConfig extends ParsedTypesConfig {
8
+ baseModel: string;
9
+ runtimeImport: string;
10
+ pyNamingConvention: PythonNamingConvention;
11
+ }
12
+ export declare class PythonVisitor<TRawConfig extends PythonPluginConfig = PythonPluginConfig, TParsedConfig extends PythonPluginParsedConfig = PythonPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> {
13
+ private _usesEnum;
14
+ private _usesTypedDict;
15
+ private _usesBaseModel;
16
+ private _usesUnion;
17
+ constructor(schema: GraphQLSchema, pluginConfig: TRawConfig);
18
+ /** Python has no `export` keyword; disable the C-family export prefix. */
19
+ protected getExportPrefix(): string;
20
+ protected _getScalar(name: string, type: 'input' | 'output'): string;
21
+ NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string;
22
+ ListType(node: ListTypeNode, key: any, parent: any, path: any, ancestors: any): string;
23
+ protected wrapWithListType(str: string): string;
24
+ NonNullType(node: NonNullTypeNode): string;
25
+ private snakeFieldName;
26
+ /**
27
+ * Field key for a `TypedDict` (input type). Input types are what the caller
28
+ * SENDS — the runtime forwards the dict to the server as-is — so they ALWAYS
29
+ * keep the wire (GraphQL) name regardless of `namingConvention`. If the name
30
+ * is a keyword / non-identifier the containing `TypedDict` is later emitted
31
+ * with functional syntax (see `buildTypedDict`).
32
+ */
33
+ private dictFieldName;
34
+ /**
35
+ * Field key for an object / interface type (a class attribute). Under `'keep'`
36
+ * a keyword / non-identifier name cannot be a class attribute and BaseModel
37
+ * cannot use functional `TypedDict` syntax, so we fall back to appending `_`
38
+ * for just that field.
39
+ */
40
+ private objectFieldName;
41
+ FieldDefinition(node: FieldDefinitionNode): string;
42
+ InputValueDefinition(node: InputValueDefinitionNode): string;
43
+ private buildClass;
44
+ /**
45
+ * Build a base-model subclass. Fields are class-level annotations; if any field
46
+ * pins a wire key with `Field(alias=...)`, a
47
+ * `model_config = ConfigDict(populate_by_name=True)` line is prepended so the
48
+ * class can be constructed by attribute name as well as by alias.
49
+ */
50
+ private buildBaseModelClass;
51
+ /**
52
+ * Split a rendered `key: 'annotation'` field back into its parts. The key is
53
+ * always a GraphQL Name (no `: '`) and the annotation is always single-quoted,
54
+ * so splitting on the first `: '` is unambiguous.
55
+ */
56
+ private splitField;
57
+ /**
58
+ * Build a `TypedDict`. If every key is a valid Python attribute, the readable
59
+ * class syntax is used; otherwise (a key is a keyword / non-identifier under
60
+ * `'keep'`) the functional `TypedDict('Name', { 'key': 'annotation' })` syntax
61
+ * is emitted so the offending keys remain valid.
62
+ */
63
+ private buildTypedDict;
64
+ ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string;
65
+ InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string;
66
+ InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string;
67
+ EnumTypeDefinition(node: EnumTypeDefinitionNode): string;
68
+ /** Scalars are handled inline via the scalar map; no standalone class. */
69
+ ScalarTypeDefinition(): string;
70
+ /**
71
+ * `union Foo = A | B` -> `Foo = Union['A', 'B']`.
72
+ *
73
+ * Must be overridden: `BaseTypesVisitor`'s implementation emits TypeScript
74
+ * (`export type Foo = A | B;`), and it dereferences the node description
75
+ * unguarded, so an undocumented union crashes the generator outright. A schema
76
+ * merely *containing* a union is enough to hit this, even when no operation
77
+ * selects one.
78
+ *
79
+ * Member names come from `parent[key]` — the original, un-visited node — the
80
+ * same way `typescript`'s visitor reads them. By `leave` time `node.types`
81
+ * has already been rendered into annotations (`Optional[Foo]`) and would
82
+ * otherwise be mangled. Members are quoted so a union declared before its
83
+ * members still resolves, matching how every other annotation in this plugin
84
+ * is emitted.
85
+ *
86
+ * Note this only makes a union *declaration* valid Python. Actually selecting
87
+ * union members in an operation needs inline-fragment support, which
88
+ * `python-operations` does not implement yet (it throws).
89
+ */
90
+ UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string;
91
+ /**
92
+ * Build the `prepend` import lines, emitting only what the generated
93
+ * `content` actually references.
94
+ */
95
+ getImports(content: string): string[];
96
+ }
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@graphql-codegen/python",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "description": "GraphQL Code Generator plugin that emits Python types",
6
+ "repository": "git@github.com:dotansimha/graphql-code-generator-community.git",
7
+ "license": "MIT",
8
+ "engines": {
9
+ "node": ">= 16.0.0"
10
+ },
11
+ "main": "dist/cjs/index.js",
12
+ "module": "dist/esm/index.js",
13
+ "exports": {
14
+ ".": {
15
+ "require": {
16
+ "types": "./dist/typings/index.d.cts",
17
+ "default": "./dist/cjs/index.js"
18
+ },
19
+ "import": {
20
+ "types": "./dist/typings/index.d.ts",
21
+ "default": "./dist/esm/index.js"
22
+ },
23
+ "default": {
24
+ "types": "./dist/typings/index.d.ts",
25
+ "default": "./dist/esm/index.js"
26
+ }
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "typings": "dist/typings/index.d.ts",
31
+ "scripts": {
32
+ "lint": "eslint **/*.ts"
33
+ },
34
+ "peerDependencies": {
35
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
36
+ },
37
+ "dependencies": {
38
+ "@graphql-codegen/plugin-helpers": "^6.3.0",
39
+ "@graphql-codegen/python-common": "0.0.1",
40
+ "@graphql-codegen/visitor-plugin-common": "^6.3.0",
41
+ "auto-bind": "~4.0.0",
42
+ "tslib": "^2.8.1"
43
+ },
44
+ "devDependencies": {
45
+ "@graphql-codegen/testing": "4.0.5"
46
+ },
47
+ "publishConfig": {
48
+ "directory": "dist",
49
+ "access": "public"
50
+ },
51
+ "sideEffects": false,
52
+ "typescript": {
53
+ "definition": "dist/typings/index.d.ts"
54
+ }
55
+ }
package/src/config.ts ADDED
@@ -0,0 +1,111 @@
1
+ import { RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
+
3
+ /**
4
+ * @description This plugin generates Python types based on your `GraphQLSchema`.
5
+ *
6
+ * It generates idiomatic Python for your entire schema:
7
+ * - object & interface types become [pydantic](https://docs.pydantic.dev)
8
+ * `BaseModel` classes with snake_case attributes; each field whose Python name
9
+ * differs from the GraphQL key is pinned with `Field(alias=...)` so pydantic
10
+ * hydrates the wire (camelCase) key onto the attribute,
11
+ * - input types become `TypedDict`s keyed by the WIRE (GraphQL) name — inputs
12
+ * are sent to the server as-is, so their keys are never snake-cased,
13
+ * - enums become `str, Enum` classes,
14
+ * - scalars are mapped to native Python types.
15
+ *
16
+ * By default object / interface field names are converted to snake_case (see
17
+ * `namingConvention`). Every annotation is quoted so forward / recursive
18
+ * references resolve without ordering constraints.
19
+ */
20
+ export interface PythonPluginConfig extends RawTypesConfig {
21
+ /**
22
+ * @description The base class that generated object and interface types extend.
23
+ * @default "BaseModel"
24
+ *
25
+ * @exampleMarkdown
26
+ * ```ts filename="codegen.ts"
27
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
28
+ *
29
+ * const config: CodegenConfig = {
30
+ * generates: {
31
+ * 'schema.py': {
32
+ * plugins: ['@graphql-codegen/python'],
33
+ * config: {
34
+ * baseModel: 'BaseModel',
35
+ * },
36
+ * },
37
+ * },
38
+ * };
39
+ * export default config;
40
+ * ```
41
+ */
42
+ baseModel?: string;
43
+ /**
44
+ * @description The module the base model is imported from. Emitted as
45
+ * `from {runtimeImport} import {baseModel}` (with `ConfigDict` / `Field`
46
+ * appended when the generated types use aliases). Defaults to `pydantic`; if
47
+ * you override it, point it at a module that re-exports a pydantic-compatible
48
+ * `BaseModel`, `ConfigDict`, and `Field`.
49
+ * @default "pydantic"
50
+ *
51
+ * @exampleMarkdown
52
+ * ```ts filename="codegen.ts"
53
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
54
+ *
55
+ * const config: CodegenConfig = {
56
+ * generates: {
57
+ * 'schema.py': {
58
+ * plugins: ['@graphql-codegen/python'],
59
+ * config: {
60
+ * runtimeImport: 'pydantic',
61
+ * },
62
+ * },
63
+ * },
64
+ * };
65
+ * export default config;
66
+ * ```
67
+ */
68
+ runtimeImport?: string;
69
+ /**
70
+ * @description Controls how GraphQL OBJECT / INTERFACE field names are turned
71
+ * into Python class attributes. Input-type keys are unaffected — they always
72
+ * keep the wire (GraphQL) name because the runtime forwards the input dict to
73
+ * the server as-is.
74
+ *
75
+ * - `'snakeCase'` (default): field names are converted from camelCase to
76
+ * snake_case and Python reserved keywords are suffixed with `_`. This is the
77
+ * idiomatic-Python choice; each renamed field is pinned with `Field(alias=...)`
78
+ * (e.g. `from` -> `from_` with `alias='from'`) so pydantic hydrates the
79
+ * original wire key onto the attribute.
80
+ * - `'keep'`: object / interface field names are used verbatim; a keyword /
81
+ * non-identifier field falls back to a trailing `_` for just that field.
82
+ *
83
+ * @default "snakeCase"
84
+ *
85
+ * @exampleMarkdown
86
+ * ```ts filename="codegen.ts"
87
+ * import type { CodegenConfig } from '@graphql-codegen/cli';
88
+ *
89
+ * const config: CodegenConfig = {
90
+ * generates: {
91
+ * 'schema.py': {
92
+ * plugins: ['@graphql-codegen/python'],
93
+ * config: {
94
+ * namingConvention: 'snakeCase',
95
+ * },
96
+ * },
97
+ * },
98
+ * };
99
+ * export default config;
100
+ * ```
101
+ */
102
+ namingConvention?: 'keep' | 'snakeCase';
103
+ /**
104
+ * @description Does not add a `__typename` field to the generated types.
105
+ * For the Python types plugin `__typename` is not emitted by default, so this
106
+ * option is accepted for parity but has no additional effect unless you opt in
107
+ * elsewhere.
108
+ * @default true
109
+ */
110
+ skipTypename?: boolean;
111
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { GraphQLSchema } from 'graphql';
2
+ import {
3
+ getCachedDocumentNodeFromSchema,
4
+ oldVisit,
5
+ PluginFunction,
6
+ Types,
7
+ } from '@graphql-codegen/plugin-helpers';
8
+ import { PythonPluginConfig } from './config.js';
9
+ import { PythonVisitor } from './visitor.js';
10
+
11
+ export * from './config.js';
12
+ export * from './visitor.js';
13
+
14
+ export const plugin: PluginFunction<PythonPluginConfig, Types.ComplexPluginOutput> = (
15
+ schema: GraphQLSchema,
16
+ _documents: Types.DocumentFile[],
17
+ config: PythonPluginConfig,
18
+ ) => {
19
+ const astNode = getCachedDocumentNodeFromSchema(schema);
20
+ const visitor = new PythonVisitor(schema, config);
21
+
22
+ const visitorResult = oldVisit(astNode, { leave: visitor });
23
+
24
+ const content = (visitorResult.definitions as unknown[])
25
+ .filter((def): def is string => typeof def === 'string' && def.length > 0)
26
+ .join('\n\n\n');
27
+
28
+ return {
29
+ prepend: visitor.getImports(content),
30
+ content,
31
+ };
32
+ };