11import importlib
22import json
3+ import functools
34
45from django .core .management .base import BaseCommand , CommandError
6+ from django .utils import autoreload
57
68from 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
3646class 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