33from sqlalchemy .dialects import postgresql
44from sqlalchemy .orm import interfaces
55
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
87from graphene .types .json import JSONString
98
109from .fields import createConnectionField
1110
1211try :
13- from sqlalchemy_utils import (
14- ChoiceType , JSONType , ScalarListType , TSVectorType )
12+ from sqlalchemy_utils import ChoiceType , JSONType , ScalarListType , TSVectorType
1513except ImportError :
1614 ChoiceType = JSONType = ScalarListType = TSVectorType = object
1715
1816
1917def get_column_doc (column ):
20- return getattr (column , ' doc' , None )
18+ return getattr (column , " doc" , None )
2119
2220
2321def is_column_nullable (column ):
24- return bool (getattr (column , ' nullable' , True ))
22+ return bool (getattr (column , " nullable" , True ))
2523
2624
2725def convert_sqlalchemy_relationship (relationship , registry ):
@@ -43,46 +41,51 @@ def dynamic_type():
4341
4442
4543def 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 )
4845
4946
5047def convert_sqlalchemy_composite (composite , registry ):
5148 converter = registry .get_converter_for_composite (composite .composite_class )
5249 if not converter :
5350 try :
5451 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+ )
5755 except AttributeError :
5856 # handle fields that are not attached to a class yet (don't have a parent)
5957 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+ )
6261 return converter (composite , registry )
6362
6463
6564def _register_composite_class (cls , registry = None ):
6665 if registry is None :
6766 from .registry import get_global_registry
67+
6868 registry = get_global_registry ()
6969
7070 def inner (fn ):
7171 registry .register_composite_converter (cls , fn )
72+
7273 return inner
7374
7475
7576convert_sqlalchemy_composite .register = _register_composite_class
7677
7778
7879def 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 )
8081
8182
8283@singledispatch
8384def convert_sqlalchemy_type (type , column , registry = None ):
8485 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+ )
8689
8790
8891@convert_sqlalchemy_type .register (types .Date )
@@ -96,40 +99,49 @@ def convert_sqlalchemy_type(type, column, registry=None):
9699@convert_sqlalchemy_type .register (postgresql .CIDR )
97100@convert_sqlalchemy_type .register (TSVectorType )
98101def 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+ )
101105
102106
103107@convert_sqlalchemy_type .register (types .DateTime )
104108def convert_column_to_datetime (type , column , registry = None ):
105109 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+ )
108114
109115
110116@convert_sqlalchemy_type .register (types .SmallInteger )
111117@convert_sqlalchemy_type .register (types .Integer )
112118def convert_column_to_int_or_id (type , column , registry = None ):
113119 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+ )
116124 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+ )
119129
120130
121131@convert_sqlalchemy_type .register (types .Boolean )
122132def 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+ )
125136
126137
127138@convert_sqlalchemy_type .register (types .Float )
128139@convert_sqlalchemy_type .register (types .Numeric )
129140@convert_sqlalchemy_type .register (types .BigInteger )
130141def 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+ )
133145
134146
135147@convert_sqlalchemy_type .register (types .Enum )
@@ -138,14 +150,16 @@ def convert_enum_to_enum(type, column, registry=None):
138150 items = type .enum_class .__members__ .items ()
139151 except AttributeError :
140152 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+ )
144158
145159
146160@convert_sqlalchemy_type .register (ChoiceType )
147161def 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 ()
149163 return Enum (name , type .choices , description = get_column_doc (column ))
150164
151165
@@ -158,19 +172,24 @@ def convert_scalar_list_to_list(type, column, registry=None):
158172def convert_postgres_array_to_list (_type , column , registry = None ):
159173 graphene_type = convert_sqlalchemy_type (column .type .item_type , column )
160174 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+ )
163180
164181
165182@convert_sqlalchemy_type .register (postgresql .HSTORE )
166183@convert_sqlalchemy_type .register (postgresql .JSON )
167184@convert_sqlalchemy_type .register (postgresql .JSONB )
168185def 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+ )
171189
172190
173191@convert_sqlalchemy_type .register (JSONType )
174192def 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