Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep nested objects in django won't work due to async #84

Open
bubblegumsoldier opened this issue Sep 3, 2021 · 6 comments
Open

Deep nested objects in django won't work due to async #84

bubblegumsoldier opened this issue Sep 3, 2021 · 6 comments

Comments

@bubblegumsoldier
Copy link

  • GraphQL AioWS version: (not using AioWS, only django-channels)
  • Python version: 3.8.2
  • Operating System: MacOSX

Description

I am trying to get clients to subscribe to my graphql with a nested object in django. The request looks like this:

subscription {
  taskUpdated(id:"04a5e92bc9084ad981481ab4314a1d33", updateInterval: 3) {
    id
    token
    result {
      id,
      profiles {
        id
      }
    }
  } 
}

Tasks contain one result which contain multiple profiles.

What I Did

In my subscription class I used the sync_to_async method which works if used for the first level:

class Subscription(graphene.ObjectType):
    task_updated = graphene.Field(
        TaskType, id=graphene.String(), update_interval=graphene.Int()
    )

    async def resolve_task_updated(root, info, id, update_interval):
        print("TRYING TO RESOLVE")
        while True:
            print("While true")
            task = await sync_to_async(Task.objects.get, thread_sensitive=True)(
                pk=id
            )
            yield task
            await asyncio.sleep(update_interval)

Using the query only targeting task.id everything works perfectly. Once I try to target task.result (which is a new object). I receive the error:

You cannot call this from an async context - use a thread or sync_to_async.

I managed to get a workaround by updating the TaskType and updating the resolver for the result like so:

class TaskType(DjangoObjectType):
    class Meta:
        model = TaskModel
        fields = "__all__"

    result = graphene.Field(TaskResultType)

    async def resolve_result(self, info):
        return await sync_to_async(
            TaskResult.objects.get, thread_sensitive=True
        )(task=self.id)

That way I could get to the next level (task -> result). When I try to subscribe to task -> result -> profiles there is no way to receive the data anymore. The same error from above is thrown. Even if I update the ResultType accordingly to fetch all profiles async:

class TaskResultType(DjangoObjectType):
    class Meta:
        model = TaskResult
        fields = "__all__"

    profiles = graphene.List(ProfileType)

    async def resolve_profiles(self, info):
        return await sync_to_async(
            Profile.objects.filter, thread_sensitive=True
        )(task_result=self.id)

I thought dynamic querying of objects and subscribing to nested objects would be an out of the box functionality but it is really difficult. I can't be the only person using this repo to actually receive ORM data?

@Miguiz
Copy link

Miguiz commented Mar 29, 2022

UP !
Any news about this issue ? I have the same error.
Actualy the only way is to override resolver in all model type.

@beni1028
Copy link

Facing the same issue.
Will there be an update? Or does it have to do with the implementation.

@beni1028
Copy link

UP ! Any news about this issue ? I have the same error. Actualy the only way is to override resolver in all model type.

Could you please elaborate ?

@Miguiz
Copy link

Miguiz commented May 21, 2022

I have ended by used ObjectType because ModelType always return error with relationship as you think actualy I can't use async with the proper way.
I can find code change in graphql-ws for make it work if you want.

@beni1028
Copy link

beni1028 commented May 21, 2022

I have ended by used ObjectType because ModelType always return error with relationship as you think actualy I can't use async with the proper way. I can find code change in graphql-ws for make it work if you want.

Hey thank you for the quick response. If you can help that would be great. Also, I'll put my code below for you reference.

This is my suscriptions.py
`

class Subscription(graphene.ObjectType):
    new_message = graphene.Context(team_id = graphene.Int())

    async def resolve_new_message(self, info,team_id):
        user = info.context['user']
        channel_name = await channel_layer.new_channel()
        await channel_layer.group_add(str(team_id), channel_name)
        try:
            while True:
                message = await channel_layer.receive(channel_name)
                yield database_sync_to_async(lambda: Message.objects.get(id=message['id']))()
        except Exception as e:
            print(e)
        finally:
            await channel_layer.group_discard(str(user.team_name), channel_name)

Below is my types.py

class MessageType(DjangoObjectType):
    class Meta:
        model = Message
        fields = "__all__"

Any and all help is much appreciated!!!!

@bubblegumsoldier
Copy link
Author

Up! I guess? No news for one year?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants