|
9 | 9 | from graphene.types.utils import yank_fields_from_attrs
|
10 | 10 | from graphene.utils.str_converters import to_snake_case
|
11 | 11 |
|
| 12 | +from graphene_mongo import MongoengineConnectionField |
12 | 13 | from .converter import convert_mongoengine_field
|
13 | 14 | from .registry import Registry, get_global_registry, get_inputs_registry
|
14 | 15 | from .utils import get_model_fields, is_valid_mongoengine_model, get_query_fields
|
@@ -128,22 +129,29 @@ def __init_subclass_with_meta__(
|
128 | 129 | use_connection = any(
|
129 | 130 | (issubclass(interface, Node) for interface in interfaces)
|
130 | 131 | )
|
131 |
| - mongoengine_fields = yank_fields_from_attrs( |
132 |
| - converted_fields, _as=graphene.Field |
133 |
| - ) |
134 |
| - if use_connection is None and interfaces: |
135 |
| - use_connection = any( |
136 |
| - (issubclass(interface, Node) for interface in interfaces) |
137 |
| - ) |
138 | 132 |
|
139 |
| - if use_connection and not connection: |
140 |
| - # We create the connection automatically |
141 |
| - if not connection_class: |
142 |
| - connection_class = Connection |
| 133 | + if use_connection and not connection: |
| 134 | + # We create the connection automatically |
| 135 | + if not connection_class: |
| 136 | + connection_class = Connection |
143 | 137 |
|
144 |
| - connection = connection_class.create_type( |
145 |
| - "{}Connection".format(cls.__name__), node=cls |
146 |
| - ) |
| 138 | + connection = connection_class.create_type( |
| 139 | + "{}Connection".format(cls.__name__), node=cls |
| 140 | + ) |
| 141 | + |
| 142 | + if connection is not None: |
| 143 | + assert issubclass(connection, Connection), ( |
| 144 | + "The attribute connection in {}.Meta must be of type Connection. " |
| 145 | + 'Received "{}" instead.' |
| 146 | + ).format(cls.__name__, type(connection)) |
| 147 | + |
| 148 | + if connection_field_class is not None: |
| 149 | + assert issubclass(connection_field_class, graphene.ConnectionField), ( |
| 150 | + "The attribute connection_field_class in {}.Meta must be of type graphene.ConnectionField. " |
| 151 | + 'Received "{}" instead.' |
| 152 | + ).format(cls.__name__, type(connection_field_class)) |
| 153 | + else: |
| 154 | + connection_field_class = MongoengineConnectionField |
147 | 155 |
|
148 | 156 | if _meta:
|
149 | 157 | assert isinstance(_meta, MongoengineGenericObjectTypeOptions), (
|
@@ -176,7 +184,12 @@ def __init_subclass_with_meta__(
|
176 | 184 | converted_fields = construct_self_referenced_fields(
|
177 | 185 | self_referenced, registry
|
178 | 186 | )
|
179 |
| - |
| 187 | + if converted_fields: |
| 188 | + mongoengine_fields = yank_fields_from_attrs( |
| 189 | + converted_fields, _as=graphene.Field |
| 190 | + ) |
| 191 | + cls._meta.fields.update(mongoengine_fields) |
| 192 | + registry.register(cls) |
180 | 193 | @classmethod
|
181 | 194 | def rescan_fields(cls):
|
182 | 195 | """Attempts to rescan fields and will insert any not converted initially"""
|
|
0 commit comments