UNPKG

@graphql-codegen/python

Version:
97 lines (96 loc) • 5.23 kB
import { EnumTypeDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NonNullTypeNode, ObjectTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql'; import { PYTHON_SCALARS } from '@graphql-codegen/python-common'; import { BaseTypesVisitor, ParsedTypesConfig } from '@graphql-codegen/visitor-plugin-common'; import { PythonPluginConfig } from './config.js'; export { PYTHON_SCALARS }; export type PythonNamingConvention = 'keep' | 'snakeCase'; export interface PythonPluginParsedConfig extends ParsedTypesConfig { baseModel: string; runtimeImport: string; pyNamingConvention: PythonNamingConvention; } export declare class PythonVisitor<TRawConfig extends PythonPluginConfig = PythonPluginConfig, TParsedConfig extends PythonPluginParsedConfig = PythonPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> { private _usesEnum; private _usesTypedDict; private _usesBaseModel; private _usesUnion; constructor(schema: GraphQLSchema, pluginConfig: TRawConfig); /** Python has no `export` keyword; disable the C-family export prefix. */ protected getExportPrefix(): string; protected _getScalar(name: string, type: 'input' | 'output'): string; NamedType(node: NamedTypeNode, key: any, parent: any, path: any, ancestors: any): string; ListType(node: ListTypeNode, key: any, parent: any, path: any, ancestors: any): string; protected wrapWithListType(str: string): string; NonNullType(node: NonNullTypeNode): string; private snakeFieldName; /** * Field key for a `TypedDict` (input type). Input types are what the caller * SENDS — the runtime forwards the dict to the server as-is — so they ALWAYS * keep the wire (GraphQL) name regardless of `namingConvention`. If the name * is a keyword / non-identifier the containing `TypedDict` is later emitted * with functional syntax (see `buildTypedDict`). */ private dictFieldName; /** * Field key for an object / interface type (a class attribute). Under `'keep'` * a keyword / non-identifier name cannot be a class attribute and BaseModel * cannot use functional `TypedDict` syntax, so we fall back to appending `_` * for just that field. */ private objectFieldName; FieldDefinition(node: FieldDefinitionNode): string; InputValueDefinition(node: InputValueDefinitionNode): string; private buildClass; /** * Build a base-model subclass. Fields are class-level annotations; if any field * pins a wire key with `Field(alias=...)`, a * `model_config = ConfigDict(populate_by_name=True)` line is prepended so the * class can be constructed by attribute name as well as by alias. */ private buildBaseModelClass; /** * Split a rendered `key: 'annotation'` field back into its parts. The key is * always a GraphQL Name (no `: '`) and the annotation is always single-quoted, * so splitting on the first `: '` is unambiguous. */ private splitField; /** * Build a `TypedDict`. If every key is a valid Python attribute, the readable * class syntax is used; otherwise (a key is a keyword / non-identifier under * `'keep'`) the functional `TypedDict('Name', { 'key': 'annotation' })` syntax * is emitted so the offending keys remain valid. */ private buildTypedDict; ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string; InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string; InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string; EnumTypeDefinition(node: EnumTypeDefinitionNode): string; /** Scalars are handled inline via the scalar map; no standalone class. */ ScalarTypeDefinition(): string; /** * `union Foo = A | B` -> `Foo = Union['A', 'B']`. * * Must be overridden: `BaseTypesVisitor`'s implementation emits TypeScript * (`export type Foo = A | B;`), and it dereferences the node description * unguarded, so an undocumented union crashes the generator outright. A schema * merely *containing* a union is enough to hit this, even when no operation * selects one. * * Member names come from `parent[key]` — the original, un-visited node — the * same way `typescript`'s visitor reads them. By `leave` time `node.types` * has already been rendered into annotations (`Optional[Foo]`) and would * otherwise be mangled. Members are quoted so a union declared before its * members still resolves, matching how every other annotation in this plugin * is emitted. * * Note this only makes a union *declaration* valid Python. Actually selecting * union members in an operation needs inline-fragment support, which * `python-operations` does not implement yet (it throws). */ UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string; /** * Build the `prepend` import lines, emitting only what the generated * `content` actually references. */ getImports(content: string): string[]; }