Skip to content

Commit 84dbc58

Browse files
committed
Improved Django Compatibility. Added graphql_schema command tests #73
1 parent 70a4bd1 commit 84dbc58

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import json
3+
from optparse import make_option
34

45
from django.core.management.base import BaseCommand, CommandError
56

@@ -8,6 +9,23 @@ class Command(BaseCommand):
89
help = 'Dump Graphene schema JSON to file'
910
can_import_settings = True
1011

12+
option_list = BaseCommand.option_list + (
13+
make_option(
14+
'--schema',
15+
type=str,
16+
dest='schema',
17+
default='',
18+
help='Django app containing schema to dump, e.g. myproject.core.schema',
19+
),
20+
make_option(
21+
'--out',
22+
type=str,
23+
dest='out',
24+
default='',
25+
help='Output file (default: schema.json)'
26+
),
27+
)
28+
1129
def add_arguments(self, parser):
1230
from django.conf import settings
1331
parser.add_argument(
@@ -24,15 +42,23 @@ def add_arguments(self, parser):
2442
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
2543
help='Output file (default: schema.json)')
2644

45+
def save_file(self, out, schema_dict):
46+
with open(out, 'w') as outfile:
47+
json.dump(schema_dict, outfile)
48+
2749
def handle(self, *args, **options):
28-
schema_module = options['schema']
29-
if schema_module == '':
50+
from django.conf import settings
51+
schema = options.get('schema') or getattr(settings, 'GRAPHENE_SCHEMA', '')
52+
out = options.get('out') or getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json')
53+
54+
if schema == '':
3055
raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
31-
i = importlib.import_module(schema_module)
56+
i = importlib.import_module(schema)
3257

3358
schema_dict = {'data': i.schema.introspect()}
59+
self.save_file(out, schema_dict)
3460

35-
with open(options['out'], 'w') as outfile:
36-
json.dump(schema_dict, outfile)
61+
style = getattr(self, 'style', None)
62+
SUCCESS = getattr(style, 'SUCCESS', lambda x: x)
3763

38-
self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out']))
64+
self.stdout.write(SUCCESS('Successfully dumped GraphQL schema to %s' % out))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mock import patch
2+
from six import StringIO
3+
4+
from django.core import management
5+
6+
7+
@patch('graphene.contrib.django.management.commands.graphql_schema.Command.save_file')
8+
def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
9+
settings.GRAPHENE_SCHEMA = 'graphene.contrib.django.tests.test_urls'
10+
out = StringIO()
11+
management.call_command('graphql_schema', schema='', stdout=out)
12+
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()

tests/django_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
SECRET_KEY = 1
22

33
INSTALLED_APPS = [
4+
'graphene.contrib.django',
45
'graphene.contrib.django.tests',
56
'examples.starwars_django',
67
]

0 commit comments

Comments
 (0)