3
3
from sqlalchemy .dialects import postgresql
4
4
from sqlalchemy .orm import interfaces
5
5
6
- from graphene import (ID , Boolean , Dynamic , Enum , Field , Float , Int , List ,
7
- String )
6
+ from graphene import ID , Boolean , Dynamic , Enum , Field , Float , Int , List , String
8
7
from graphene .types .json import JSONString
9
8
10
9
from .fields import createConnectionField
11
10
12
11
try :
13
- from sqlalchemy_utils import (
14
- ChoiceType , JSONType , ScalarListType , TSVectorType )
12
+ from sqlalchemy_utils import ChoiceType , JSONType , ScalarListType , TSVectorType
15
13
except ImportError :
16
14
ChoiceType = JSONType = ScalarListType = TSVectorType = object
17
15
18
16
19
17
def get_column_doc (column ):
20
- return getattr (column , ' doc' , None )
18
+ return getattr (column , " doc" , None )
21
19
22
20
23
21
def is_column_nullable (column ):
24
- return bool (getattr (column , ' nullable' , True ))
22
+ return bool (getattr (column , " nullable" , True ))
25
23
26
24
27
25
def convert_sqlalchemy_relationship (relationship , registry ):
@@ -43,46 +41,51 @@ def dynamic_type():
43
41
44
42
45
43
def convert_sqlalchemy_hybrid_method (hybrid_item ):
46
- return String (description = getattr (hybrid_item , '__doc__' , None ),
47
- required = False )
44
+ return String (description = getattr (hybrid_item , "__doc__" , None ), required = False )
48
45
49
46
50
47
def convert_sqlalchemy_composite (composite , registry ):
51
48
converter = registry .get_converter_for_composite (composite .composite_class )
52
49
if not converter :
53
50
try :
54
51
raise Exception (
55
- "Don't know how to convert the composite field %s (%s)" %
56
- (composite , composite .composite_class ))
52
+ "Don't know how to convert the composite field %s (%s)"
53
+ % (composite , composite .composite_class )
54
+ )
57
55
except AttributeError :
58
56
# handle fields that are not attached to a class yet (don't have a parent)
59
57
raise Exception (
60
- "Don't know how to convert the composite field %r (%s)" %
61
- (composite , composite .composite_class ))
58
+ "Don't know how to convert the composite field %r (%s)"
59
+ % (composite , composite .composite_class )
60
+ )
62
61
return converter (composite , registry )
63
62
64
63
65
64
def _register_composite_class (cls , registry = None ):
66
65
if registry is None :
67
66
from .registry import get_global_registry
67
+
68
68
registry = get_global_registry ()
69
69
70
70
def inner (fn ):
71
71
registry .register_composite_converter (cls , fn )
72
+
72
73
return inner
73
74
74
75
75
76
convert_sqlalchemy_composite .register = _register_composite_class
76
77
77
78
78
79
def convert_sqlalchemy_column (column , registry = None ):
79
- return convert_sqlalchemy_type (getattr (column , ' type' , None ), column , registry )
80
+ return convert_sqlalchemy_type (getattr (column , " type" , None ), column , registry )
80
81
81
82
82
83
@singledispatch
83
84
def convert_sqlalchemy_type (type , column , registry = None ):
84
85
raise Exception (
85
- "Don't know how to convert the SQLAlchemy field %s (%s)" % (column , column .__class__ ))
86
+ "Don't know how to convert the SQLAlchemy field %s (%s)"
87
+ % (column , column .__class__ )
88
+ )
86
89
87
90
88
91
@convert_sqlalchemy_type .register (types .Date )
@@ -96,40 +99,49 @@ def convert_sqlalchemy_type(type, column, registry=None):
96
99
@convert_sqlalchemy_type .register (postgresql .CIDR )
97
100
@convert_sqlalchemy_type .register (TSVectorType )
98
101
def convert_column_to_string (type , column , registry = None ):
99
- return String (description = get_column_doc (column ),
100
- required = not (is_column_nullable (column )))
102
+ return String (
103
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
104
+ )
101
105
102
106
103
107
@convert_sqlalchemy_type .register (types .DateTime )
104
108
def convert_column_to_datetime (type , column , registry = None ):
105
109
from graphene .types .datetime import DateTime
106
- return DateTime (description = get_column_doc (column ),
107
- required = not (is_column_nullable (column )))
110
+
111
+ return DateTime (
112
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
113
+ )
108
114
109
115
110
116
@convert_sqlalchemy_type .register (types .SmallInteger )
111
117
@convert_sqlalchemy_type .register (types .Integer )
112
118
def convert_column_to_int_or_id (type , column , registry = None ):
113
119
if column .primary_key :
114
- return ID (description = get_column_doc (column ),
115
- required = not (is_column_nullable (column )))
120
+ return ID (
121
+ description = get_column_doc (column ),
122
+ required = not (is_column_nullable (column )),
123
+ )
116
124
else :
117
- return Int (description = get_column_doc (column ),
118
- required = not (is_column_nullable (column )))
125
+ return Int (
126
+ description = get_column_doc (column ),
127
+ required = not (is_column_nullable (column )),
128
+ )
119
129
120
130
121
131
@convert_sqlalchemy_type .register (types .Boolean )
122
132
def convert_column_to_boolean (type , column , registry = None ):
123
- return Boolean (description = get_column_doc (column ),
124
- required = not (is_column_nullable (column )))
133
+ return Boolean (
134
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
135
+ )
125
136
126
137
127
138
@convert_sqlalchemy_type .register (types .Float )
128
139
@convert_sqlalchemy_type .register (types .Numeric )
129
140
@convert_sqlalchemy_type .register (types .BigInteger )
130
141
def convert_column_to_float (type , column , registry = None ):
131
- return Float (description = get_column_doc (column ),
132
- required = not (is_column_nullable (column )))
142
+ return Float (
143
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
144
+ )
133
145
134
146
135
147
@convert_sqlalchemy_type .register (types .Enum )
@@ -138,14 +150,16 @@ def convert_enum_to_enum(type, column, registry=None):
138
150
items = type .enum_class .__members__ .items ()
139
151
except AttributeError :
140
152
items = zip (type .enums , type .enums )
141
- return Field (Enum (type .name , items ),
142
- description = get_column_doc (column ),
143
- required = not (is_column_nullable (column )))
153
+ return Field (
154
+ Enum (type .name , items ),
155
+ description = get_column_doc (column ),
156
+ required = not (is_column_nullable (column )),
157
+ )
144
158
145
159
146
160
@convert_sqlalchemy_type .register (ChoiceType )
147
161
def convert_column_to_enum (type , column , registry = None ):
148
- name = ' {}_{}' .format (column .table .name , column .name ).upper ()
162
+ name = " {}_{}" .format (column .table .name , column .name ).upper ()
149
163
return Enum (name , type .choices , description = get_column_doc (column ))
150
164
151
165
@@ -158,19 +172,24 @@ def convert_scalar_list_to_list(type, column, registry=None):
158
172
def convert_postgres_array_to_list (_type , column , registry = None ):
159
173
graphene_type = convert_sqlalchemy_type (column .type .item_type , column )
160
174
inner_type = type (graphene_type )
161
- return List (inner_type , description = get_column_doc (column ),
162
- required = not (is_column_nullable (column )))
175
+ return List (
176
+ inner_type ,
177
+ description = get_column_doc (column ),
178
+ required = not (is_column_nullable (column )),
179
+ )
163
180
164
181
165
182
@convert_sqlalchemy_type .register (postgresql .HSTORE )
166
183
@convert_sqlalchemy_type .register (postgresql .JSON )
167
184
@convert_sqlalchemy_type .register (postgresql .JSONB )
168
185
def convert_json_to_string (type , column , registry = None ):
169
- return JSONString (description = get_column_doc (column ),
170
- required = not (is_column_nullable (column )))
186
+ return JSONString (
187
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
188
+ )
171
189
172
190
173
191
@convert_sqlalchemy_type .register (JSONType )
174
192
def convert_json_type_to_string (type , column , registry = None ):
175
- return JSONString (description = get_column_doc (column ),
176
- required = not (is_column_nullable (column )))
193
+ return JSONString (
194
+ description = get_column_doc (column ), required = not (is_column_nullable (column ))
195
+ )
0 commit comments