2016-09-18 02:29:00 +03:00
|
|
|
import importlib
|
|
|
|
import json
|
|
|
|
from distutils.version import StrictVersion
|
|
|
|
from optparse import make_option
|
|
|
|
|
|
|
|
from django import get_version as get_django_version
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
from graphene_django.settings import graphene_settings
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
LT_DJANGO_1_8 = StrictVersion(get_django_version()) < StrictVersion('1.8')
|
|
|
|
|
|
|
|
if LT_DJANGO_1_8:
|
|
|
|
class CommandArguments(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option(
|
|
|
|
'--schema',
|
|
|
|
type=str,
|
|
|
|
dest='schema',
|
|
|
|
default='',
|
2016-09-20 08:04:23 +03:00
|
|
|
help='Django app containing schema to dump, e.g. myproject.core.schema.schema',
|
2016-09-18 02:29:00 +03:00
|
|
|
),
|
|
|
|
make_option(
|
|
|
|
'--out',
|
|
|
|
type=str,
|
|
|
|
dest='out',
|
|
|
|
default='',
|
|
|
|
help='Output file (default: schema.json)'
|
|
|
|
),
|
2016-10-17 10:08:04 +03:00
|
|
|
make_option(
|
|
|
|
'--indent',
|
|
|
|
type=int,
|
|
|
|
dest='indent',
|
|
|
|
default=None,
|
|
|
|
help='Output file indent (default: None)'
|
|
|
|
),
|
2016-09-18 02:29:00 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
class CommandArguments(BaseCommand):
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
'--schema',
|
|
|
|
type=str,
|
|
|
|
dest='schema',
|
2016-09-20 08:04:23 +03:00
|
|
|
default=graphene_settings.SCHEMA,
|
|
|
|
help='Django app containing schema to dump, e.g. myproject.core.schema.schema')
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--out',
|
|
|
|
type=str,
|
|
|
|
dest='out',
|
2016-09-20 08:04:23 +03:00
|
|
|
default=graphene_settings.SCHEMA_OUTPUT,
|
2016-09-18 02:29:00 +03:00
|
|
|
help='Output file (default: schema.json)')
|
|
|
|
|
2016-10-17 10:08:04 +03:00
|
|
|
parser.add_argument(
|
|
|
|
'--indent',
|
|
|
|
type=int,
|
|
|
|
dest='indent',
|
|
|
|
default=graphene_settings.SCHEMA_INDENT,
|
|
|
|
help='Output file indent (default: None)')
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Command(CommandArguments):
|
|
|
|
help = 'Dump Graphene schema JSON to file'
|
|
|
|
can_import_settings = True
|
|
|
|
|
2016-10-17 10:08:04 +03:00
|
|
|
def save_file(self, out, schema_dict, indent):
|
2016-09-18 02:29:00 +03:00
|
|
|
with open(out, 'w') as outfile:
|
2016-10-17 10:08:04 +03:00
|
|
|
json.dump(schema_dict, outfile, indent=indent)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-09-20 08:04:23 +03:00
|
|
|
options_schema = options.get('schema')
|
2016-09-22 00:57:47 +03:00
|
|
|
|
|
|
|
if options_schema and type(options_schema) is str:
|
|
|
|
module_str, schema_name = options_schema.rsplit('.', 1)
|
|
|
|
mod = importlib.import_module(module_str)
|
|
|
|
schema = getattr(mod, schema_name)
|
|
|
|
|
|
|
|
elif options_schema:
|
|
|
|
schema = options_schema
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
else:
|
|
|
|
schema = graphene_settings.SCHEMA
|
|
|
|
|
|
|
|
out = options.get('out') or graphene_settings.SCHEMA_OUTPUT
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
if not schema:
|
|
|
|
raise CommandError('Specify schema on GRAPHENE.SCHEMA setting or by using --schema')
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2016-10-17 10:08:04 +03:00
|
|
|
indent = options.get('indent')
|
2016-09-20 08:04:23 +03:00
|
|
|
schema_dict = {'data': schema.introspect()}
|
2016-10-17 10:08:04 +03:00
|
|
|
self.save_file(out, schema_dict, indent)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
style = getattr(self, 'style', None)
|
2016-09-18 03:09:56 +03:00
|
|
|
success = getattr(style, 'SUCCESS', lambda x: x)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2016-09-18 03:09:56 +03:00
|
|
|
self.stdout.write(success('Successfully dumped GraphQL schema to %s' % out))
|