Samson Ssali
04/04/2021, 2:40 AMerror: sorry, too many clients already
can i expect this in production or its a dev only bug? i know it's as a result of nextjs' fast-refresh
which only occurs in the dev environment ... but i'm just sking to be sure.Ryan
04/06/2021, 6:19 AMSamson Ssali
04/06/2021, 9:37 AMRyan
04/06/2021, 9:44 AMSamson Ssali
04/06/2021, 10:07 AMCannot read property 'findUnique' of undefined:
Ryan
04/06/2021, 10:08 AMprisma generate
and check again?Samson Ssali
04/06/2021, 10:15 AMSamson Ssali
04/06/2021, 10:16 AMyarn prisma generate
same errorRyan
04/06/2021, 10:18 AMprisma
available in the context? Could you log that and check?Samson Ssali
04/06/2021, 10:22 AMSamson Ssali
04/06/2021, 10:23 AM// prisma/index.ts
import { PrismaClient } from '@prisma/client'
declare global {
var prisma: PrismaClient
}
let prisma: PrismaClient
// check to use this workaround only in development and not in production
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.prisma) {
global.prisma = new PrismaClient()
}
prisma = global.prisma
}
export default prisma
Samson Ssali
04/06/2021, 10:24 AMSamson Ssali
04/06/2021, 10:25 AM// nexus/context.ts
require('dotenv').config()
// import { PubSub } from 'apollo-server'
import { PostgresPubSub } from "graphql-postgres-subscriptions";
import { Client } from "pg";
import { NextApiRequest, NextApiResponse } from 'next'
import jwt from 'jsonwebtoken'
import cookie from 'cookie'
import { PrismaClient } from "@prisma/client";
import prisma from 'prisma'
console.log(prisma)
const jwtRefreshToken = process.env.JWT_REFRESH_TOKEN
const jwtAuthToken = process.env.JWT_AUTH_TOKEN
export interface Context{
prisma: PrismaClient;
pubsub: PostgresPubSub;
req: NextApiRequest;
res: NextApiResponse;
phone?: string;
}
// const pubsub = new PubSub()
const connectionString = process.env.DATABASE_URL;
const client = new Client({
connectionString: connectionString
});
client.connect();
const pubsub = new PostgresPubSub({ client }); // const pubsub = new PubSub()
export const context = async({ req, res }): Promise<Context> => {
const {accessToken,refreshToken} =req.cookies
// refresh jwt
jwt.verify(refreshToken, jwtRefreshToken, (err, decoded) => {
if (!err&&decoded) {
const { data } = (decoded)
const accessToken = jwt.sign({data},jwtAuthToken,{expiresIn:'5m'})
res.setHeader('Set-Cookie', cookie.serialize('accessToken', accessToken,{
path:"/",
maxAge: 5 * 60,
sameSite: 'strict',
httpOnly: true,
secure:process.env.NODE_ENV!=='development'
}))
} else {
res.setHeader('Set-Cookie', cookie.serialize('accessToken', '',{
path:"/",
expires: new Date(0),
sameSite: 'strict',
httpOnly: true,
secure:process.env.NODE_ENV!=='development'
}))
}
})
// authenticate
let phone=''
jwt.verify(accessToken, jwtAuthToken, async (err, decoded) => {
if (!err&&decoded) {
const { data } = (decoded)
phone=data
}
})
return{ prisma, pubsub, req, res,phone }
}
Samson Ssali
04/06/2021, 10:27 AMRyan
04/06/2021, 10:32 AMprisma
is defined where you are returning the context?
We also have an example that does the same.Samson Ssali
04/06/2021, 11:15 AMdeclare global {
var prisma: PrismaClient
}
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.prisma) {
global.prisma = new PrismaClient()
}
prisma = global.prisma
}
Samson Ssali
04/06/2021, 11:18 AMprisma
scope. i was declaring it in multiple scopes ... and at some point it assumed a value of {}
so here i only declare it in the global scope and then initialize it accordingly