philch
05/19/2018, 5:02 PMmedelman
05/19/2018, 5:21 PMlawjolla
05/19/2018, 5:23 PMmedelman
05/19/2018, 5:27 PMmedelman
05/19/2018, 5:28 PMlawjolla
05/19/2018, 5:31 PMphilch
05/19/2018, 7:14 PMphilch
05/19/2018, 8:02 PMdirective @auth(
requires: Role = SUBSCRIBER
isOwner: Boolean = false
) on FIELD_DEFINITION | QUERY
type Query {
me: User @auth
users: [User!]! @auth(requires: ADMIN)
}
and if your schema directive you have
export class IsAuthenticated extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>, details) {
console.log('DETAILS', details.objectType);
// console.log('SchemaVisitorArgs', this.args);
const requiredRole = this.args.requires;
const { resolve = defaultFieldResolver } = field;
field.resolve = async function(...resolveArgs) {
const [source, args, ctx, info] = resolveArgs;
console.log('SOURCE INSIDE FIELD RESOLVE', source);
console.log('ARGS INSIDE FIELD RESOLVE', args);
console.log('INFO INSIDE FIELD RESOLVE', info.fieldNodes);
const { userId, role } = validateJwt(ctx);
const isOwner = await ctx.db.exists[Type]({
where: {
author: id,
},
});
console.log('USER ID + ROLE FROM JWT', userId, role);
if (role !== requiredRole) {
throw new Error(`You must be a ${requiredRole}`);
}
ctx.id = userId;
const result = await resolve.apply(this, resolveArgs);
return result;
};
}
}
as your schema directive, where would you get the Type fromlawjolla
05/19/2018, 10:02 PMusers: [User!]! @auth(type: "User", requires: ADMIN)
philch
05/19/2018, 10:05 PMphilch
05/19/2018, 10:05 PMphilch
05/19/2018, 10:06 PM