1

Imagine the condition that I have a query called "users" that returns all the users and these users can be associated with one or more companies, so I have a type UserCompanies (I need it because it saves some more information beyond the relation). I'm using Prisma and I need to force a filter that returns only users that are of the same company as the requester. I get the information of the company from JWT and need to inject this to the query before sending it to Prisma.

So, query should be like that:

query allUsers {
  users {
    name
    id
    status
    email
    userCompanies{
      id
      role
    }
  }
}

and on server side, I should transform it to: (user where is ok, just changing args)

query allUsers {
  users(where: {
    userCompanies_some: {
      companyId: "companyId-from-jwt"
    }
  }) {
    name
    id
    status
    email
    userCompanies(where: {
      companyId: "companyId-from-jwt"
    }){
      id
      role
    }
  }
}

I'm seeing a few resolutions to this, but I don't know if it is the best way: 1 - Using addFragmentToInfo, does the job to put conditions on the query, but if the query has a usercompanies already set, it gives me a conflict. Otherwise, it works fine. 2 - I can use an alias for the query, but after DB result I will need to edit all the results in array to overwrite the result. 3 - don't use info on Prisma and filter in js. 4 - Edit info(4th parameter) of type GraphqlResolveInfo

Smita Kagwade
  • 281
  • 1
  • 4
  • 12
arceliver
  • 336
  • 1
  • 11
  • I'm sorry, the problem is not how to get the id, is how to edit the received query to that one with filters. But thanks! – arceliver May 02 '20 at 23:59
  • You can check if `addFragmentToInfo` usage is required (userCompanies already requested or not, check if already provided id matches - validation) - no conflict? – xadm May 03 '20 at 01:06
  • Yes, I can check if is provided, but if it is, it will not be with the where condition. So, when I create it via addFragmentInfo it leads me to the conflict. – arceliver May 03 '20 at 04:12
  • You can pass `companyId` from `users` to `userCompanies` resolver ... where you can check if it exists in parent arg an use it ... combine with existing args (user id)/replace them.... if still not enough enum more precisely use cases? – xadm May 03 '20 at 14:12
  • The problem is that when using Prisma, you don't need to pass the attributes through resolvers, you can simply pass a info with nested values to fetch and Prisma take care of all. I did end up with a resolution by extending "addFragmentToInfo" of graphql-binding package, I will make some changes and then will post here. Thank you – arceliver May 04 '20 at 16:15

0 Answers0