GraphQl查询中的嵌套字段如何获得自己的参数

发布于 2025-01-19 18:05:07 字数 711 浏览 1 评论 0原文

我正在研究基于光标的分页,该分页需要嵌套字段才能访问ARGS。到目前为止,我一直无法将Args带到其所需的领域。

   type Game {
    players(first: Int, after: String, last: Int, before: String): GameToPlayerConnection
  }

  game(id: GameID!): Game

我只能访问传递给game的args,而不是传递给players的args。

 game: async (parent: any, args: { id: string; first: number; last: number; after: 
       string; before: string; }, _ctx: any, info: any) => 
        {
        const { id, first, last, after, before } = args;
        console.log("args", args);
        }



  game(id: 'fortnite', first: 3){
    players(first: 2){
     ....
     }
    }

我正在尝试访问传递给players的args

I'm working on a cursor based pagination that requires the a nested field to access the args. So far, i've been unable to get the args to its desired field.

   type Game {
    players(first: Int, after: String, last: Int, before: String): GameToPlayerConnection
  }

  game(id: GameID!): Game

I am able to only access the args passed to the game and not the args passed to the players.

 game: async (parent: any, args: { id: string; first: number; last: number; after: 
       string; before: string; }, _ctx: any, info: any) => 
        {
        const { id, first, last, after, before } = args;
        console.log("args", args);
        }



  game(id: 'fortnite', first: 3){
    players(first: 2){
     ....
     }
    }

I am trying to access the args passed to the players

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2025-01-26 18:05:07

没错。每个解析器只能访问其自身属性的参数。每个对象也都有其自己的一组解析器,因此您需要一个对象进行查询,另一个对象进行游戏:

const resolvers = {
  Query: {
    game: async (parent, { id }, context, info) {
      return games.load(id);
    },
  },
  Game: {
    players: async ({ id }, { first, last, before, after }, context, info) {
      return playersByGame({
        gameId: id,
        first,
        last,
        before,
        after,
      });
    },
  },
};

That's correct. Every resolver can only access the arguments for its own property. Each object also has its own set of resolvers, so you need one object for Query, and another for Game:

const resolvers = {
  Query: {
    game: async (parent, { id }, context, info) {
      return games.load(id);
    },
  },
  Game: {
    players: async ({ id }, { first, last, before, after }, context, info) {
      return playersByGame({
        gameId: id,
        first,
        last,
        before,
        after,
      });
    },
  },
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文