I'm currently writing something that can filter a list of users. The filter is based on a query which passes a filter
parameter which is then picked up by a resolver which searches through various fields and returns the data.
This works well for text, but prisma has some restrictions in the types of searches that you can do for scalars. So it's hard to see what the best way to search for a boolean would be.
My resolver looks like this:
const listUsers = async (parent, args, context, info) => {
const where = args.filter
? {
OR: [
{ name_contains: args.filter },
{ email_contains: args.filter },
{ phone_contains: args.filter },
{ active: args.filter },
],
}
: {}
return await context.prisma.users({
where,
})
}
And a sample query looks like this:
query {
listUsers(filter: "test@test.com") {
name
email
}
}
In theory, this would work if I simply passed 'TRUE' or 'FALSE' as my filter argument but this obviously doesn't work as more booleans are added to the User type, and also is not very clear.
What is the best way to handle this?
You could split between the filter
(with text) and the options
with boolean and such:
query {
listUsers(filter: "test@test.com", options: {active: true}) {
name
email
}
}
const fields = ['name', 'email', 'phone']
const listUsers = async (parent, args, context, info) => {
const where = {
OR: [
...fields.reduce((acc, field) => {
acc[name+'_contains'] = args.filter
return acc
},{}),
...args.options
]
}
return await context.prisma.users({
where,
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.