fix (api): change the way prisma is passed to resolvers

This commit is contained in:
MTG2000
2022-03-24 17:45:21 +02:00
parent 3fa3de1c2b
commit 90e0ce7c33
5 changed files with 30 additions and 32 deletions

View File

@@ -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,
};
},
});

View File

@@ -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

View File

@@ -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 }
})

View File

@@ -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,

View File

@@ -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");