hello everyone, my graphql error with message : "E...
# orm-help
a
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
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
How does your resolver handle players?
j
I think you need to pass
parent.id
to the
fetch
call.
parent
itself is an object containing the entire 'parent'.
a
i added
Copy code
parent.id
like this
Copy code
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
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
@CCBCodeMonkey in console log ->
Copy code
{ player: 'Invalid Team ID passed' }
c
Copy code
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
in
Copy code
parent.id
i will get idTeam from
Copy code
urlLookupLeague
in my code above then get result all player in team from
Copy code
urlPlayers
c
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
my schema in code above
Copy code
type Team {
  id: ID!
  strTeam: String!
  players: [Player!]!
}
c
if you switch your query to this does it work?
teams { strTeam, id, players { id, strPlayer } }
a
Copy code
"message": "Cannot return null for non-nullable field Team.id."
my complete scheme
Copy code
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
Copy code
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`>))