我试图通过构建具有以下架构的服务器来熟悉Graphql:
type Game
{
id: ID!
players: [Player!]!
winner: Player
...
}
type Player {
id: ID!
game: Game!
...
}
type Query {
getGame(id: ID!): Game
getPlayer(id: ID!): Player
}
并映射到以下数据:
const games = {
1: {
id: 1,
players: [2, 3],
winner: undefined
}
};
const players = {
2: {
id: 2,
game: 1
},
3: {
id: 3,
game: 1
},
};
在编写解析器时,我注意到了很多样板代码-我必须为每个持有外键的对象成员编写相同的字段解析器。
const typedefs = {
Query: {
getGame: (_, {id}) => games[id],
getPlayer: (_, {id}) => players[id]
},
Game: {
winner: (game) => players[game.winner],
players: (game) => game.players.map(id => players[id])
},
Player: {
game: (player) => games[player.id]
}
}
是否可以通过ID创建类型解析器?
例如,Player
字段或查询将始终由(id) => players[id]
解决,
并且游戏领域或查询将始终由(id) => games[id]
解决。
我看到使用AWS-Amplify的@model
指令是可以实现的,但是我想知道是否有一种我所缺少的标准方法而不是实现自己的指令。
这是当前Graphql实现的功能吗?我在apollo-graphql的文档(或graphql-js的文档)中找不到任何类似的东西。
谢谢。