rdunk
12/14/2021, 4:45 PMMaciek K
12/14/2021, 5:00 PMimport { rule, shield } from "graphql-shield";
import { applyMiddleware } from "graphql-middleware";
const hasAccessToEntity = rule({ cache: "strict" })(async (_parent, args, ctx) => {
const entities = await ctx.prisma.teams.count({
where: {
entityId: args.entityId,
userId: args.userId,
},
});
return entities > 0;
});
export const permissions = shield(
{
Mutation: {
updateEntity: hasAccessToEntity
},
Query: {
entity: hasAccessToEntity
},
},
);
...
const schemaWithPermissions = applyMiddleware(schema, permissions);
...
server.register(mercurius, {
schema: schemaWithPermissions,
});
rdunk
12/14/2021, 5:16 PMctx.prisma.entity.findFirst({
where: {
id: args.entityId,
teamId: {
in: ctx.user.teams.map((t) => t.teamId),
},
},
});
rdunk
12/14/2021, 5:17 PMMaciek K
12/14/2021, 5:18 PMMaciek K
12/14/2021, 5:21 PMrdunk
12/14/2021, 5:23 PMMaciek K
12/14/2021, 5:26 PMrdunk
12/14/2021, 5:29 PMMaciek K
12/14/2021, 5:40 PM