graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Usage with Apollo server datasources

Open WEeziel172 opened this issue 4 years ago • 2 comments

Hey!

Are there any examples of using this library with apollo datasources? Could it be feasable to use some of the resolvers in a custom datasource if needed?

WEeziel172 avatar Jun 22 '21 18:06 WEeziel172

It quite simple, just read datasources from context and use as usual in resolve methods:

// add Fields to Query
schemaComposer.Query.addFields({
  user: {
    type: UserTC,
    args: { id: 'Int' },
    resolve: (_, { id }, { dataSources: { users } }) => users.getUser(id)
  }
});

// add relation between types
PostTC.addFields({
  author: {
    type: () => UserTC,
    resolve: (post, _, { dataSources: { users } }) => users.getUser(post.authorId),
  },
})

nodkz avatar Jun 22 '21 20:06 nodkz

Thanks for your input!

Is there any reason not to use some of the available mongoose resolvers in the actual datasource class? For example: We would have a users datasource. Some of the data comes from and external API, and some could be fetched straight from the database. Could we use mongoose resolvers from UserTC inside the datasource if needed?

WEeziel172 avatar Jun 22 '21 20:06 WEeziel172