arsan.irianto
02/26/2019, 2:50 AMCCBCodeMonkey
02/26/2019, 5:25 AMCCBCodeMonkey
02/26/2019, 5:28 AMCCBCodeMonkey
02/26/2019, 5:28 AMsandorTuranszky
02/26/2019, 7:47 AMJenkins
02/26/2019, 7:47 AMparent.id to the fetch call. parent itself is an object containing the entire 'parent'.arsan.irianto
02/26/2019, 8:28 AMparent.id like this Team: {
players: parent => {
return fetch(`${urlPlayers}${parent.id}`).then(res => res.json()).then(res => res.player)
}
} and now get error messsage "Cannot return null for non-nullable field Team.players."CCBCodeMonkey
02/26/2019, 8:29 AMCCBCodeMonkey
02/26/2019, 8:29 AMCCBCodeMonkey
02/26/2019, 8:29 AMCCBCodeMonkey
02/26/2019, 8:30 AMasync await instead of all the thens I think it would make your code more readableCCBCodeMonkey
02/26/2019, 8:30 AMarsan.irianto
02/26/2019, 8:31 AM{ player: 'Invalid Team ID passed' }CCBCodeMonkey
02/26/2019, 8:32 AMconst fetched = await fetch(...);
const { player } = await fetched.json();
return player;CCBCodeMonkey
02/26/2019, 8:32 AMCCBCodeMonkey
02/26/2019, 8:32 AMparent.id?CCBCodeMonkey
02/26/2019, 8:32 AMCCBCodeMonkey
02/26/2019, 8:34 AMCCBCodeMonkey
02/26/2019, 8:34 AMplayer prop in the resultCCBCodeMonkey
02/26/2019, 8:34 AMCCBCodeMonkey
02/26/2019, 8:35 AMCCBCodeMonkey
02/26/2019, 8:35 AMarsan.irianto
02/26/2019, 8:36 AMparent.id i will get idTeam from urlLookupLeague in my code above then get result all player in team from urlPlayersCCBCodeMonkey
02/26/2019, 8:36 AMCCBCodeMonkey
02/26/2019, 8:37 AMCCBCodeMonkey
02/26/2019, 8:38 AMCCBCodeMonkey
02/26/2019, 8:38 AMCCBCodeMonkey
02/26/2019, 8:38 AMCCBCodeMonkey
02/26/2019, 8:39 AMteams { players { id, strPlayer } } ?arsan.irianto
02/26/2019, 8:40 AMtype Team {
id: ID!
strTeam: String!
players: [Player!]!
}CCBCodeMonkey
02/26/2019, 8:40 AMCCBCodeMonkey
02/26/2019, 8:40 AMteams { strTeam, id, players { id, strPlayer } }arsan.irianto
02/26/2019, 8:45 AM"message": "Cannot return null for non-nullable field Team.id."arsan.irianto
02/26/2019, 8:45 AMtype Query {
teams: [Team!]!
players(id: ID!): [Player!]!
}
type Team {
id: ID!
strTeam: String!
players: [Player!]!
}
type Player {
id: ID!
strPlayer: String!
}arsan.irianto
02/26/2019, 9:03 AMarsan.irianto
02/26/2019, 9:04 AMconst { GraphQLServer } = require('graphql-yoga')
const fetch = require('node-fetch')
const urlLookupLeague = `<https://www.thesportsdb.com/api/v1/json/1/lookup_all_teams.php?id=4328>`
const urlPlayers = `<https://www.thesportsdb.com/api/v1/json/1/lookup_all_players.php?id=>`
const typeDefs = `
type Query {
teams: [Team!]!
players(id: ID!): [Player!]!
}
type Team {
idTeam: ID!
strTeam: String!
players: [Player]
}
type Player {
idTeam: ID!
strPlayer: String!
}
`
const resolvers = {
Query: {
teams: () => {
return fetch(`${urlLookupLeague}`).then(res => res.json()).then(res => res.teams)
}
},
Team: {
players: parent => {
return fetch(`${urlPlayers}${parent.idTeam}`).then(res => res.json()).then(res => res.player)
}
}
}
const server = new GraphQLServer({
typeDefs,
resolvers,
})
server.start(() => console.log(`Server Running on <http://localhost:4000`>))