Here are my typescript objects (simplified):
@ObjectType()
export default class Game {
@Field(() => ID)
id?: string
@arrayProp({items: Player})
@Field(() => [Player])
players: Player[] = []
}
@ObjectType()
export class Player {
@Field(() => ID)
id?: string
}
It generates this schema using type-graphql:
type Game {
id: ID!
players: [Player!]!
}
type Player {
id: ID!
}
I use a resolver to query the user's games:
@Resolver(() => User)
export class UserGameResolver {
@FieldResolver(() => [Game])
async ongoingGames(@Ctx('userId') userId: string) {
return GameModel.find({'players.userId': userId, setup: null})
}
}
And it produces this error: "GraphQL error: Cannot return null for non-nullable field Player.id"
What I already know:
If someone has an clue about what is happening here, it would be great!
you should try to use _id
if you really want to use id
(the virtual), then you need to either use .toJson
or .toObject
with the options { virtuals: true }
return GameModel.find({'players.userId': userId, setup: null}).toObject({ virtuals: true });