Title
a

arsan.irianto

02/26/2019, 2:50 AM
hello everyone, my graphql error with message : "Expected Iterable, but did not find one for field Team.players." how to solve this?. I will to query graphql client like this : query { teams{ strTeam players{ strPlayer } } }
c

CCBCodeMonkey

02/26/2019, 5:25 AM
sounds like team.players isn't returning the right type
looks to me like your players resolver is returning a single player
but your schema probably defines [player!]! or something
s

sandorTuranszky

02/26/2019, 7:47 AM
How does your resolver handle players?
j

Jenkins

02/26/2019, 7:47 AM
I think you need to pass
parent.id
to the
fetch
call.
parent
itself is an object containing the entire 'parent'.
a

arsan.irianto

02/26/2019, 8:28 AM
i added
parent.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."
c

CCBCodeMonkey

02/26/2019, 8:29 AM
can you add a console log
or something to see what is being returned from your API
you are returning null in that case
also its kind of nice to use
async await
instead of all the
thens
I think it would make your code more readable
and you coud log what the different steps are returning
a

arsan.irianto

02/26/2019, 8:31 AM
@CCBCodeMonkey in console log ->
{ player: 'Invalid Team ID passed' }
c

CCBCodeMonkey

02/26/2019, 8:32 AM
const fetched = await fetch(...);
const { player } = await fetched.json();
return player;
so you are passing something invalid...
what is
parent.id
?
this really has more to do with your sports API usage than graphql...
you are kind of wrong in a lot of places...
from what I see there isn't even a
player
prop in the result
oh nevermind ignore that, player is an array
weird API
a

arsan.irianto

02/26/2019, 8:36 AM
in
parent.id
i will get idTeam from
urlLookupLeague
in my code above then get result all player in team from
urlPlayers
c

CCBCodeMonkey

02/26/2019, 8:36 AM
but what is parent.id in your players resolver
also what is your schema for players?
oh nm I see its in that file
I usually keep mine in a separate file
your query is like:
teams { players { id, strPlayer } }
?
a

arsan.irianto

02/26/2019, 8:40 AM
my schema in code above
type Team {
  id: ID!
  strTeam: String!
  players: [Player!]!
}
c

CCBCodeMonkey

02/26/2019, 8:40 AM
if you switch your query to this does it work?
teams { strTeam, id, players { id, strPlayer } }
a

arsan.irianto

02/26/2019, 8:45 AM
"message": "Cannot return null for non-nullable field Team.id."
my complete scheme
type Query {
  teams: [Team!]!
  players(id: ID!): [Player!]!
}
type Team {
  id: ID!
  strTeam: String!
  players: [Player!]!
}
type Player {
  id: ID!
  strPlayer: String!
}
thanks for all respon,,, my problem solved now 😄
this my script now
const { 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`>))