graphene-django/graphene_django/management/commands/graphql_schema.py

95 lines
2.9 KiB
Python
Raw Normal View History

import importlib
import json
import functools
from django.core.management.base import BaseCommand, CommandError
from django.utils import autoreload
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(
2018-07-20 02:51:33 +03:00
"--schema",
2017-12-10 08:53:13 +03:00
type=str,
2018-07-20 02:51:33 +03:00
dest="schema",
2017-12-10 08:53:13 +03:00
default=graphene_settings.SCHEMA,
2018-07-20 02:51:33 +03:00
help="Django app containing schema to dump, e.g. myproject.core.schema.schema",
)
2017-12-10 08:53:13 +03:00
parser.add_argument(
2018-07-20 02:51:33 +03:00
"--out",
2017-12-10 08:53:13 +03:00
type=str,
2018-07-20 02:51:33 +03:00
dest="out",
2017-12-10 08:53:13 +03:00
default=graphene_settings.SCHEMA_OUTPUT,
help="Output file, --out=- prints to stdout (default: schema.json)",
2018-07-20 02:51:33 +03:00
)
2017-12-10 08:53:13 +03:00
parser.add_argument(
2018-07-20 02:51:33 +03:00
"--indent",
2017-12-10 08:53:13 +03:00
type=int,
2018-07-20 02:51:33 +03:00
dest="indent",
2017-12-10 08:53:13 +03:00
default=graphene_settings.SCHEMA_INDENT,
2018-07-20 02:51:33 +03:00
help="Output file indent (default: None)",
)
parser.add_argument(
"--watch",
dest="watch",
default=False,
action="store_true",
help="Updates the schema on file changes (default: False)",
)
class Command(CommandArguments):
2018-07-20 02:51:33 +03:00
help = "Dump Graphene schema JSON to file"
can_import_settings = True
def save_file(self, out, schema_dict, indent):
2018-07-20 02:51:33 +03:00
with open(out, "w") as outfile:
json.dump(schema_dict, outfile, indent=indent, sort_keys=True)
def get_schema(self, schema, out, indent):
schema_dict = {"data": schema.introspect()}
if out == "-":
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
else:
self.save_file(out, schema_dict, indent)
style = getattr(self, "style", None)
success = getattr(style, "SUCCESS", lambda x: x)
self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))
def handle(self, *args, **options):
2018-07-20 02:51:33 +03:00
options_schema = options.get("schema")
if options_schema and type(options_schema) is str:
2018-07-20 02:51:33 +03:00
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
else:
schema = graphene_settings.SCHEMA
2018-07-20 02:51:33 +03:00
out = options.get("out") or graphene_settings.SCHEMA_OUTPUT
if not schema:
2018-07-20 02:51:33 +03:00
raise CommandError(
"Specify schema on GRAPHENE.SCHEMA setting or by using --schema"
)
2018-07-20 02:51:33 +03:00
indent = options.get("indent")
watch = options.get("watch")
if watch:
autoreload.run_with_reloader(
functools.partial(self.get_schema, schema, out, indent)
)
else:
self.get_schema(schema, out, indent)