Closed srosato closed 2 years ago
Can you try subsequent prisma client calls instead of nested include
? That's what typgraphql-prisma
resolvers are doing.
Do you mean something like this?
import { prisma } from './prisma'
const main = async () => {
const user = await prisma.user.findFirst({
where: {
id: 'eid-1'
}
});
console.log(user?.id, user?.pin); //outputs pin 9999
const jobPosition = await prisma.jobPosition.findFirst({
select: {
user: true
},
where: {
user: {
id: {
equals: 'eid-1'
}
}
}
})
console.log(jobPosition?.user.pin); //does NOT output pin 9999 (outputs encrypted version)
};
main().catch((e) => {
console.error(e);
process.exit(1);
});
Then in this case if I use select to get the user, it is not encrypted. Probably has nothing to do with typegraphql then :thinking:
Yes 😉 So you need to adjust your middleware or report bug in Prisma repo
For reference, I created a PR on prisma-field-encryption, as I was able to reproduce using the repo itself.
@MichalLytek Sry to bother again. Can you give me some insights as to how graphql queries are being translated behind when using relations?
Following my merged PR on prisma-field-encryption
, I thought I would fix my issue by making sure the select
portion of the query would be taken into consideration. And with raw prisma, I can now do:
const jobPosition = await prisma.jobPosition.findFirst({
select: {
user: {
select: {
pin: true,
}
}
},
where: {
user: {
id: {
equals: 'eid-1'
}
}
}
})
console.log(jobPosition?.user.pin); //correctly outputs uncrypted pin
but if I do:
query {
jobPositions {
user {
pin // still unencrypted
}
}
}
Any insights would help. I tried navigating the code but I am not sure to what my graphql queries essentially translate to behind so I can properly fix the issue in prisma-field-encryption. Thanks!
I found a workaround for anybody stumbling in the same issue of unencrypting a nested value with prisma-field-encryption
. Probably not ideal, but here it is:
@Resolver((of) => User)
export class UserResolver {
/*
* This is a workaround, as I am not sure how typegraphql-prisma handles queries. Nested resolvers that
* need the pin found themselves with an encrypted value instead of an unencrypted one. This works around that,
* although not performant.
*/
@FieldResolver((type) => String)
async pin(@Root() user: User, @Ctx() {prisma}: Context): Promise<string> {
const foundUser = await prisma.user.findUnique({ where: { id: user.id } });
return foundUser?.pin || user.pin
}
Instead of relying on nested queries, I make sure to do another one no matter where I request for the encrypted field in my graphql queries.
Describe the Bug I seem to be having trouble using prisma-field-encryption middleware on nested resolvers. Nested fields do not get decrypted. Only top level ones.
To Reproduce Add
prisma-field-encryption
middleware. Create quick schema with@encrypted
annotation (see my code below)Expected Behavior I expect nested fields to be decrypted (therefore passing through the middleware correctly)
Environment (please complete the following information):
typegraphql-prisma
0.21.5Additional Context
I have this prisma client (as singleton):
Passed as context to this graphql-yoga server:
I have this schema (simplified for this submission purpose)
I have this query:
I get this response:
Notice the nested field not being properly decrypted, but the top level is. Not sure what I'm doing wrong here. I tried using prisma directly:
And was able to see the nested field being decrypted successfully