1
1
import importlib
2
2
import json
3
+ from optparse import make_option
3
4
4
5
from django .core .management .base import BaseCommand , CommandError
5
6
@@ -8,6 +9,23 @@ class Command(BaseCommand):
8
9
help = 'Dump Graphene schema JSON to file'
9
10
can_import_settings = True
10
11
12
+ option_list = BaseCommand .option_list + (
13
+ make_option (
14
+ '--schema' ,
15
+ type = str ,
16
+ dest = 'schema' ,
17
+ default = '' ,
18
+ help = 'Django app containing schema to dump, e.g. myproject.core.schema' ,
19
+ ),
20
+ make_option (
21
+ '--out' ,
22
+ type = str ,
23
+ dest = 'out' ,
24
+ default = '' ,
25
+ help = 'Output file (default: schema.json)'
26
+ ),
27
+ )
28
+
11
29
def add_arguments (self , parser ):
12
30
from django .conf import settings
13
31
parser .add_argument (
@@ -24,15 +42,23 @@ def add_arguments(self, parser):
24
42
default = getattr (settings , 'GRAPHENE_SCHEMA_OUTPUT' , 'schema.json' ),
25
43
help = 'Output file (default: schema.json)' )
26
44
45
+ def save_file (self , out , schema_dict ):
46
+ with open (out , 'w' ) as outfile :
47
+ json .dump (schema_dict , outfile )
48
+
27
49
def handle (self , * args , ** options ):
28
- schema_module = options ['schema' ]
29
- if schema_module == '' :
50
+ from django .conf import settings
51
+ schema = options .get ('schema' ) or getattr (settings , 'GRAPHENE_SCHEMA' , '' )
52
+ out = options .get ('out' ) or getattr (settings , 'GRAPHENE_SCHEMA_OUTPUT' , 'schema.json' )
53
+
54
+ if schema == '' :
30
55
raise CommandError ('Specify schema on GRAPHENE_SCHEMA setting or by using --schema' )
31
- i = importlib .import_module (schema_module )
56
+ i = importlib .import_module (schema )
32
57
33
58
schema_dict = {'data' : i .schema .introspect ()}
59
+ self .save_file (out , schema_dict )
34
60
35
- with open ( options [ 'out' ] , 'w' ) as outfile :
36
- json . dump ( schema_dict , outfile )
61
+ style = getattr ( self , 'style' , None )
62
+ SUCCESS = getattr ( style , 'SUCCESS' , lambda x : x )
37
63
38
- self .stdout .write (self . style . SUCCESS ('Successfully dumped GraphQL schema to %s' % options [ ' out' ] ))
64
+ self .stdout .write (SUCCESS ('Successfully dumped GraphQL schema to %s' % out ))
0 commit comments