From 90e0ce7c33e1ebd44e5c1cbe529c58ce665f5f95 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Thu, 24 Mar 2022 17:45:21 +0200 Subject: [PATCH] fix (api): change the way prisma is passed to resolvers --- functions/graphql/index.js | 4 ---- functions/graphql/prisma/index.js | 15 +++++++-------- functions/graphql/types/category.js | 11 ++++++----- functions/graphql/types/project.js | 25 +++++++++++++------------ functions/graphql/types/vote.js | 7 ++++--- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/functions/graphql/index.js b/functions/graphql/index.js index 850d5dd..77596d9 100644 --- a/functions/graphql/index.js +++ b/functions/graphql/index.js @@ -1,7 +1,4 @@ const { ApolloServer } = require("apollo-server-lambda"); -// const resolvers = require("./resolvers/resolvers"); -// const typeDefs = require("./schema"); -const { prisma } = require('./prisma') const schema = require('./schema') @@ -10,7 +7,6 @@ const server = new ApolloServer({ context: () => { return { - prisma, }; }, }); diff --git a/functions/graphql/prisma/index.js b/functions/graphql/prisma/index.js index c8b45dd..5057359 100644 --- a/functions/graphql/prisma/index.js +++ b/functions/graphql/prisma/index.js @@ -1,15 +1,14 @@ const { PrismaClient } = require('@prisma/client') -let prisma +let prisma; -if (process.env.NODE_ENV === 'production') { - prisma = new PrismaClient() -} else { - if (!global.prisma) { - global.prisma = new PrismaClient() - } - prisma = global.prisma +if (!global.prisma) { + console.log("New Prisma Client"); + global.prisma = new PrismaClient({ + log: ["info"], + }); } +prisma = global.prisma; module.exports = { prisma diff --git a/functions/graphql/types/category.js b/functions/graphql/types/category.js index ae2edd4..1c888d4 100644 --- a/functions/graphql/types/category.js +++ b/functions/graphql/types/category.js @@ -4,6 +4,7 @@ const { extendType, nonNull, } = require('nexus'); +const { prisma } = require('../prisma') const Category = objectType({ @@ -16,13 +17,13 @@ const Category = objectType({ t.nonNull.int('votes_sum', { - async resolve(parent, _, { prisma }) { + async resolve(parent) { const projects = await prisma.category.findUnique({ where: { id: parent.id } }).project(); return projects.reduce((total, project) => total + project.votes_count, 0); } }); t.nonNull.int('apps_count', { - async resolve(parent, _, { prisma }) { + async resolve(parent) { const projects = await prisma.category.findUnique({ where: { id: parent.id } }).project(); return projects.length; @@ -31,7 +32,7 @@ const Category = objectType({ t.nonNull.list.nonNull.field('project', { type: "Project", - resolve: (parent, _, { prisma }) => { + resolve: (parent) => { return parent.project ?? prisma.category.findUnique({ where: { id: parent.id } }).project() @@ -45,7 +46,7 @@ const allCategoriesQuery = extendType({ definition(t) { t.nonNull.list.nonNull.field('allCategories', { type: "Category", - resolve: async (parent, args, { prisma }) => { + resolve: async () => { const categories = await prisma.category.findMany({ include: { _count: { @@ -70,7 +71,7 @@ const getCategory = extendType({ args: { id: nonNull(intArg()) }, - resolve(parent, { id }, { prisma }) { + resolve(parent, { id }) { return prisma.category.findUnique({ where: { id } }) diff --git a/functions/graphql/types/project.js b/functions/graphql/types/project.js index 9ed4673..678ac3d 100644 --- a/functions/graphql/types/project.js +++ b/functions/graphql/types/project.js @@ -5,6 +5,7 @@ const { extendType, nonNull, } = require('nexus') +const { prisma } = require('../prisma') const { paginationArgs, getLnurlDetails, lightningAddressToLnurl } = require('./helpers'); @@ -25,21 +26,21 @@ const Project = objectType({ t.nonNull.field('category', { type: "Category", - resolve: (parent, args, { prisma }) => { + resolve: (parent) => { return prisma.project.findUnique({ where: { id: parent.id } }).category(); } }); t.nonNull.list.nonNull.field('awards', { type: "Award", - resolve: (parent, args, { prisma }) => { + resolve: (parent) => { return prisma.project.findUnique({ where: { id: parent.id } }).awards(); } }); t.nonNull.list.nonNull.field('tags', { type: "Tag", - resolve: (parent, args, { prisma }) => { + resolve: (parent) => { return prisma.project.findUnique({ where: { id: parent.id } }).tags(); } }) @@ -56,7 +57,7 @@ const Award = objectType({ t.nonNull.string('url'); t.nonNull.field('project', { type: "Project", - resolve: (parent, args, { prisma }) => { + resolve: (parent) => { return prisma.award.findUnique({ where: { id: parent.id } }).project(); } }) @@ -70,7 +71,7 @@ const Tag = objectType({ t.nonNull.string('title'); t.nonNull.list.nonNull.field('project', { type: "Project", - resolve: (parent, args, { prisma }) => { + resolve: (parent) => { return prisma.tag.findUnique({ where: { id: parent.id } }).project(); } }) @@ -86,7 +87,7 @@ const getProject = extendType({ args: { id: nonNull(intArg()) }, - resolve(_, { id }, { prisma }) { + resolve(_, { id }) { return prisma.project.findUnique({ where: { id } }) @@ -101,7 +102,7 @@ const allProjects = extendType({ t.nonNull.list.nonNull.field('allProjects', { type: "Project", args: paginationArgs({ take: 50 }), - resolve(_, { take, skip }, { prisma }) { + resolve(_, { take, skip }) { return prisma.project.findMany({ orderBy: { votes_count: "desc" }, skip, @@ -118,7 +119,7 @@ const newProjects = extendType({ t.nonNull.list.nonNull.field('newProjects', { type: "Project", args: paginationArgs({ take: 50 }), - resolve(_, args, { prisma }) { + resolve(_, args) { const take = args.take || 50; const skip = args.skip || 0; return prisma.project.findMany({ @@ -138,7 +139,7 @@ const hottestProjects = extendType({ t.nonNull.list.nonNull.field('hottestProjects', { type: "Project", args: paginationArgs({ take: 50 }), - async resolve(_, { take, skip }, { prisma }) { + async resolve(_, { take, skip }) { return prisma.project.findMany({ orderBy: { votes_count: "desc" }, skip, @@ -159,7 +160,7 @@ const searchProjects = extendType({ ...paginationArgs({ take: 50 }), search: nonNull(stringArg()) }, - async resolve(_, { take, skip, search }, { prisma }) { + async resolve(_, { take, skip, search }) { return prisma.project.findMany({ where: { OR: [{ @@ -192,7 +193,7 @@ const projectsByCategory = extendType({ ...paginationArgs(), category_id: nonNull(intArg()) }, - async resolve(_, { take, skip, category_id }, { prisma }) { + async resolve(_, { take, skip, category_id }) { return prisma.project.findMany({ where: { category_id }, orderBy: { votes_count: "desc" }, @@ -211,7 +212,7 @@ const getLnurlDetailsForProject = extendType({ t.nonNull.field('getLnurlDetailsForProject', { type: "LnurlDetails", args: { project_id: nonNull(intArg()) }, - async resolve(_, args, { prisma }) { + async resolve(_, args) { const project = await prisma.project.findUnique({ where: { id: args.project_id, diff --git a/functions/graphql/types/vote.js b/functions/graphql/types/vote.js index 8d1535f..cb79cfd 100644 --- a/functions/graphql/types/vote.js +++ b/functions/graphql/types/vote.js @@ -8,6 +8,7 @@ const { const { parsePaymentRequest } = require('invoices'); const { getPaymetRequestForProject, hexToUint8Array } = require('./helpers'); const { createHash } = require('crypto'); +const { prisma } = require('../prisma') @@ -24,7 +25,7 @@ const Vote = objectType({ t.nonNull.field('project', { type: "Project", - resolve: (parent, args, { prisma }) => { + resolve: (parent, args,) => { return parent.project ?? prisma.vote.findUnique({ where: { id: parent.id } }).project() @@ -53,7 +54,7 @@ const voteMutation = extendType({ project_id: nonNull(intArg()), amount_in_sat: nonNull(intArg()) }, - resolve: async (_, args, { prisma }) => { + resolve: async (_, args) => { const project = await prisma.project.findUnique({ where: { id: args.project_id }, }); @@ -85,7 +86,7 @@ const confirmVoteMutation = extendType({ payment_request: nonNull(stringArg()), preimage: nonNull(stringArg()) }, - resolve: async (_, args, { prisma }) => { + resolve: async (_, args) => { const paymentHash = createHash("sha256") .update(hexToUint8Array(args.preimage)) .digest("hex");