-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
87 additions
and
29 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
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,30 @@ | ||
import click | ||
|
||
from strawberry.cli.utils import load_schema | ||
from strawberry.codegen import QueryCodegen | ||
from strawberry.codegen.plugins.python import PythonPlugin | ||
|
||
|
||
@click.command(short_help="Generate code from a query") | ||
@click.argument("schema", type=str) | ||
@click.argument("query", type=str) | ||
@click.option( | ||
"--app-dir", | ||
default=".", | ||
type=str, | ||
show_default=True, | ||
help=( | ||
"Look for the module in the specified directory, by adding this to the " | ||
"PYTHONPATH. Defaults to the current working directory. " | ||
"Works the same as `--app-dir` in uvicorn." | ||
), | ||
) | ||
def codegen(schema: str, query: str, app_dir: str): | ||
schema_symbol = load_schema(schema, app_dir) | ||
|
||
code_generator = QueryCodegen(schema_symbol, plugins=[PythonPlugin()]) | ||
|
||
with open(query) as f: | ||
code = code_generator.codegen(f.read()) | ||
|
||
print(code) |
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,23 @@ | ||
import sys | ||
|
||
import click | ||
|
||
from strawberry import Schema | ||
from strawberry.utils.importer import import_module_symbol | ||
|
||
|
||
def load_schema(schema: str, app_dir: str) -> Schema: | ||
sys.path.insert(0, app_dir) | ||
|
||
try: | ||
schema_symbol = import_module_symbol(schema, default_symbol_name="schema") | ||
except (ImportError, AttributeError) as exc: | ||
message = str(exc) | ||
|
||
raise click.BadArgumentUsage(message) | ||
|
||
if not isinstance(schema_symbol, Schema): | ||
message = "The `schema` must be an instance of strawberry.Schema" | ||
raise click.BadArgumentUsage(message) | ||
|
||
return schema_symbol |
File renamed without changes.
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
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