-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial POC for codegeneration * Add support for descriptions * Rename test file * Add support for interfaces * Add support for multi-line descriptions * Add support for snake casing names * For the future * Initial cli, add support for explicit schema * Add support for enums * Add todo * Add support for unions * Add support for inputs * Add support for extensions * Add tests for extension * fix snake case * Add support for scalars * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Mypy fixes * Fix backwards compatibility * Add support for generating the schema object * Add release file * Fix tests * Improve coverage * Add basic docs * Add link * Add CLI tests --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3f219a1
commit 70357c0
Showing
30 changed files
with
1,355 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
Release type: minor | ||
|
||
This release adds support for generating Strawberry types from SDL files. For example, given the following SDL file: | ||
|
||
```graphql | ||
type Query { | ||
user: User | ||
} | ||
|
||
type User { | ||
id: ID! | ||
name: String! | ||
} | ||
``` | ||
|
||
you can run | ||
|
||
```bash | ||
strawberry schema-codegen schema.graphql | ||
``` | ||
|
||
to generate the following Python code: | ||
|
||
```python | ||
import strawberry | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
user: User | None | ||
|
||
|
||
@strawberry.type | ||
class User: | ||
id: strawberry.ID | ||
name: str | ||
|
||
|
||
schema = strawberry.Schema(query=Query) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Schema codegen | ||
--- | ||
|
||
# Schema codegen | ||
|
||
Strawberry supports code generation from SDL files. | ||
|
||
Let's assume we have the following SDL file: | ||
|
||
```graphql | ||
type Query { | ||
user: User | ||
} | ||
|
||
type User { | ||
id: ID! | ||
name: String! | ||
} | ||
``` | ||
|
||
by running the following command: | ||
|
||
```shell | ||
strawberry schema-codegen schema.graphql | ||
``` | ||
|
||
we'll get the following output: | ||
|
||
```python | ||
import strawberry | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
user: User | None | ||
|
||
|
||
@strawberry.type | ||
class User: | ||
id: strawberry.ID | ||
name: str | ||
|
||
|
||
schema = strawberry.Schema(query=Query) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import typer | ||
|
||
from strawberry.cli.app import app | ||
from strawberry.schema_codegen import codegen | ||
|
||
|
||
@app.command(help="Generate code from a query") | ||
def schema_codegen( | ||
schema: Path = typer.Argument(exists=True), | ||
output: Optional[Path] = typer.Option( | ||
None, | ||
"-o", | ||
"--output", | ||
file_okay=True, | ||
dir_okay=False, | ||
writable=True, | ||
resolve_path=True, | ||
), | ||
) -> None: | ||
generated_output = codegen(schema.read_text()) | ||
|
||
if output is None: | ||
typer.echo(generated_output) | ||
return | ||
|
||
output.parent.mkdir(parents=True, exist_ok=True) | ||
output.write_text(generated_output) | ||
|
||
typer.echo(f"Code generated at `{output.name}`") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.