Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datamodel code generator python code #1591

Open
wants to merge 2 commits into
base: source
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
---
name: datamodel-code-generator
description: Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, GraphQL and YAML data sources.
url: https://koxudaxi.github.io/datamodel-code-generator/
github: 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

```graphql
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
```

```python
# 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')

```
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
name: graphql-query
description: Complete GraphQL query string generation for python.
description: Complete Domain Specific Language (DSL) for GraphQL query in Python.
url: https://denisart.github.io/graphql-query/
github: denisart/graphql-query
---

**graphql_query** is complete GraphQL query string builder for python. With **graphql_query**
you can The documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/).
**graphql_query** is a complete Domain Specific Language (DSL) for GraphQL query in python. The documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/).

```
$ pip install graphql_query
Expand Down