Skip to content

Commit

Permalink
map str as default for scalars (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishi0x01 authored Jan 13, 2023
1 parent ad9a60a commit 9165fca
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 4 deletions.
12 changes: 12 additions & 0 deletions docs/plugins/pydantic_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ Supported definitions are:
- `fragment`
- `query`

## Opinionated Custom Scalars

The `pydantic_v1` plugin has an opinionated approach towards some very common custom scalars
defined in https://the-guild.dev/graphql/scalars/docs

Currently it maps the following:

- `JSON` maps to `pydantic.Json`
- `DateTime` maps to `datetime.datetime`

Any other custom scalar will be mapped to `str`.

## Examples

### Query with inline fragments
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "qenerate"
version = "0.4.6"
version = "0.4.7"
description = "Code Generator for GraphQL Query and Fragment Data Classes"
authors = [
"Red Hat - Service Delivery - AppSRE <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion qenerate/plugins/pydantic_v1/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def graphql_primitive_to_python(graphql_type: GraphQLOutputType) -> str:
"DateTime": "datetime",
"JSON": "Json",
}
return mapping.get(str(graphql_type), str(graphql_type))
return mapping.get(str(graphql_type), "str")


def graphql_field_name_to_python(name: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup_kwargs = {
"name": "qenerate",
"version": "0.4.6",
"version": "0.4.7",
"description": "Code Generator for GraphQL Query and Fragment Data Classes",
"long_description": "Qenerate is a Code Generator for GraphQL Query and Fragment Data Classes. Documentation is at https://github.com/app-sre/qenerate .",
"author": "Service Delivery - AppSRE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ query IssuesDate {
issues(first: 10) {
nodes {
createdAt
bodyHTML
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ query IssuesDate {
issues(first: 10) {
nodes {
createdAt
bodyHTML
}
}
}
Expand All @@ -34,6 +35,7 @@ query IssuesDate {

class Issue(BaseModel):
created_at: datetime = Field(..., alias="createdAt")
body_html: str = Field(..., alias="bodyHTML")

class Config:
smart_union = True
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Schema(Enum):
{},
{
"invitations_enum": GQLDefinitionType.QUERY,
"issues_datetime": GQLDefinitionType.QUERY,
"issues_datetime_html": GQLDefinitionType.QUERY,
},
{},
Schema.GITHUB,
Expand Down

0 comments on commit 9165fca

Please sign in to comment.