add indent support for graphql_schema command

This commit is contained in:
cph 2016-10-17 15:08:04 +08:00
parent 60bcc0beef
commit 62d3eb911a
2 changed files with 19 additions and 3 deletions

View File

@ -27,6 +27,13 @@ if LT_DJANGO_1_8:
default='', default='',
help='Output file (default: schema.json)' help='Output file (default: schema.json)'
), ),
make_option(
'--indent',
type=int,
dest='indent',
default=None,
help='Output file indent (default: None)'
),
) )
else: else:
class CommandArguments(BaseCommand): class CommandArguments(BaseCommand):
@ -46,14 +53,21 @@ else:
default=graphene_settings.SCHEMA_OUTPUT, default=graphene_settings.SCHEMA_OUTPUT,
help='Output file (default: schema.json)') 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)')
class Command(CommandArguments): class Command(CommandArguments):
help = 'Dump Graphene schema JSON to file' help = 'Dump Graphene schema JSON to file'
can_import_settings = True can_import_settings = True
def save_file(self, out, schema_dict): def save_file(self, out, schema_dict, indent):
with open(out, 'w') as outfile: with open(out, 'w') as outfile:
json.dump(schema_dict, outfile) json.dump(schema_dict, outfile, indent=indent)
def handle(self, *args, **options): def handle(self, *args, **options):
options_schema = options.get('schema') options_schema = options.get('schema')
@ -74,8 +88,9 @@ class Command(CommandArguments):
if not schema: if not schema:
raise CommandError('Specify schema on GRAPHENE.SCHEMA setting or by using --schema') raise CommandError('Specify schema on GRAPHENE.SCHEMA setting or by using --schema')
indent = options.get('indent')
schema_dict = {'data': schema.introspect()} schema_dict = {'data': schema.introspect()}
self.save_file(out, schema_dict) self.save_file(out, schema_dict, indent)
style = getattr(self, 'style', None) style = getattr(self, 'style', None)
success = getattr(style, 'SUCCESS', lambda x: x) success = getattr(style, 'SUCCESS', lambda x: x)

View File

@ -28,6 +28,7 @@ except ImportError:
DEFAULTS = { DEFAULTS = {
'SCHEMA': None, 'SCHEMA': None,
'SCHEMA_OUTPUT': 'schema.json', 'SCHEMA_OUTPUT': 'schema.json',
'SCHEMA_INDENT': None,
'MIDDLEWARE': (), 'MIDDLEWARE': (),
} }