name | description | url | github |
---|---|---|---|
datamodel-code-generator |
Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, GraphQL and YAML data sources. |
koxudaxi/datamodel-code-generator |
This code generator creates pydantic v1 and v2 model, dataclasses.dataclass, typing.TypedDict and msgspec.Struct from a graphql file and others.
$ pip install 'datamodel-code-generator[graphql]'
You can generate models from a local file. For the schema file
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
type Starship {
id: ID!
name: String!
length: Float
}
type Human implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
starships: [Starship]
totalCredits: Int
}
type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}
we can running
$ datamodel-codegen --input schema.graphql --input-file-type graphql --output model.py
# generated by datamodel-codegen:
# filename: schema.graphql
# timestamp: 2023-11-27T07:48:49+00:00
from __future__ import annotations
from enum import Enum
from typing import List, Optional, TypeAlias
from pydantic import BaseModel, Field
from typing_extensions import Literal
Boolean: TypeAlias = bool
"""
The `Boolean` scalar type represents `true` or `false`.
"""
Float: TypeAlias = float
"""
The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).
"""
ID: TypeAlias = str
"""
The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.
"""
Int: TypeAlias = int
"""
The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
"""
String: TypeAlias = str
"""
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
"""
class Episode(Enum):
EMPIRE = 'EMPIRE'
JEDI = 'JEDI'
NEWHOPE = 'NEWHOPE'
class Character(BaseModel):
appearsIn: List[Optional[Episode]]
friends: Optional[List[Optional[Character]]] = Field(default_factory=list)
id: ID
name: String
typename__: Optional[Literal['Character']] = Field('Character', alias='__typename')
class Droid(Character):
appearsIn: List[Optional[Episode]]
friends: Optional[List[Optional[Character]]] = Field(default_factory=list)
id: ID
name: String
primaryFunction: Optional[String] = None
typename__: Optional[Literal['Droid']] = Field('Droid', alias='__typename')
class Human(Character):
appearsIn: List[Optional[Episode]]
friends: Optional[List[Optional[Character]]] = Field(default_factory=list)
id: ID
name: String
starships: Optional[List[Optional[Starship]]] = Field(default_factory=list)
totalCredits: Optional[Int] = None
typename__: Optional[Literal['Human']] = Field('Human', alias='__typename')
class Starship(BaseModel):
id: ID
length: Optional[Float] = None
name: String
typename__: Optional[Literal['Starship']] = Field('Starship', alias='__typename')