1
1
import importlib
2
2
import json
3
+ import functools
3
4
4
5
from django .core .management .base import BaseCommand , CommandError
6
+ from django .utils import autoreload
5
7
6
8
from graphene_django .settings import graphene_settings
7
9
@@ -32,6 +34,14 @@ def add_arguments(self, parser):
32
34
help = "Output file indent (default: None)" ,
33
35
)
34
36
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
+
35
45
36
46
class Command (CommandArguments ):
37
47
help = "Dump Graphene schema JSON to file"
@@ -41,6 +51,18 @@ def save_file(self, out, schema_dict, indent):
41
51
with open (out , "w" ) as outfile :
42
52
json .dump (schema_dict , outfile , indent = indent , sort_keys = True )
43
53
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
+
44
66
def handle (self , * args , ** options ):
45
67
options_schema = options .get ("schema" )
46
68
@@ -63,13 +85,10 @@ def handle(self, *args, **options):
63
85
)
64
86
65
87
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
+ )
69
93
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