-
-
Notifications
You must be signed in to change notification settings - Fork 674
Open
Labels
Community 👨👧Something initiated by a communitySomething initiated by a communityEnhancement 🆕New feature or requestNew feature or request
Milestone
Description
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.
glen-84
Metadata
Metadata
Assignees
Labels
Community 👨👧Something initiated by a communitySomething initiated by a communityEnhancement 🆕New feature or requestNew feature or request