UNPKG

@graphql-codegen/python-operations

Version:

Document-driven GraphQL Code Generator plugin that emits Python operation types (per-operation result + variables).

82 lines (81 loc) • 3.67 kB
import { GraphQLSchema, OperationDefinitionNode } from 'graphql'; import { BaseVisitor } from '@graphql-codegen/visitor-plugin-common'; import { PythonOperationsPluginConfig } from './config.js'; /** * Walks GraphQL `OperationDefinition` nodes against the schema and recursively * emits named, nested Python `TypedDict` classes for every object selection set, * plus a `<Operation>Variables` `TypedDict` for the operation variables. * * Unlike the C-family `SelectionSetToObject`/`DeclarationBlock` approach (which * emits inline anonymous objects), this mirrors the C#/Java strategy: every * nested selection becomes its own named type referenced by its parent. */ export declare class PythonOperationsVisitor extends BaseVisitor<PythonOperationsPluginConfig, any> { private _schema; private _typesImport?; private _namingConvention; private _baseModel; private _runtimeImport; private _usesOptional; private _usesAny; private _usesLiteral; private _usesBaseModel; private _usesConfigDict; private _usesField; private _anonymousCount; constructor(schema: GraphQLSchema, rawConfig: PythonOperationsPluginConfig); /** PascalCase a name segment without applying any prefix/suffix. */ private pascal; private get operationRootTypes(); /** * Build a Python annotation from a (resolved) GraphQL output type. The * `objectBaseName` is the generated nested type name used when the base type * is an object/interface/union selection. */ private pyTypeFromGraphQL; private innerFromGraphQL; /** * Build a Python annotation from a variable `TypeNode`. Named types resolve * to mapped scalars, or the bare base type name (enums / input objects). */ private pyTypeFromTypeNode; private innerFromTypeNode; /** * Build a variables `TypedDict`. Variable keys are ALWAYS the wire (GraphQL) * name — they are sent to the server as-is. If every key is a valid Python * attribute the readable class syntax is used; otherwise (a key is a keyword / * non-identifier) the functional `TypedDict('Name', { 'key': '...' })` syntax * is emitted so the offending keys remain valid. */ private buildVariablesClass; /** * Emit the per-class `__init__` that pre-maps wire keys to their escaped * attribute names before delegating to the base model. Only emitted for result * classes that contain keyword fields. */ /** * Render a single result field line: `key: 'annotation'`, with a pydantic * `Field(alias=...)` when the Python attribute differs from the wire response * key, and a `None` default for optional fields. */ private renderResultField; /** * Build a result base-model subclass. A field whose Python attribute differs * from the wire key is pinned with `Field(alias=...)`, and a * `model_config = ConfigDict(populate_by_name=True)` line is prepended so such * classes can also be built by attribute name. */ private buildResultClass; /** * Emit a `TypedDict` for `selectionSet` (a selection against `parentType`), * named `typeName`. Nested object selections are emitted FIRST (deepest * first) so references resolve top-to-bottom. Returns nothing; classes are * pushed onto `out`. */ private processSelectionSet; private buildVariables; /** Generate all types for a single operation. Returns joined class blocks. */ generateOperation(operation: OperationDefinitionNode): string; /** Build the `prepend` import lines, emitting only what is referenced. */ getImports(): string[]; }