简体   繁体   中英

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:

// 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.

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.

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:

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. Thanks a lot!

It seems like you are not using plain GraphQL.js but some additional library/framework that creates SQL queries for you. 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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM