GraphQL无法解析突变的架构

时间:2020-06-16 08:22:30

标签: node.js graphql

我有两种模式:

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的基础知识。非常感谢!

1 个答案:

答案 0 :(得分:1)

似乎您不是在使用纯GraphQL.js,而是在使用一些其他库/框架来为您创建SQL查询。这不是标准方法,通常字段使用解析器来获取其他数据。

如果您运行的查询有效,那么该框架将通过GraphQL类型config中的注释为您神奇地创建类似于该查询的查询:

return ListTile(
      contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 0.0),
      dense: true,
      title: Row(
        children: <Widget>[
          Icon(icon),
          Padding(
            padding: EdgeInsets.only(left: 8.0, top: 0.0, bottom: 0.0),
            child: Expanded(
              child: Text(
                googleplace.address,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          )
        ],
      ),
      onTap: onTap,
    );

注意如何自动添加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 语句。但是在您的变异中,您选择了手动编写查询并将其直接发送到数据库。您的框架无法帮助您并插入联接。数据库查询将仅返回球员字段而不包含团队字段。