Deep nested objects in django won't work due to async
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
Piste de recherche
Commencez par les exemples de resolvers Subscription, TaskType et TaskResultType dans l’issue, puis reproduisez la subscription imbriquée task → result → profiles avec la configuration Django. Suivez l’endroit où l’erreur de contexte asynchrone se produit et vérifiez que la subscription imbriquée complète renvoie les données de profil sans nécessiter de workarounds supplémentaires dans les resolvers.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
- 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?
- Langage dominant
- Python
- Étoiles
- 270
- Forks
- 84
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de graphql-python/graphql-ws
-
using this with signalsOuverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 20/100
graphql-python/graphql-ws#95 · 1 réaction ·
-
graphql-ws-nextOuverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 20/100
graphql-python/graphql-ws#90 · 4 commentaires ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
graphql-python/graphql-ws#89 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 25/100
graphql-python/graphql-ws#87 ·
-
Update to graphene v3 ?Ouverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 18/100
graphql-python/graphql-ws#66 · 8 commentaires · 2 réactions ·
Toutes les issues de graphql-python/graphql-ws
Issues similaires
-
python triage
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
microsoft/semantic-kernel#14590 ·
Les mainteneurs répondent en général sous 3 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
pinterest/pymemcache#662 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 Une demi-journée Accessibilité débutants 62/100
gnocchixyz/gnocchi#1527 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
tenstorrent/tt-model-manager#181 ·
Les mainteneurs répondent en général sous 1 jour