以下の内容はhttps://blog.utgw.net/entry/2022/04/18/224253より取得しました。


grapheneでナイーブなnodesクエリを実装する

複数のIDを指定してデータを一括で取得したい場合があって、手作りした。ナイーブにはこういう感じでいける。N+1クエリになるのでもうちょっとうまくやる必要がありそう。

import graphene

class Query(ObjectType):
    # 中略

    nodes = graphene.Field(
        graphene.NonNull(graphene.List(relay.Node)),
        ids=graphene.List(
            graphene.NonNull(graphene.ID),
            required=True,
            description="The IDs of the object",
        ),
    )

    def resolve_nodes(root, info, **args):
        ids = args["ids"]
        return [graphene.relay.Node.node_resolver(None, root, info, id) for id in ids]

こういう実装をしたら以下のようなGraphQLスキーマになる。

type Query {
  # 中略

  nodes(
    """The IDs of the object"""
    ids: [ID!]!
  ): [Node]!
}



以上の内容はhttps://blog.utgw.net/entry/2022/04/18/224253より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14