Skip to content

Commit

Permalink
feat: add output file for export-schema (#3033)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>
  • Loading branch information
3 people authored Aug 15, 2023
1 parent 4fe8537 commit 472d7fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
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"
"}"
)

0 comments on commit 472d7fd

Please sign in to comment.