-
Notifications
You must be signed in to change notification settings - Fork 11
Wrap Resolver
Alex Ald edited this page Apr 29, 2019
·
2 revisions
This decorator allows you to modify a field for a Graphql type and apply specific logic around it. A decorator for PostGraphile's makeWrapResolversPlugin.
Decorator takes a single parameter of type WrapResolverOptions
interface WrapResolverOptions {
fieldName: string;
// PostGraphile specific options
childColumns?: Array<{ column: string; alias: string }>;
siblingColumns?: Array<{ column: string; alias: string }>;
}
Visit PostGraphile's docs, for more information about the parameters.
@WrapResolver({ fieldName: 'email' })
public alterEmail(
resolve: GraphQLFieldResolver,
user: any,
args: any,
context: any,
resolveInfo: GraphQLResolveInfo,
) {
...
}
We can use the WrapResolver
to easily prevent the user's email for being shown, by returning null if the user requesting the field is not the same as the user for which the email was requested. (Note that the email is still retrieved from the database, it is just not returned to the user.)
Note: This example was taken from the PostGraphile docs, and updated to be used with the decorators.
@SchemaType({ typeName: 'User'})
export class UserType {
@WrapResolver({ fieldName: 'email', siblingColumns: [{ column: "id", alias: "$user_id" }] })
public alterEmail(resolve: GraphQLFieldResolver, user: any, args: any, context: any) {
if (context.jwtClaims.user_id !== user.$user_id) {
return null;
} else {
return resolver();
}
}
}