You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After considering multiple solutions (that were known to me) including a schema-first approach with directives
I've started discussing with @benjie where he pointed me to some great alternatives for achieving authorization on root fields.
I settled on an implementation that worked for my use case, which is as follows:
import{context,lambda,sideEffect}from"postgraphile/grafast";import{makeWrapPlansPlugin}from"postgraphile/utils";import{GraphQLError}from"postgraphile/graphql";constUSER_ROLE_ADMIN="ADMIN";constgetUserRoleFromContext=()=>{returncontext().get("pgSettings").get("myapp.user.userRole");};constuserHasAdminRole=(userRole: string)=>{if(userRole!==USER_ROLE_ADMIN){thrownewGraphQLError("User does not have the correct role to access this resource.",);}};// I've added :any for the plans here, since the proper type: SmartFieldPlanResolver is not exported and TS doesn't infer it correctly.exportconstAuthorizationPlugin=makeWrapPlansPlugin({Mutation: {createCustomer(plan: any){sideEffect(getUserRoleFromContext(),userHasAdminRole);returnplan();},editCustomerById(plan: any){sideEffect(getUserRoleFromContext(),userHasAdminRole);returnplan();},},});
The text was updated successfully, but these errors were encountered:
After considering multiple solutions (that were known to me) including a schema-first approach with directives
I've started discussing with @benjie where he pointed me to some great alternatives for achieving authorization on root fields.
His proposal:
With
currentUserHasWriteAccessToCustomerId
being:I settled on an implementation that worked for my use case, which is as follows:
The text was updated successfully, but these errors were encountered: