Skip to content

Commit

Permalink
Merge remote-tracking branch 'strollby-fork/develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	graphene_mongo/fields_async.py
#	graphene_mongo/types.py
  • Loading branch information
abhinand-c committed Dec 9, 2023
2 parents c2d410c + 4676be8 commit 23557c6
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To create a GraphQL schema and async executor; for it you simply have to write t
import graphene

from graphene_mongo import AsyncMongoengineObjectType
from asgiref.sync import sync_to_async
from graphene_mongo.utils import sync_to_async
from concurrent.futures import ThreadPoolExecutor

from .models import User as UserModel
Expand Down
27 changes: 18 additions & 9 deletions graphene_mongo/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ async def reference_resolver_async(root, *args, **kwargs):
)
tasks.append(task)
result = await asyncio.gather(*tasks)
result = [each[0] for each in result]
result_object_ids = list()
for each in result:
result_object_ids.append(each.id)
result_object = {}
for items in result:
for item in items:
result_object[item.id] = item
ordered_result = list()
for each in to_resolve_object_ids:
ordered_result.append(result[result_object_ids.index(each)])
ordered_result.append(result_object[each])
return ordered_result
return None

Expand Down Expand Up @@ -350,10 +350,19 @@ def convert_field_to_union(field, registry=None, executor: ExecutorEnum = Execut
if len(_types) == 0:
return None

name = (
to_camel_case("{}_{}".format(field._owner_document.__name__, field.db_field)) + "UnionType"
if ExecutorEnum.SYNC
else "AsyncUnionType"
field_name = field.db_field
if field_name is None:
# Get db_field name from parent mongo_field
for db_field_name, _mongo_parent_field in field.owner_document._fields.items():
if hasattr(_mongo_parent_field, "field") and _mongo_parent_field.field == field:
field_name = db_field_name
break

name = to_camel_case(
"{}_{}_union_type".format(
field._owner_document.__name__,
field_name,
)
)
Meta = type("Meta", (object,), {"types": tuple(_types)})
_union = type(name, (graphene.Union,), {"Meta": Meta})
Expand Down
15 changes: 9 additions & 6 deletions graphene_mongo/types.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor

import graphene
import mongoengine
from graphene.relay import Connection, Node
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
from graphene.types.inputobjecttype import InputObjectType, InputObjectTypeOptions
from graphene.types.interface import Interface, InterfaceOptions
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
from graphene.types.utils import yank_fields_from_attrs
from graphene.utils.str_converters import to_snake_case
from graphene_mongo import MongoengineConnectionField

from graphene_mongo import MongoengineConnectionField
from .converter import convert_mongoengine_field
from .registry import Registry, get_global_registry, get_inputs_registry
from .utils import get_model_fields, is_valid_mongoengine_model, get_query_fields, ExecutorEnum, sync_to_async
from .utils import (
ExecutorEnum,
get_model_fields,
get_query_fields,
is_valid_mongoengine_model,
sync_to_async,
)


def construct_fields(
Expand Down Expand Up @@ -240,8 +245,6 @@ async def get_node(cls, info, id):
required_fields = list(set(required_fields))
return await sync_to_async(
cls._meta.model.objects.no_dereference().only(*required_fields).get,
thread_sensitive=False,
executor=ThreadPoolExecutor(),
)(pk=id)

def resolve_id(self, info):
Expand Down
6 changes: 3 additions & 3 deletions graphene_mongo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import inspect
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Callable, Union
from typing import Any, Callable, Coroutine, ParamSpec

import mongoengine
from asgiref.sync import SyncToAsync, sync_to_async as asgiref_sync_to_async
from asgiref.sync import sync_to_async as asgiref_sync_to_async
from graphene import Node
from graphene.utils.trim_docstring import trim_docstring
from graphql import FieldNode
Expand Down Expand Up @@ -283,7 +283,7 @@ def sync_to_async(
func: Callable = None,
thread_sensitive: bool = False,
executor: Any = None, # noqa
) -> Union[SyncToAsync, Callable[[Callable[..., Any]], SyncToAsync]]:
) -> Callable[[ParamSpec("_P")], Coroutine[Any, Any, Any]]:
"""
Wrapper over sync_to_async from asgiref.sync
Defaults to thread insensitive with ThreadPoolExecutor of n workers
Expand Down
Loading

0 comments on commit 23557c6

Please sign in to comment.