From 73c04f22c8400c17af377fc532bb73cca59d1c0b Mon Sep 17 00:00:00 2001 From: Kevin Abiera Date: Fri, 18 Dec 2015 20:41:28 +0800 Subject: [PATCH] Implement Django command for dumping schema.json * Implement Django `./manage.py graphql_schema` * Required to have 'graphene.django.contrib' in INSTALLED_APPS setting * Introduces two new optional settings GRAPHENE_SCHEMA and GRAPHENE_SCHEMA_OUTPUT * GRAPHENE_SCHEMA for the Django app containing the schema * GRAPHENE_SCHEMA_OUTPUT for the output file (defaults to schema.json) * Use `./manage.py graphql_schema -h` for help in command line --- .../contrib/django/management/__init__.py | 0 .../django/management/commands/__init__.py | 0 .../management/commands/graphql_schema.py | 38 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 graphene/contrib/django/management/__init__.py create mode 100644 graphene/contrib/django/management/commands/__init__.py create mode 100644 graphene/contrib/django/management/commands/graphql_schema.py diff --git a/graphene/contrib/django/management/__init__.py b/graphene/contrib/django/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/graphene/contrib/django/management/commands/__init__.py b/graphene/contrib/django/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/graphene/contrib/django/management/commands/graphql_schema.py b/graphene/contrib/django/management/commands/graphql_schema.py new file mode 100644 index 00000000..35eb2772 --- /dev/null +++ b/graphene/contrib/django/management/commands/graphql_schema.py @@ -0,0 +1,38 @@ +from django.core.management.base import BaseCommand, CommandError + +import importlib +import json + + +class Command(BaseCommand): + help = 'Dump Graphene schema JSON to file' + can_import_settings = True + + def add_arguments(self, parser): + from django.conf import settings + parser.add_argument( + '--schema', + type=str, + dest='schema', + default=getattr(settings, 'GRAPHENE_SCHEMA', ''), + help='Django app containing schema to dump, e.g. myproject.core.schema') + + parser.add_argument( + '--out', + type=str, + dest='out', + default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), + help='Output file (default: schema.json)') + + def handle(self, *args, **options): + schema_module = options['schema'] + if schema_module == '': + raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema') + i = importlib.import_module(schema_module) + + schema_dict = {'data': i.schema.introspect()} + + with open(options['out'], 'w') as outfile: + json.dump(schema_dict, outfile) + + self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out']))