Hi, I have a question regarding prisma and graphQL...
# orm-help
a
Hi, I have a question regarding prisma and graphQL. I would like to create a query that would be able to filter a table with regards to different custom enum types, that would respond to things such as category or status. So far I managed to implement it only in the case that all different filter options are passed, but I don't seem to figure out a way to leave some of those blank and still be able to perform the query on Prisma syntax. This is the query I wrote so far:
Copy code
const filterSuggestions = async (_, { input }, { db, me, utils }) => {
    try {
        if (!input.archived) {
            input.archived = false;
        }

        return await db.suggestion.findMany({
            where: {
                AND: [
                    { status: input.status },
                    { category: input.category },
                    { priority: input.priority },
                    { archived: input.archived }
                ]
            }
        });
So far, as you can see only if input contains a valid value for all of the filter options it will work. Is there a way to implement it correctly?
r
you can try:
Copy code
AND: [
                 ...(input.status &&   { status: input.status }),
                 
                ]
so it would look like:
Copy code
const filterSuggestions = async (_, { input }, { db, me, utils }) => {
    try {
        if (!input.archived) {
            input.archived = false;
        }
        return await db.suggestion.findMany({
            where: {
                AND: [
                    ...(input.status && { status: input.status }),
                    ...(input.category && { category: input.category }),
                    ...(input.priority && { priority: input.priority }),
                    ...(input.archived && { archived: input.archived )}
                ]
            }
        });
sometimes that plays all kinds of havok with my ts-server . You can also break the query out to it's own object:
Copy code
const filterSuggestions = async (_, { input }, { db, me, utils }) => {
    try {
        if (!input.archived) {
            input.archived = false;
        }

        const queryAnd = [];
        if(input.status) {
            queryParams.push({status: input.status})
        }
        if(input.category) {
            queryParams.push({category: input.category})
        }
        //etc for the other params
        return await db.suggestion.findMany({
            where: {
                AND: queryAnd
            }
        });