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

withMiddlewares does not fire within a custom resolver #322

Open tasoskakour opened 3 years ago

tasoskakour commented 3 years ago

Take a look at the code blocks below.

In the first code block, the myMiddleware never executes. The reason I added the withMiddlewares property inside that object is because it's part of the Resolver.d.ts file so I assumed that this is the correct place to add middlewares for a custom resolver and technically this should have worked.

In the second code block the myMiddeware executes correctly.

First code block (middleware does not fire)

const UserTC = composeMongoose(User);

schemaComposer.Query.addFields({
    myCustomResolver: {
        args: {},
        type: UserTC.getType(),
        resolve: userGetMe,
                withMiddlewares: [myMiddleware]
      }
});

Second code block (middleware fires OK)

const UserTC = composeMongoose(User);

schemaComposer.Query.addFields({
    myCustomResolver: UserTC.addResolver({
                args:{},
        name: 'userGetMe',
        type: UserTC.getType(),
        resolve: userGetMe,
        description: 'Returns current logged in user based on Authorization headers.',
    })
        .getResolver('userGetMe')
        .withMiddlewares([myMiddleware]),
});
yurtaev commented 3 years ago

withMiddlewares is method of Resolver, you can chaining withMiddlewares with your resolver resolve: userGetMe.withMiddlewares([myMiddleware]) if userGetMe is instance of Resolver.

if you use object in addFields = type of ObjectTypeComposerFieldConfigAsObjectDefinition