Allow graphql schema export to use a canonical representation

This commit is contained in:
Gary Donovan 2018-05-17 15:26:46 +10:00 committed by Gary Donovan
parent ea2cd9894f
commit 1ca44b55cb
3 changed files with 18 additions and 4 deletions

View File

@ -11,7 +11,7 @@ data to ``schema.json`` that is compatible with babel-relay-plugin.
Usage Usage
----- -----
Include ``graphene_django`` to ``INSTALLED_APPS`` in you project Include ``graphene_django`` to ``INSTALLED_APPS`` in your project
settings: settings:
.. code:: python .. code:: python
@ -29,6 +29,8 @@ It dumps your full introspection schema to ``schema.json`` inside your
project root directory. Point ``babel-relay-plugin`` to this file and project root directory. Point ``babel-relay-plugin`` to this file and
you're ready to use Relay with Graphene GraphQL implementation. you're ready to use Relay with Graphene GraphQL implementation.
The schema file is sorted to create a reproducible canonical representation.
Advanced Usage Advanced Usage
-------------- --------------

View File

@ -39,7 +39,7 @@ class Command(CommandArguments):
def save_file(self, out, schema_dict, indent): def save_file(self, out, schema_dict, indent):
with open(out, "w") as outfile: with open(out, "w") as outfile:
json.dump(schema_dict, outfile, indent=indent) json.dump(schema_dict, outfile, indent=indent, sort_keys=True)
def handle(self, *args, **options): def handle(self, *args, **options):
options_schema = options.get("schema") options_schema = options.get("schema")
@ -65,7 +65,7 @@ class Command(CommandArguments):
indent = options.get("indent") indent = options.get("indent")
schema_dict = {"data": schema.introspect()} schema_dict = {"data": schema.introspect()}
if out == '-': if out == '-':
self.stdout.write(json.dumps(schema_dict, indent=indent)) self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
else: else:
self.save_file(out, schema_dict, indent) self.save_file(out, schema_dict, indent)

View File

@ -1,5 +1,5 @@
from django.core import management from django.core import management
from mock import patch from mock import patch, mock_open
from six import StringIO from six import StringIO
@ -8,3 +8,15 @@ def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
out = StringIO() out = StringIO()
management.call_command("graphql_schema", schema="", stdout=out) management.call_command("graphql_schema", schema="", stdout=out)
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue() assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()
@patch('json.dump')
def test_files_are_sorted(dump_mock):
open_mock = mock_open()
with patch('graphene_django.management.commands.graphql_schema.open', open_mock):
management.call_command('graphql_schema', schema='')
open_mock.assert_called_once()
dump_mock.assert_called_once()
assert dump_mock.call_args[1]["sort_keys"], "json.mock() should be used to sort the output"