pablo
01/06/2024, 4:21 PMgraphql
# Define a directive named @authCheck
directive @authCheck(resource: String!) on FIELD_DEFINITION
# Define your types and fields
type Query {
# Example field that uses the @authCheck directive
someProtectedResource: SomeResourceType @authCheck(resource: "resource_name")
}
js
class AuthCheckDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
const { resource } = this.args;
field.resolve = async function (...args) {
const context = args[2];
const userId = context.userId;
// Check permissions using spicedb.check
const hasPermission = await spicedb.check(resource, userId);
if (!hasPermission) {
throw new Error('Unauthorized');
}
return resolve.apply(this, args);
};
}
}
vroldanbet
01/08/2024, 10:08 AMpablo
01/09/2024, 8:59 AM