graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Usage with Apollo server datasources #351

Open WEeziel172 opened 3 years ago

WEeziel172 commented 3 years ago

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?

nodkz commented 3 years ago

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),
  },
})
WEeziel172 commented 3 years ago

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?