Skip to content

Commit 062c0f9

Browse files
committed
Merge pull request #73 from lightning18/dump-schema
Implement Django command for dumping schema.json
2 parents 9b8c75b + 73c04f2 commit 062c0f9

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

graphene/contrib/django/management/__init__.py

Whitespace-only changes.

graphene/contrib/django/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
3+
import importlib
4+
import json
5+
6+
7+
class Command(BaseCommand):
8+
help = 'Dump Graphene schema JSON to file'
9+
can_import_settings = True
10+
11+
def add_arguments(self, parser):
12+
from django.conf import settings
13+
parser.add_argument(
14+
'--schema',
15+
type=str,
16+
dest='schema',
17+
default=getattr(settings, 'GRAPHENE_SCHEMA', ''),
18+
help='Django app containing schema to dump, e.g. myproject.core.schema')
19+
20+
parser.add_argument(
21+
'--out',
22+
type=str,
23+
dest='out',
24+
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
25+
help='Output file (default: schema.json)')
26+
27+
def handle(self, *args, **options):
28+
schema_module = options['schema']
29+
if schema_module == '':
30+
raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
31+
i = importlib.import_module(schema_module)
32+
33+
schema_dict = {'data': i.schema.introspect()}
34+
35+
with open(options['out'], 'w') as outfile:
36+
json.dump(schema_dict, outfile)
37+
38+
self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out']))

0 commit comments

Comments
 (0)