How can I re-write not commented code in one reque...
# orm-help
g
How can I re-write not commented code in one request? I tried to do like in commented code, but I got error
Field "OR" is not defined by type UserWhereUniqueInput.
https://gist.github.com/SilencerWeb/a376b66a519c092e8bdeb8b98dede422
j
As far as I can see what you've done is correct... Looking at my own schema, OR is a valid member of UserWhereInput and expects an array of UserWhereInput. Hopefully someone comes around who's able to help you!
g
yea, i hope that somebody will help me too because i took this concept from tutorial in docs and i was surprised when I got such error
j
Aaaah, I see why.
Either email or nick is unique. You can't do OR'ing on unique keys. In mine I have two unique fields, id and email, and this
Copy code
type UserWhereUniqueInput {
    id: ID
    email: String
}
is everything.
g
yea, i have been thinking about it but i think it is okay to do OR if u have same value for both unique fields
j
It's not part of the schema, so I don't know. I can only use what I can see.
Use context.prisma.query.users instead. OR doesn't exists on the "query.user" schema.
You just have to assert that you only have one. Can be done with
const [user, ...rest] = context.prisma.query.users({your stuff}); if (rest) {shit}
g
u think it is a good idea to use users for searching one user?
j
It's the only user query path that has an OR operator.
Just assert that there was only one with similar code to what I wrote above.
g
how do u think it will be better than do 2 requests or not?
j
I'd probably do the OR query as it's a single query to the database and not two, as in your circumstance.
g
thanks, i did like u said and it works 🙂 but i have one strange thing... here i get an array with one object const user = await context.prisma.query.users( { where: { OR: [ { email: args.login, }, { nickname: args.login }, ], }, }, ); so, then i should use it like user[0] but if I do this, i get undefined, wtf?)) const user = await context.prisma.query.users( { where: { OR: [ { email: args.login, }, { nickname: args.login }, ], }, }, )[0];
is it because it is await function or what?
j
Yes, sorry for the late reply. I'd recommend to do arraay deconstruction. const [user, ...rest] = db.query... user will have your user if it exists and if rest is not undefined then you have a problem... 😛
❤️ 1
g
@Jenkins yep, that works, thank u so much :))