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

Dataloader's documentation

Aperta
#1,336 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
graphql, python
Ambito
documentation

Direzione di ricerca

Inizia dalla documentazione di DataLoader collegata e rivedi l’esempio del resolver User, inclusi root.best_friend_id, root.friend_ids e User.objects.filter(id__in=keys). Confronta questi esempi con le funzioni helper descritte e chiarisci quale codice sia eseguibile e quale sia solo illustrativo. Il lavoro è completato quando la documentazione presenta un caso d’uso autonomo e ben definito e spiega il comportamento previsto della funzione batch.

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

Descrizione

🐛 bug

The dataloader documentation is quite unclear in the current state, especially this piece of code:

class User(graphene.ObjectType):
    name = graphene.String()
    best_friend = graphene.Field(lambda: User)
    friends = graphene.List(lambda: User)

    def resolve_best_friend(root, info):
        return user_loader.load(root.best_friend_id)

    def resolve_friends(root, info):
        return user_loader.load_many(root.friend_ids)

It's not clear how this could work, because as it's presented here, neither root.best_friend_id nor root.friend_ids would be defined. Wouldn't it make sense to describe a real use case? The current documentation is more or less useless because when you first read it you can't tell what is an approximation and what is not (example of another non-working bit of code User.objects.filter(id__in=keys)). I'm not even sure yet about all this because I know graphene rewrites classes but I don't know all the details since I only used it (and never contributed). So maybe there is to trickery hidden in these statements, but I really feel there are several errors in the provided examples.

Maybe the solution to this is only to add empty objects/resolver (pass) but with well defined types. So everything is clearly defined (but unimplemented).

I've tried to make sense about graphene + aioloader by writing three simple helpers function that create some batch functions:

from collections import defaultdict
from typing import Callable, Dict, List, Optional, Tuple, TypeVar

class OneToOne:
    T = TypeVar("T")
    K = TypeVar("K", str, int)
    Query = Callable[[List[K]], List[T]]
    Key = Callable[[T], K]


OneToMany = OneToOne


class ManyToMany:
    T = TypeVar("T")
    Query = Callable[[List[str]], List[Tuple[T, str]]]


def one_to_one(query: OneToOne.Query, key: OneToOne.Key):

    async def batch_function(keys: List[str]) -> List[Optional[OneToOne.T]]:
        user_objects = {key(obj): obj for obj in query(keys)}
        return [user_objects.get(k) for k in keys]

    return batch_function


def one_to_many(query: OneToMany.Query, key: OneToMany.Key):

    async def batch_function(keys: List[str]) -> List[List[OneToMany.T]]:
        objects = query(keys)
        user_objects = defaultdict(list)
        for obj in objects:
            user_objects[key(obj)].append(obj)
        return [user_objects.get(k) or [] for k in keys]

    return batch_function


def many_to_many(query: ManyToMany.Query):

    async def batch_function(keys: List[str]) -> List[List[ManyToMany.T]]:
        obj_key_pairs = query(keys)
        user_objects: Dict[str, List[ManyToMany.T]] = defaultdict(list)
        for obj, k in obj_key_pairs:
            user_objects[k].append(obj)
        return [user_objects.get(k) or [] for k in keys]

    return batch_function

Describing this kind of functions might even help people understanding what is expected from them. I'm not sure yet these types (or even helper functions) are 100% correct, let me know if you find better solutions.

Lingua principale
Python
Stelle
8.2k
Fork
818
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

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/graphene

Tutte le issue di graphql-python/graphene

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.