mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 09:36:44 +03:00
Improved Django Compatibility. Added graphql_schema command tests #73
This commit is contained in:
parent
70a4bd13c1
commit
84dbc58115
|
@ -1,5 +1,6 @@
|
|||
import importlib
|
||||
import json
|
||||
from optparse import make_option
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
|
@ -8,6 +9,23 @@ class Command(BaseCommand):
|
|||
help = 'Dump Graphene schema JSON to file'
|
||||
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):
|
||||
from django.conf import settings
|
||||
parser.add_argument(
|
||||
|
@ -24,15 +42,23 @@ class Command(BaseCommand):
|
|||
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:
|
||||
def save_file(self, out, schema_dict):
|
||||
with open(out, 'w') as 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))
|
||||
|
|
12
graphene/contrib/django/tests/test_command.py
Normal file
12
graphene/contrib/django/tests/test_command.py
Normal 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()
|
|
@ -1,6 +1,7 @@
|
|||
SECRET_KEY = 1
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'graphene.contrib.django',
|
||||
'graphene.contrib.django.tests',
|
||||
'examples.starwars_django',
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue
Block a user