Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Dataloader's documentation

未关闭
#1,336 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
graphql, python
领域
documentation

调研方向

从链接的 DataLoader 文档开始,查看 User resolver 示例,包括 root.best_friend_id、root.friend_ids 和 User.objects.filter(id__in=keys)。将这些示例与所描述的辅助函数进行比较,并明确哪些代码可以运行,哪些代码仅用于说明。完成的标准是文档呈现一个自包含且定义明确的用例,并解释 batch 函数的预期行为。

由索引模型根据 Issue 内容生成。

描述

🐛 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.

主要语言
Python
星标
8.2k
派生
818
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphql-python/graphene 的其他 Issue

查看 graphql-python/graphene 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。