I've set up a schema that includes database tables and entity classes as shown below:
type User {
id: Int!
phoneNumber: String!
}
type Event {
id: Int!
host: User
}
Now, I'm attempting to create a query using Apollo like this:
Query{
event(id:1){
host{
firstName
}
}
}
However, I'm struggling to get the Apollo library to resolve the User type in the host field to the corresponding hostId stored on the event object.
I modified the event to include the hostId field, which resolved successfully. But Graphql is unable to resolve the id to the correct user type. What am I missing?
Edit: Here's the missing resolver code:
event: async (parent: any, args: { id: number }) => {
const eventRepository = getConnection().getRepository(Event);
const event = await eventRepository.findOne(args.id);
return event;
},
I did manage to make things work by using
findOne(args.id, { relations: ['host']})
, but I'm not entirely comfortable with this approach as it feels like something that should be handled by Graphql itself.