2016-09-18 02:29:00 +03:00
|
|
|
import importlib
|
|
|
|
import json
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
|
2016-09-20 08:04:23 +03:00
|
|
|
from graphene_django.settings import graphene_settings
|
|
|
|
|
2017-12-18 20:02:04 +03:00
|
|
|
|
2017-12-10 08:53:13 +03:00
|
|
|
class CommandArguments(BaseCommand):
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
'--schema',
|
|
|
|
type=str,
|
|
|
|
dest='schema',
|
|
|
|
default=graphene_settings.SCHEMA,
|
|
|
|
help='Django app containing schema to dump, e.g. myproject.core.schema.schema')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--out',
|
|
|
|
type=str,
|
|
|
|
dest='out',
|
|
|
|
default=graphene_settings.SCHEMA_OUTPUT,
|
|
|
|
help='Output file (default: schema.json)')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--indent',
|
|
|
|
type=int,
|
|
|
|
dest='indent',
|
|
|
|
default=graphene_settings.SCHEMA_INDENT,
|
|
|
|
help='Output file indent (default: None)')
|
2016-10-17 10:08:04 +03:00
|
|
|
|
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))
|