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

feat: add output file for export-schema #3033

Merged
merged 7 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Adds a new flag to `export-schema` command, `--output`, which allows the user to specify the output file. If unset (current behavior), the command will continue to print to stdout.
6 changes: 6 additions & 0 deletions docs/guides/schema-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ In order to store the exported schema in a file, pipes or redirection can be uti
```bash
strawberry export-schema package.module:schema > schema.graphql
```

Alternatively, the `--output` option can be used:

```bash
strawberry export-schema package.module:schema --output schema.graphql
```
16 changes: 15 additions & 1 deletion strawberry/cli/commands/export_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import typer

from strawberry.cli.app import app
Expand All @@ -18,7 +20,19 @@ def export_schema(
"Works the same as `--app-dir` in uvicorn."
),
),
output: Path = typer.Option(
None,
"--output",
"-o",
help="File to save the exported schema. If not provided, prints to console.",
),
) -> None:
schema_symbol = load_schema(schema, app_dir)

print(print_schema(schema_symbol)) # noqa: T201
schema_text = print_schema(schema_symbol)

if output:
Path(output).write_text(schema_text)
typer.echo(f"Schema exported to {output}")
else:
print(schema_text) # noqa: T201
22 changes: 22 additions & 0 deletions tests/cli/test_export_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,25 @@ def test_invalid_schema_instance(cli_app: Typer, cli_runner: CliRunner):

assert result.exit_code == 2
assert expected_error in result.stdout.replace("\n", "")


def test_output_option(cli_app: Typer, cli_runner: CliRunner, tmp_path):
selector = "tests.fixtures.sample_package.sample_module:schema"
output = tmp_path / "schema.graphql"
output_commands = ["--output", "-o"]
for output_command in output_commands:
result = cli_runner.invoke(
cli_app, ["export-schema", selector, output_command, str(output)]
)

assert result.exit_code == 0
assert output.read_text() == (
"type Query {\n"
" user: User!\n"
"}\n"
"\n"
"type User {\n"
" name: String!\n"
" age: Int!\n"
"}"
)
Loading