【发布时间】:2020-06-17 03:55:52
【问题描述】:
我有两个架构:
var Player = new graphql.GraphQLObjectType({
name: 'Player',
fields: () => ({
id: { type: graphql.GraphQLString },
first_name: { type: graphql.GraphQLString },
last_name: { type: graphql.GraphQLString },
team: {
type: Team,
sqlJoin: (playerTable, teamTable, args) => `${playerTable}.team_id = ${teamTable}.id`
}
})
});
Player._typeConfig = {
sqlTable: 'player',
uniqueKey: 'id',
}
和
var Team = new graphql.GraphQLObjectType({
name: 'Team',
fields: () => ({
id: { type: graphql.GraphQLInt },
name: { type: graphql.GraphQLString },
})
})
Team._typeConfig = {
sqlTable: 'team',
uniqueKey: 'id'
}
我也有 1 个突变和 1 个查询:
// Mutation
const MutationRoot = new graphql.GraphQLObjectType({
name: 'Mutation',
fields: () => ({
player: {
type: Player,
args: {
first_name: { type: graphql.GraphQLNonNull(graphql.GraphQLString) },
last_name: { type: graphql.GraphQLNonNull(graphql.GraphQLString) },
team_id: { type: graphql.GraphQLNonNull(graphql.GraphQLInt) },
},
resolve: (root, args) => {
return client.query("INSERT INTO player (first_name, last_name, team_id) VALUES ($1, $2, $3) RETURNING *", [args.first_name, args.last_name, args.team_id]).then(result=>{
console.log(result);
return result.rows[0];
})
}
}
})
})
和
// Query
const QueryRoot = new graphql.GraphQLObjectType({
name: 'Query',
fields: () => ({
player: {
type: Player,
args: { id: { type: graphql.GraphQLNonNull(graphql.GraphQLInt) } },
where: (playerTable, args, context) => `${playerTable}.id = ${args.id}`,
resolve: (parent, args, context, resolveInfo) => {
return joinMonster.default(resolveInfo, {}, sql => {
return client.query(sql)
})
}
}
})
基本上,应用程序可以插入一个球员和他/她所属的球队ID。
在进行突变(插入记录)时,记录已成功添加并且我能够查询正确的数据。问题是在插入球员的同时还请求球队信息时,GraphQL 会为球队返回空值。
mutation{
player(first_name:"Kobe", last_name:"Bryant", team_id:1) {
id
last_name
first_name
team{
id
name
}
}
}
返回:
{
"data": {
"player": {
"id": "70",
"last_name": "Bryant",
"first_name": "Kobe",
"team": null
}
}
}
但是在请求id为“70”的玩家时,团队信息解析成功:
query{
player(id:70) {
id
team{
id
name
}
}
}
返回:
{
"data": {
"player": {
"id": "70",
"team": {
"id": 1,
"name": "Los Angeles Lakers"
}
}
}
}
关于我在这里缺少什么的任何想法?
对不起,如果我的解释有点混乱,因为我仍在学习 GraphQL 的基础知识。非常感谢!
【问题讨论】: