<s> from ... core . exceptions import SkipField <EOL> from ... core . fields import Field <EOL> from ... core . types . base import FieldType <EOL> from ... core . types . definitions import List <EOL> from ... relay import ConnectionField <EOL> from ... relay . utils import is_node <EOL> from . utils import get_query , get_type_for_model , maybe_query <EOL> class DefaultQuery ( object ) : <EOL> pass <EOL> class SQLAlchemyConnectionField ( ConnectionField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:default>' ] = kwargs . pop ( '<STR_LIT:default>' , lambda : DefaultQuery ) <EOL> return super ( SQLAlchemyConnectionField , self ) . __init__ ( * args , ** kwargs ) <EOL> @ property <EOL> def model ( self ) : <EOL> return self . type . _meta . model <EOL> def from_list ( self , connection_type , resolved , args , info ) : <EOL> if resolved is DefaultQuery : <EOL> resolved = get_query ( self . model , info ) <EOL> query = maybe_query ( resolved ) <EOL> return super ( SQLAlchemyConnectionField , self ) . from_list ( connection_type , query , args , info ) <EOL> class ConnectionOrListField ( Field ) : <EOL> def internal_type ( self , schema ) : <EOL> model_field = self . type <EOL> field_object_type = model_field . get_object_type ( schema ) <EOL> if not field_object_type : <EOL> raise SkipField ( ) <EOL> if is_node ( field_object_type ) : <EOL> field = SQLAlchemyConnectionField ( field_object_type ) <EOL> else : <EOL> field = Field ( List ( field_object_type ) ) <EOL> field . contribute_to_class ( self . object_type , self . attname ) <EOL> return schema . T ( field ) <EOL> class SQLAlchemyModelField ( FieldType ) : <EOL> def __init__ ( self , model , * args , ** kwargs ) : <EOL> self . model = model <EOL> super ( SQLAlchemyModelField , self ) . __init__ ( * args , ** kwargs ) <EOL> def internal_type ( self , schema ) : <EOL> _type = self . get_object_type ( schema ) <EOL> if not _type and self . parent . _meta . only_fields : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . model , <EOL> self . parent , <EOL> ) <EOL> ) <EOL> if not _type : <EOL> raise SkipField ( ) <EOL> return schema . T ( _type ) <EOL> def get_object_type ( self , schema ) : <EOL> return get_type_for_model ( schema , self . model ) </s>
