Add watch option to graphql_schema

This commit is contained in:
Emil Goldsmith Olesen 2019-06-05 20:19:03 +02:00
parent 0916e03cb3
commit 044d86fd66
2 changed files with 29 additions and 12 deletions

View File

@ -50,9 +50,7 @@ def test_should_query_field():
""" """
expected = { expected = {
"reporter": {"lastName": "ABA"}, "reporter": {"lastName": "ABA"},
"_debug": { "_debug": {"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]},
"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]
},
} }
schema = graphene.Schema(query=Query) schema = graphene.Schema(query=Query)
result = schema.execute( result = schema.execute(

View File

@ -1,7 +1,9 @@
import importlib import importlib
import json import json
import functools
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.utils import autoreload
from graphene_django.settings import graphene_settings from graphene_django.settings import graphene_settings
@ -32,6 +34,14 @@ class CommandArguments(BaseCommand):
help="Output file indent (default: None)", 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): class Command(CommandArguments):
help = "Dump Graphene schema JSON to file" help = "Dump Graphene schema JSON to file"
@ -41,6 +51,18 @@ class Command(CommandArguments):
with open(out, "w") as outfile: with open(out, "w") as outfile:
json.dump(schema_dict, outfile, indent=indent, sort_keys=True) 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): def handle(self, *args, **options):
options_schema = options.get("schema") options_schema = options.get("schema")
@ -63,13 +85,10 @@ class Command(CommandArguments):
) )
indent = options.get("indent") indent = options.get("indent")
schema_dict = {"data": schema.introspect()} watch = options.get("watch")
if out == "-": if watch:
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True)) autoreload.run_with_reloader(
functools.partial(self.get_schema, schema, out, indent)
)
else: else:
self.save_file(out, schema_dict, indent) self.get_schema(schema, out, indent)
style = getattr(self, "style", None)
success = getattr(style, "SUCCESS", lambda x: x)
self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))