Skip to content

Accessing to resolver metadata in middleware #123

@erencay

Description

@erencay

Providing metadata/resolver information into a middleware operation might have some benefits for extended use cases.

Example Use Case:
In this example I wanted to change level field to a fixed value if user is not an admin.

Decorator example;

function ValueUnlessAdmin<T>(val: any) {
    return function(inputCls: any, field: any) {
        Reflect.defineMetadata("valueUnlessAdmin", val, inputCls.constructor, field);
    };
}

InputType example;

@InputType()
class CreateUserInput {
    @Field()
    @ValueUnlessAdmin("member") // Creates metadata
    level: string;
}

Middleware example;

class SetValues implements MiddlewareInterface<Cntx> {
    async use({ context, args }: ResolverData<Cntx>, next: NextFn) {
        // At this point one might want to use metadata
        return next();
    }
}

export const schema = buildSchemaSync({
    resolvers: [UserResolver],
    globalMiddlewares: [SetValues],
});

Here's a suggested workaround by @19majkel94

> technically you can create a class decorator to mark an input class that it has special decorator like @ValueUnlessAdmin
> they both can use Reflect.defineMetadata to store some data attached
> and then in middleware you can browse the info object to search if the mutation use an input type with this custom decorator
> middlewares don't know if next is a middleware or a resolver so they have no access to compiled resolver params
> so you can't get array of params with instances of classes and make some checks
> and input type field have no middlewares or guards and input types are only data transfer object, they're not executed, no logic there

This workaround did the trick. That being said, it could be easier to realize such intentions and may be more if metadata/resolver information can be accessed in the middleware.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions