From 34cfa31fe963b4159487d4df9f11c3571c93bd19 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 23 Apr 2022 00:18:53 +0400 Subject: [PATCH 1/4] fix: getFrontpage query --- src/backend/frontpage.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/backend/frontpage.ts b/src/backend/frontpage.ts index 3d41e93..e50ab0f 100644 --- a/src/backend/frontpage.ts +++ b/src/backend/frontpage.ts @@ -3,14 +3,13 @@ import { Question } from "@prisma/client"; import { measureTime } from "./utils/measureTime"; export async function getFrontpage(): Promise { - const questions = await prisma.question.findMany({ - where: { - onFrontpage: { - isNot: null, + const questions = ( + await prisma.frontpageId.findMany({ + include: { + question: true, }, - }, - }); - console.log(questions.length); + }) + ).map((f) => f.question); return questions; } From 38d666fec090c6d43d1547ddd678937b2a7f9ffd Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 23 Apr 2022 00:40:09 +0400 Subject: [PATCH 2/4] feat: filter out non-existent questions on frontpage --- src/backend/frontpage.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/frontpage.ts b/src/backend/frontpage.ts index e50ab0f..beb106d 100644 --- a/src/backend/frontpage.ts +++ b/src/backend/frontpage.ts @@ -9,7 +9,9 @@ export async function getFrontpage(): Promise { question: true, }, }) - ).map((f) => f.question); + ) + .map((f) => f.question) + .filter((q) => q); return questions; } From 66938bcaf41d3cb9f4ba9ab61c4152f90b085323 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 23 Apr 2022 00:40:24 +0400 Subject: [PATCH 3/4] debug: exploratory logging for frontpage --- src/graphql/schema/frontpage.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/graphql/schema/frontpage.ts b/src/graphql/schema/frontpage.ts index 5b7693e..5c137da 100644 --- a/src/graphql/schema/frontpage.ts +++ b/src/graphql/schema/frontpage.ts @@ -7,7 +7,12 @@ builder.queryField("frontpage", (t) => type: [QuestionObj], description: "Get a list of questions that are currently on the frontpage", resolve: async () => { - return await getFrontpage(); + try { + return await getFrontpage(); + } catch (e) { + console.error(e); + throw e; + } }, }) ); From 02cb8089a76829bc705def485347414ee0b62327 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sat, 23 Apr 2022 00:49:52 +0400 Subject: [PATCH 4/4] fix: prisma import, rename global._prisma --- src/backend/database/prisma.ts | 6 +++--- src/backend/frontpage.ts | 1 + src/graphql/schema/frontpage.ts | 7 +------ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/backend/database/prisma.ts b/src/backend/database/prisma.ts index ecaa186..63118ce 100644 --- a/src/backend/database/prisma.ts +++ b/src/backend/database/prisma.ts @@ -3,13 +3,13 @@ import { PrismaClient } from "@prisma/client"; declare global { // allow global `var` declarations // eslint-disable-next-line no-var - var prisma: PrismaClient | undefined; + var _prisma: PrismaClient | undefined; } export const prisma = - global.prisma || + global._prisma || new PrismaClient({ log: ["query"], }); -if (process.env.NODE_ENV !== "production") global.prisma = prisma; +if (process.env.NODE_ENV !== "production") global._prisma = prisma; diff --git a/src/backend/frontpage.ts b/src/backend/frontpage.ts index beb106d..2197b8a 100644 --- a/src/backend/frontpage.ts +++ b/src/backend/frontpage.ts @@ -1,5 +1,6 @@ import { Question } from "@prisma/client"; +import { prisma } from "./database/prisma"; import { measureTime } from "./utils/measureTime"; export async function getFrontpage(): Promise { diff --git a/src/graphql/schema/frontpage.ts b/src/graphql/schema/frontpage.ts index 5c137da..5b7693e 100644 --- a/src/graphql/schema/frontpage.ts +++ b/src/graphql/schema/frontpage.ts @@ -7,12 +7,7 @@ builder.queryField("frontpage", (t) => type: [QuestionObj], description: "Get a list of questions that are currently on the frontpage", resolve: async () => { - try { - return await getFrontpage(); - } catch (e) { - console.error(e); - throw e; - } + return await getFrontpage(); }, }) );