Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#84 6 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
django, graphql, python
Ambito
api, backend, database

Direzione di ricerca

Inizia con gli esempi di resolver per Subscription, TaskType e TaskResultType nell’issue, quindi riproduci la subscription annidata task → result → profiles con la configurazione Django. Traccia il punto in cui si verifica l’errore di contesto asincrono e verifica che la subscription annidata completa restituisca i dati del profilo senza richiedere ulteriori workaround nei resolver.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

  • 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?

Lingua principale
Python
Stelle
270
Fork
85
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di graphql-python/graphql-ws

Tutte le issue di graphql-python/graphql-ws

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.