0
votes

I have a schema (with the appropriate database tables and entity classes defined) like

type User {
    id: Int!
    phoneNumber: String!
}

type Event {
    id: Int!
    host: User
  }

and I'm trying to use Apollo to write a query like

Query{
  event(id:1){
    host{
      firstName
    }
  }
}

But I can't figure out how to get the Apollo library to resolve the User type in the host field to the hostId that is stored on the event object.

I modified the event to return the hostId field, and it works perfectly fine, but Graphql won't resolve the id to the appropriate user type. What am I missing?

edit: 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 managed to get a working version by using findOne(args.id, { relations: ['host']}), but I don't like that because it seems like something that would be appropriate to delegate to graphql to handle.

1
Please include the relevant resolver code. - Daniel Rearden

1 Answers

2
votes

Your resolver should be like that

const resolver = {

Query: {
    event: async (_: any, args: any) => {
      return await event.findOne(args.id);
    }
  },
  event: {
    host: async (parent: any, args: any, context: any) => {
      return await user.find({ id: parent.id });
    }
  }
};