@@ -23,6 +23,14 @@ class JSONType(object):
23
23
pass
24
24
25
25
26
+ def get_column_doc (column ):
27
+ return getattr (column , 'doc' , None )
28
+
29
+
30
+ def is_column_nullable (column ):
31
+ return bool (getattr (column , 'nullable' , False ))
32
+
33
+
26
34
def convert_sqlalchemy_relationship (relationship , registry ):
27
35
direction = relationship .direction
28
36
model = relationship .mapper .entity
@@ -90,64 +98,64 @@ def convert_sqlalchemy_type(type, column, registry=None):
90
98
@convert_sqlalchemy_type .register (postgresql .ENUM )
91
99
@convert_sqlalchemy_type .register (postgresql .UUID )
92
100
def convert_column_to_string (type , column , registry = None ):
93
- return String (description = getattr (column , 'doc' , None ),
94
- required = not (getattr (column , 'nullable' , True )))
101
+ return String (description = get_column_doc (column ),
102
+ required = not (is_column_nullable (column )))
95
103
96
104
97
105
@convert_sqlalchemy_type .register (types .DateTime )
98
106
def convert_column_to_datetime (type , column , registry = None ):
99
107
from graphene .types .datetime import DateTime
100
- return DateTime (description = getattr (column , 'doc' , None ),
101
- required = not (getattr (column , 'nullable' , True )))
108
+ return DateTime (description = get_column_doc (column ),
109
+ required = not (is_column_nullable (column )))
102
110
103
111
104
112
@convert_sqlalchemy_type .register (types .SmallInteger )
105
113
@convert_sqlalchemy_type .register (types .Integer )
106
114
def convert_column_to_int_or_id (type , column , registry = None ):
107
115
if column .primary_key :
108
- return ID (description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
116
+ return ID (description = get_column_doc (column ), required = not (is_column_nullable (column )))
109
117
else :
110
- return Int (description = getattr (column , 'doc' , None ),
111
- required = not (getattr (column , 'nullable' , True )))
118
+ return Int (description = get_column_doc (column ),
119
+ required = not (is_column_nullable (column )))
112
120
113
121
114
122
@convert_sqlalchemy_type .register (types .Boolean )
115
123
def convert_column_to_boolean (type , column , registry = None ):
116
- return Boolean (description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
124
+ return Boolean (description = get_column_doc (column ), required = not (is_column_nullable (column )))
117
125
118
126
119
127
@convert_sqlalchemy_type .register (types .Float )
120
128
@convert_sqlalchemy_type .register (types .Numeric )
121
129
@convert_sqlalchemy_type .register (types .BigInteger )
122
130
def convert_column_to_float (type , column , registry = None ):
123
- return Float (description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
131
+ return Float (description = get_column_doc (column ), required = not (is_column_nullable (column )))
124
132
125
133
126
134
@convert_sqlalchemy_type .register (ChoiceType )
127
135
def convert_column_to_enum (type , column , registry = None ):
128
136
name = '{}_{}' .format (column .table .name , column .name ).upper ()
129
- return Enum (name , type .choices , description = getattr (column , 'doc' , None ))
137
+ return Enum (name , type .choices , description = get_column_doc (column ))
130
138
131
139
132
140
@convert_sqlalchemy_type .register (ScalarListType )
133
141
def convert_scalar_list_to_list (type , column , registry = None ):
134
- return List (String , description = getattr (column , 'doc' , None ))
142
+ return List (String , description = get_column_doc (column ))
135
143
136
144
137
145
@convert_sqlalchemy_type .register (postgresql .ARRAY )
138
146
def convert_postgres_array_to_list (_type , column , registry = None ):
139
147
graphene_type = convert_sqlalchemy_type (column .type .item_type , column )
140
148
inner_type = type (graphene_type )
141
- return List (inner_type , description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
149
+ return List (inner_type , description = get_column_doc (column ), required = not (is_column_nullable (column )))
142
150
143
151
144
152
@convert_sqlalchemy_type .register (postgresql .HSTORE )
145
153
@convert_sqlalchemy_type .register (postgresql .JSON )
146
154
@convert_sqlalchemy_type .register (postgresql .JSONB )
147
155
def convert_json_to_string (type , column , registry = None ):
148
- return JSONString (description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
156
+ return JSONString (description = get_column_doc (column ), required = not (is_column_nullable (column )))
149
157
150
158
151
159
@convert_sqlalchemy_type .register (JSONType )
152
160
def convert_json_type_to_string (type , column , registry = None ):
153
- return JSONString (description = getattr (column , 'doc' , None ), required = not (getattr (column , 'nullable' , None )))
161
+ return JSONString (description = get_column_doc (column ), required = not (is_column_nullable (column )))
0 commit comments