maticzav/graphql-shield

How to handle pagination

Closed this issue ยท 5 comments

Question about GraphQL Shield

I'm using Prisma and the way I'm handling pagination is this one:

getBranchOffices(where: BranchOfficeWhereInput, orderBy: BranchOfficeOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): BranchOfficePayload

type BranchOfficePayload {
  branchOffices: [BranchOffice]!
  count: Int!
}

type BranchOffice {
  id: Int!
  name: String!
  ...
}
async function getBranchOffices(parent, args, context, info) {
    const branchOffices = await context.prisma.branchOffices(args);
    const count = await context.prisma
      .branchOfficesConnection({ where: args.where })
      .aggregate()
      .count()
    return {
      branchOffices,
      count,
    }
}

And this is how I handle permissions:
There are branch offices that need to be shown to some users but be hidden for others. So what we do is create a permission for every branch e.g. BRANCH_1, BRANCH_2.

const canViewBranchOffice = rule({ cache: 'strict' })(async (parent, args, context, info) => {
  const branchId = parent.id
  const userPermissions = context.request.user;
  const hasPermission = userPermissions.find(({name}) => {
    return name.toLowerCase().includes(`branch_${branchId}`);
  })
  return !!hasPermission;
});

module.exports = {
  isAuthenticated,
  canViewBranchOffice
};

const permissions = shield(
  {
    Query: {
      '*': rules.isAuthenticated
    },
    Mutation: {
      '*': rules.isAuthenticated
    },
    BranchOffice: rules.canViewBranchOffice
  }
);

My main problem is that I don't know how to update count with the total number of branch offices the user has access to.

Also, this is my first time trying to implement authorization, at least in the way I described above, so I'm not sure if it is the correct approach, so I'm open to sugestions

  • I have checked other questions and found none that matches mine.

Hey @HugoLiconV ๐Ÿ‘‹,

Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our Open Collective and consider contributing financially.

https://opencollective.com/graphql-shield

PS.: We offer priority support for all financial contributors. Don't forget to add priority label once you start contributing ๐Ÿ˜„

Having similar problems, any update in the issue?

I was just looking for something similar, any solution?

Hey @HugoLiconV ๐Ÿ‘‹,

Thank you for posting the question and for being so patient with my response. Have you by chance thought of introducing a new relationship that would determine who has access to which branches? This way you could filter branches before paginating them.

Do you think that could work?

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.