Skip to content

Commit fcc491f

Browse files
emilgoldsmithjkimbo
authored andcommitted
Add watch option to graphql_schema (#656)
* Add watch option to graphql_schema * add documentation for grapql_schema --watch
1 parent a9a8d67 commit fcc491f

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

docs/introspection.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Advanced Usage
3535
The ``--indent`` option can be used to specify the number of indentation spaces to
3636
be used in the output. Defaults to `None` which displays all data on a single line.
3737

38+
The ``--watch`` option can be used to run ``./manage.py graphql_schema`` in watch mode, where it will automatically output a new schema every time there are file changes in your project
39+
3840
To simplify the command to ``./manage.py graphql_schema``, you can
3941
specify the parameters in your settings.py:
4042

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)