Improved Django Compatibility. Added graphql_schema command tests #73

This commit is contained in:
Syrus Akbary 2016-01-03 17:17:56 +01:00
parent 70a4bd13c1
commit 84dbc58115
3 changed files with 49 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import importlib import importlib
import json import json
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
@ -8,6 +9,23 @@ class Command(BaseCommand):
help = 'Dump Graphene schema JSON to file' help = 'Dump Graphene schema JSON to file'
can_import_settings = True can_import_settings = True
option_list = BaseCommand.option_list + (
make_option(
'--schema',
type=str,
dest='schema',
default='',
help='Django app containing schema to dump, e.g. myproject.core.schema',
),
make_option(
'--out',
type=str,
dest='out',
default='',
help='Output file (default: schema.json)'
),
)
def add_arguments(self, parser): def add_arguments(self, parser):
from django.conf import settings from django.conf import settings
parser.add_argument( parser.add_argument(
@ -24,15 +42,23 @@ class Command(BaseCommand):
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
help='Output file (default: schema.json)') help='Output file (default: schema.json)')
def handle(self, *args, **options): def save_file(self, out, schema_dict):
schema_module = options['schema'] with open(out, 'w') as outfile:
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) json.dump(schema_dict, outfile)
self.stdout.write(self.style.SUCCESS('Successfully dumped GraphQL schema to %s' % options['out'])) def handle(self, *args, **options):
from django.conf import settings
schema = options.get('schema') or getattr(settings, 'GRAPHENE_SCHEMA', '')
out = options.get('out') or getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json')
if schema == '':
raise CommandError('Specify schema on GRAPHENE_SCHEMA setting or by using --schema')
i = importlib.import_module(schema)
schema_dict = {'data': i.schema.introspect()}
self.save_file(out, schema_dict)
style = getattr(self, 'style', None)
SUCCESS = getattr(style, 'SUCCESS', lambda x: x)
self.stdout.write(SUCCESS('Successfully dumped GraphQL schema to %s' % out))

View File

@ -0,0 +1,12 @@
from mock import patch
from six import StringIO
from django.core import management
@patch('graphene.contrib.django.management.commands.graphql_schema.Command.save_file')
def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
settings.GRAPHENE_SCHEMA = 'graphene.contrib.django.tests.test_urls'
out = StringIO()
management.call_command('graphql_schema', schema='', stdout=out)
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()

View File

@ -1,6 +1,7 @@
SECRET_KEY = 1 SECRET_KEY = 1
INSTALLED_APPS = [ INSTALLED_APPS = [
'graphene.contrib.django',
'graphene.contrib.django.tests', 'graphene.contrib.django.tests',
'examples.starwars_django', 'examples.starwars_django',
] ]