私が歌川です

@utgwkk が書いている

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]!
}