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

Add hooks to resolvers config options (after releasing v9.0.0) #275

Open nodkz opened 3 years ago

nodkz commented 3 years ago

The new major 9.0.0 version of graphql-compose-mongoose will introduce a new process for resolver creation. Before 9.0.0 all resolvers were created on composeWithMongoose method call. But starting from 9.0.0 introduced a new method composeMongose for creating TypeComposers from mongoose models. And this method will not create resolvers. You will be able to create them via mongooseResolvers:

import { UserModel } from './user';
import { composeMongoose } from 'graphql-compose-mongoose';

const UserTC = composeMongoose(UserModel);
// ... modify type as you need

const userFindManyResolver = UserTC.mongooseResolvers.findMany(configForFindMany);
const userRemoveByIdResolver = UserTC.mongooseResolvers.removeById(configForRemoveById);

Such granular resolver creation unlocks the ability to provide as many configuration options as we need. And the first candidate is HOOKS (which should replace spaghetti with wrapResolve).

HOOKS may solve the following tasks:

You may see how hooks are implemented in mongo-graphql-starter. I want to provide something similar but on a resolver basis.

Anyway, if you have any ideas about how configs for HOOKS should look like, or found somewhere a convenient hook realization – please share with us.

natac13 commented 3 years ago

Hooks sound like a great idea! I am wondering where I can find a type definition of the config options? In each resolver's file... :disappointed:

I was curious because I wanted to see how the relay style middleware fits into this? or if it does still? With getResolver I was able to provide an array of middlewares. Would the new hooks replace this?

oklas commented 3 years ago

Current approach for middlewares is declared here https://github.com/graphql-compose/graphql-compose-mongoose/issues/158#issuecomment-772695238:

schemaComposer.Query.addFields({
    userById: UserTC.mongooseResolvers.findById().withMiddlewares([authMiddleware]),
   userByIds: UserTC.mongooseResolvers.findByIds().withMiddlewares([authMiddleware]),
});