【问题标题】:GraphQL Unable to Resolve Schema on MutationGraphQL 无法解析突变模式
【发布时间】: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 的基础知识。非常感谢!

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:

    您似乎没有使用普通的 GraphQL.js,而是使用了一些为您创建 SQL 查询的附加库/框架。这不是标准,通常字段使用解析器来获取额外的数据。

    如果您运行运行该框架的查询,由于您在 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
    

    注意它是如何自动添加JOIN-statement 的。但是在您的突变中,您选择手动编写查询并将其直接发送到数据库。您的框架无法帮助您插入连接。数据库查询将只返回球员字段而不返回球队字段。

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 2021-07-18
      • 2020-09-23
      • 2022-12-04
      • 2018-10-12
      • 2021-01-13
      • 2020-09-21
      • 2021-03-01
      • 1970-01-01
      相关资源
      最近更新 更多