Skip to content

Commit 044d86f

Browse files
committed
Add watch option to graphql_schema
1 parent 0916e03 commit 044d86f

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

graphene_django/debug/tests/test_query.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ def resolve_reporter(self, info, **args):
5050
"""
5151
expected = {
5252
"reporter": {"lastName": "ABA"},
53-
"_debug": {
54-
"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]
55-
},
53+
"_debug": {"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]},
5654
}
5755
schema = graphene.Schema(query=Query)
5856
result = schema.execute(

graphene_django/management/commands/graphql_schema.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import importlib
22
import json
3+
import functools
34

45
from django.core.management.base import BaseCommand, CommandError
6+
from django.utils import autoreload
57

68
from graphene_django.settings import graphene_settings
79

@@ -32,6 +34,14 @@ def add_arguments(self, parser):
3234
help="Output file indent (default: None)",
3335
)
3436

37+
parser.add_argument(
38+
"--watch",
39+
dest="watch",
40+
default=False,
41+
action="store_true",
42+
help="Updates the schema on file changes (default: False)",
43+
)
44+
3545

3646
class Command(CommandArguments):
3747
help = "Dump Graphene schema JSON to file"
@@ -41,6 +51,18 @@ def save_file(self, out, schema_dict, indent):
4151
with open(out, "w") as outfile:
4252
json.dump(schema_dict, outfile, indent=indent, sort_keys=True)
4353

54+
def get_schema(self, schema, out, indent):
55+
schema_dict = {"data": schema.introspect()}
56+
if out == "-":
57+
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
58+
else:
59+
self.save_file(out, schema_dict, indent)
60+
61+
style = getattr(self, "style", None)
62+
success = getattr(style, "SUCCESS", lambda x: x)
63+
64+
self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))
65+
4466
def handle(self, *args, **options):
4567
options_schema = options.get("schema")
4668

@@ -63,13 +85,10 @@ def handle(self, *args, **options):
6385
)
6486

6587
indent = options.get("indent")
66-
schema_dict = {"data": schema.introspect()}
67-
if out == "-":
68-
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
88+
watch = options.get("watch")
89+
if watch:
90+
autoreload.run_with_reloader(
91+
functools.partial(self.get_schema, schema, out, indent)
92+
)
6993
else:
70-
self.save_file(out, schema_dict, indent)
71-
72-
style = getattr(self, "style", None)
73-
success = getattr(style, "SUCCESS", lambda x: x)
74-
75-
self.stdout.write(success("Successfully dumped GraphQL schema to %s" % out))
94+
self.get_schema(schema, out, indent)

0 commit comments

Comments
 (0)