Skip to content

Commit 348fcf3

Browse files
authored
Detect format from output file path (#868)
* Detect format from output file path * Fix tests * Add test for exporting graphql file * Add some documentation
1 parent b8e598d commit 348fcf3

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

docs/introspection.rst

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ you're ready to use Relay with Graphene GraphQL implementation.
2929

3030
The schema file is sorted to create a reproducible canonical representation.
3131

32+
GraphQL SDL Representation
33+
--------------------------
34+
35+
The schema can also be exported as a GraphQL SDL file by changing the file
36+
extension :
37+
38+
.. code:: bash
39+
40+
./manage.py graphql_schema --schema tutorial.quickstart.schema --out schema.graphql
41+
42+
When exporting the schema as a ``.graphql`` file the ``--indent`` option is
43+
ignored.
44+
45+
3246
Advanced Usage
3347
--------------
3448

graphene_django/management/commands/graphql_schema.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import os
12
import importlib
23
import json
34
import functools
45

56
from django.core.management.base import BaseCommand, CommandError
67
from django.utils import autoreload
78

9+
from graphql import print_schema
810
from graphene_django.settings import graphene_settings
911

1012

@@ -44,24 +46,40 @@ def add_arguments(self, parser):
4446

4547

4648
class Command(CommandArguments):
47-
help = "Dump Graphene schema JSON to file"
49+
help = "Dump Graphene schema as a JSON or GraphQL file"
4850
can_import_settings = True
4951

50-
def save_file(self, out, schema_dict, indent):
52+
def save_json_file(self, out, schema_dict, indent):
5153
with open(out, "w") as outfile:
5254
json.dump(schema_dict, outfile, indent=indent, sort_keys=True)
5355

56+
def save_graphql_file(self, out, schema):
57+
with open(out, "w") as outfile:
58+
outfile.write(print_schema(schema))
59+
5460
def get_schema(self, schema, out, indent):
5561
schema_dict = {"data": schema.introspect()}
5662
if out == "-":
5763
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
5864
else:
59-
self.save_file(out, schema_dict, indent)
65+
# Determine format
66+
_, file_extension = os.path.splitext(out)
67+
68+
if file_extension == ".graphql":
69+
self.save_graphql_file(out, schema)
70+
elif file_extension == ".json":
71+
self.save_json_file(out, schema_dict, indent)
72+
else:
73+
raise CommandError(
74+
'Unrecognised file format "{}"'.format(file_extension)
75+
)
6076

6177
style = getattr(self, "style", None)
6278
success = getattr(style, "SUCCESS", lambda x: x)
6379

64-
self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))
80+
self.stdout.write(
81+
success("Successfully dumped GraphQL schema to {}".format(out))
82+
)
6583

6684
def handle(self, *args, **options):
6785
options_schema = options.get("schema")

graphene_django/tests/test_command.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
from textwrap import dedent
2+
13
from django.core import management
2-
from mock import patch, mock_open
4+
from mock import mock_open, patch
35
from six import StringIO
46

7+
from graphene import ObjectType, Schema, String
8+
59

6-
@patch("graphene_django.management.commands.graphql_schema.Command.save_file")
7-
def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
10+
@patch("graphene_django.management.commands.graphql_schema.Command.save_json_file")
11+
def test_generate_json_file_on_call_graphql_schema(savefile_mock, settings):
812
out = StringIO()
913
management.call_command("graphql_schema", schema="", stdout=out)
1014
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
1115

1216

1317
@patch("json.dump")
14-
def test_files_are_canonical(dump_mock):
18+
def test_json_files_are_canonical(dump_mock):
1519
open_mock = mock_open()
1620
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
1721
management.call_command("graphql_schema", schema="")
@@ -25,3 +29,34 @@ def test_files_are_canonical(dump_mock):
2529
assert (
2630
dump_mock.call_args[1]["indent"] > 0
2731
), "output should be pretty-printed by default"
32+
33+
34+
def test_generate_graphql_file_on_call_graphql_schema():
35+
class Query(ObjectType):
36+
hi = String()
37+
38+
mock_schema = Schema(query=Query)
39+
40+
open_mock = mock_open()
41+
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
42+
management.call_command(
43+
"graphql_schema", schema=mock_schema, out="schema.graphql"
44+
)
45+
46+
open_mock.assert_called_once()
47+
48+
handle = open_mock()
49+
assert handle.write.called_once()
50+
51+
schema_output = handle.write.call_args[0][0]
52+
assert schema_output == dedent(
53+
"""\
54+
schema {
55+
query: Query
56+
}
57+
58+
type Query {
59+
hi: String
60+
}
61+
"""
62+
)

0 commit comments

Comments
 (0)