WSC-Nettie-Owners / wscnettie

https://www.wscnettie.com
2 stars 0 forks source link

Optimize Query in Quizzes #10

Open Nottommy11 opened 11 months ago

Nottommy11 commented 11 months ago

Navigate to the +page.svelte in Quizzes.

Optimize the queries in the getQuestionsCount and getQuestions functions so they are easier to read and use the netLvl variable to make the queries.

Here is an example:

const myres = client.query({
    Method: 'POST',
    query: gql`
        query getNet${netLvl}QuestionsCount {
            net_${netLvl}_net_questions_aggregate {
                aggregate {
                    count
                }
            }
        }
    `
});

return (await myres).data[`net_${netLvl}_net_questions_aggregate`].aggregate.count;

So basically, we don't want to use the if statements checking netLvl anymore.

kyphillips commented 10 months ago

Can we do this by just having a single query variable that looks for the level, like this

async function getQuestionsCount() { try { const myres = client.query({ Method: 'POST', query: gql query getQuestionsCount($netLvl: Int) { net_questions_aggregate(where: {net_level: {_eq: $netLvl}}){ aggregate { count } } } , variables: { netLvl: netLvl } });

    return (await myres).data.net_questions_aggregate.aggregate.count;
} catch (err) {
    console.log(err);
}

}