|
1 | 1 | from __future__ import unicode_literals |
2 | 2 |
|
3 | | -import enum |
4 | | -import inspect |
5 | 3 | from collections import OrderedDict |
6 | 4 | from concurrent.futures import ThreadPoolExecutor |
| 5 | +import enum |
| 6 | +import inspect |
7 | 7 | from typing import Any, Callable, Union |
8 | 8 |
|
9 | | -import mongoengine |
10 | 9 | from asgiref.sync import SyncToAsync |
11 | 10 | from asgiref.sync import sync_to_async as asgiref_sync_to_async |
12 | 11 | from graphene import Node |
|
19 | 18 | VariableNode, |
20 | 19 | ) |
21 | 20 | from graphql_relay.connection.array_connection import offset_to_cursor |
| 21 | +import mongoengine |
22 | 22 |
|
23 | 23 |
|
24 | 24 | class ExecutorEnum(enum.Enum): |
@@ -163,19 +163,15 @@ def collect_query_fields(node, fragments, variables): |
163 | 163 | for leaf in selection_set.selections: |
164 | 164 | if leaf.kind == "field": |
165 | 165 | if include_field_by_directives(leaf, variables): |
166 | | - field.update( |
167 | | - {leaf.name.value: collect_query_fields(leaf, fragments, variables)} |
168 | | - ) |
| 166 | + field.update({ |
| 167 | + leaf.name.value: collect_query_fields(leaf, fragments, variables) |
| 168 | + }) |
169 | 169 | elif leaf.kind == "fragment_spread": |
170 | 170 | field.update(collect_query_fields(fragments[leaf.name.value], fragments, variables)) |
171 | 171 | elif leaf.kind == "inline_fragment": |
172 | | - field.update( |
173 | | - { |
174 | | - leaf.type_condition.name.value: collect_query_fields( |
175 | | - leaf, fragments, variables |
176 | | - ) |
177 | | - } |
178 | | - ) |
| 172 | + field.update({ |
| 173 | + leaf.type_condition.name.value: collect_query_fields(leaf, fragments, variables) |
| 174 | + }) |
179 | 175 |
|
180 | 176 | return field |
181 | 177 |
|
@@ -227,26 +223,22 @@ def collect_query_fields_for_union(node, fragments, variables): |
227 | 223 | for leaf in selection_set.selections: |
228 | 224 | if leaf.kind == "field": |
229 | 225 | if include_field_by_directives(leaf, variables): |
230 | | - field.update( |
231 | | - {leaf.name.value: collect_query_fields(leaf, fragments, variables)} |
232 | | - ) |
| 226 | + field.update({ |
| 227 | + leaf.name.value: collect_query_fields(leaf, fragments, variables) |
| 228 | + }) |
233 | 229 | elif leaf.kind == "fragment_spread": # This is different |
234 | 230 | fragment = fragments[leaf.name.value] |
235 | | - field.update( |
236 | | - { |
237 | | - fragment.type_condition.name.value: collect_query_fields( |
238 | | - fragment, fragments, variables |
239 | | - ) |
240 | | - } |
241 | | - ) |
| 231 | + field.update({ |
| 232 | + fragment.type_condition.name.value: collect_query_fields( |
| 233 | + fragment, fragments, variables |
| 234 | + ) |
| 235 | + }) |
242 | 236 | elif leaf.kind == "inline_fragment": |
243 | | - field.update( |
244 | | - { |
245 | | - leaf.type_condition.name.value: collect_query_fields( |
246 | | - leaf, fragments, variables |
247 | | - ) |
248 | | - } |
249 | | - ) |
| 237 | + field.update({ |
| 238 | + leaf.type_condition.name.value: collect_query_fields( |
| 239 | + leaf, fragments, variables |
| 240 | + ) |
| 241 | + }) |
250 | 242 |
|
251 | 243 | return field |
252 | 244 |
|
@@ -421,3 +413,30 @@ def sync_to_async( |
421 | 413 | if executor is None: |
422 | 414 | executor = ThreadPoolExecutor() |
423 | 415 | return asgiref_sync_to_async(func=func, thread_sensitive=thread_sensitive, executor=executor) |
| 416 | + |
| 417 | + |
| 418 | +def get_field_resolver( |
| 419 | + field_resolver: Callable | None, |
| 420 | + default_async_resolver: Callable, |
| 421 | + default_sync_resolver: Callable, |
| 422 | + executor: ExecutorEnum, |
| 423 | +) -> Callable: |
| 424 | + """ |
| 425 | + Helpr function to get the resolver for a field |
| 426 | +
|
| 427 | + Args: |
| 428 | + field_resolver: user defined resolver (optional) |
| 429 | + default_async_resolver: default library async resolver |
| 430 | + default_sync_resolver: default library sync resolver |
| 431 | + executor: ExecutorEnum |
| 432 | +
|
| 433 | + Returns: |
| 434 | + resolver: Callable |
| 435 | + """ |
| 436 | + if field_resolver is not None: |
| 437 | + return field_resolver |
| 438 | + |
| 439 | + if executor == ExecutorEnum.ASYNC: |
| 440 | + return default_async_resolver |
| 441 | + |
| 442 | + return default_sync_resolver |
0 commit comments