[英]GraphQL Unable to Resolve Schema on Mutation
I have two schemas:我有两个模式:
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',
}
And和
var Team = new graphql.GraphQLObjectType({
name: 'Team',
fields: () => ({
id: { type: graphql.GraphQLInt },
name: { type: graphql.GraphQLString },
})
})
Team._typeConfig = {
sqlTable: 'team',
uniqueKey: 'id'
}
I also have 1 mutation and 1 query:我也有 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];
})
}
}
})
})
And和
// 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)
})
}
}
})
Basically the application can insert a player and the team id of which he/she belongs to.基本上,应用程序可以插入一个球员和他/她所属的球队ID。
When doing mutation (inserting a record), the record is added successfully & I am able to query the correct data.进行突变(插入记录)时,记录已成功添加并且我能够查询正确的数据。 The problem is when doing an insertion of player while also requesting the team information, the GraphQL returns a null value for team.
问题是在插入球员的同时还请求球队信息时,GraphQL 返回球队的 null 值。
mutation{
player(first_name:"Kobe", last_name:"Bryant", team_id:1) {
id
last_name
first_name
team{
id
name
}
}
}
Returns:回报:
{
"data": {
"player": {
"id": "70",
"last_name": "Bryant",
"first_name": "Kobe",
"team": null
}
}
}
However when requesting the player with the id "70", team information was resolved successfully:但是,当请求 id 为“70”的玩家时,团队信息被成功解析:
query{
player(id:70) {
id
team{
id
name
}
}
}
Returns:回报:
{
"data": {
"player": {
"id": "70",
"team": {
"id": 1,
"name": "Los Angeles Lakers"
}
}
}
}
Any ideas on what am I missing here?关于我在这里缺少什么的任何想法?
Sorry if my explanation is a bit confusing, as I am still learning the basics of GraphQL.抱歉,如果我的解释有点混乱,因为我仍在学习 GraphQL 的基础知识。 Thanks a lot!
非常感谢!
It seems like you are not using plain GraphQL.js but some additional library/framework that creates SQL queries for you.似乎您没有使用普通的 GraphQL.js,而是使用一些额外的库/框架来为您创建 SQL 查询。 This is not the standard and usually fields use resolvers to fetch additional data.
这不是标准,通常字段使用解析器来获取额外的数据。
If you run the query that works this framework magically creates a similar query like this query for you thanks to the annotations that you made in the GraphQL type config:如果您运行运行该框架的查询,由于您在 GraphQL 类型配置中所做的注释,该框架会神奇地为您创建与此查询类似的查询:
SELECT id, team.id as team_id, team.name as team_name
FROM player
JOIN team ON player.team_id = team.id
WHERE player.id = 70
Notice how it automatically adds a JOIN
-statement.注意它是如何自动添加一个
JOIN
语句的。 But in your mutation you have chosen to write a query by hand and send it to the database directly.但是在您的突变中,您选择手动编写查询并将其直接发送到数据库。 Your framework cannot help you and insert a join.
您的框架无法帮助您插入连接。 The database query will only return the player fields without the team fields.
数据库查询将只返回球员字段而不返回球队字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.