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

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

Open nodkz opened this issue 5 years ago • 2 comments

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:

  • authorization
  • logging
  • validation
  • subscription support (pushing event to PubSub)

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.

nodkz avatar Sep 14 '20 19:09 nodkz

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?

natac13 avatar Sep 27 '20 11:09 natac13

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]),
});

oklas avatar Feb 12 '21 10:02 oklas