From 1ca44b55cb9d3d222ef7785a9730205ee3255d9c Mon Sep 17 00:00:00 2001 From: Gary Donovan Date: Thu, 17 May 2018 15:26:46 +1000 Subject: [PATCH] Allow graphql schema export to use a canonical representation --- docs/introspection.rst | 4 +++- .../management/commands/graphql_schema.py | 4 ++-- graphene_django/tests/test_command.py | 14 +++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/introspection.rst b/docs/introspection.rst index 0d30ee4..bd80f26 100644 --- a/docs/introspection.rst +++ b/docs/introspection.rst @@ -11,7 +11,7 @@ data to ``schema.json`` that is compatible with babel-relay-plugin. Usage ----- -Include ``graphene_django`` to ``INSTALLED_APPS`` in you project +Include ``graphene_django`` to ``INSTALLED_APPS`` in your project settings: .. 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 you're ready to use Relay with Graphene GraphQL implementation. +The schema file is sorted to create a reproducible canonical representation. + Advanced Usage -------------- diff --git a/graphene_django/management/commands/graphql_schema.py b/graphene_django/management/commands/graphql_schema.py index 4e526ec..d7f83da 100644 --- a/graphene_django/management/commands/graphql_schema.py +++ b/graphene_django/management/commands/graphql_schema.py @@ -39,7 +39,7 @@ class Command(CommandArguments): def save_file(self, out, schema_dict, indent): 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): options_schema = options.get("schema") @@ -65,7 +65,7 @@ class Command(CommandArguments): indent = options.get("indent") schema_dict = {"data": schema.introspect()} if out == '-': - self.stdout.write(json.dumps(schema_dict, indent=indent)) + self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True)) else: self.save_file(out, schema_dict, indent) diff --git a/graphene_django/tests/test_command.py b/graphene_django/tests/test_command.py index ff6e6e1..2ed4ce0 100644 --- a/graphene_django/tests/test_command.py +++ b/graphene_django/tests/test_command.py @@ -1,5 +1,5 @@ from django.core import management -from mock import patch +from mock import patch, mock_open from six import StringIO @@ -8,3 +8,15 @@ def test_generate_file_on_call_graphql_schema(savefile_mock, settings): out = StringIO() management.call_command("graphql_schema", schema="", stdout=out) 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"