diff --git a/src/content/code/language-support/python/client/datamodel-code-generator.md b/src/content/code/language-support/python/client/datamodel-code-generator.md new file mode 100644 index 0000000000..4aee3fbbcf --- /dev/null +++ b/src/content/code/language-support/python/client/datamodel-code-generator.md @@ -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') + +``` diff --git a/src/content/code/language-support/python/client/graphql-query.md b/src/content/code/language-support/python/client/graphql-query.md index 3f1d4e80b9..3d367a23fa 100644 --- a/src/content/code/language-support/python/client/graphql-query.md +++ b/src/content/code/language-support/python/client/graphql-query.md @@ -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