refine module instantiation
This commit is contained in:
parent
2dba0b3ffe
commit
8e0aaab161
|
@ -1,16 +1,20 @@
|
|||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
if (!global.prisma) {
|
||||
global.prisma = new PrismaClient({
|
||||
const prisma = global.prisma || (() => {
|
||||
console.log('initing prisma')
|
||||
const prisma = new PrismaClient({
|
||||
log: [{ level: 'query', emit: 'event' }, 'warn', 'error']
|
||||
})
|
||||
global.prisma.$on('query', (e) => {
|
||||
prisma.$on('query', (e) => {
|
||||
if (e.duration > 50) {
|
||||
console.log('Query: ' + e.query)
|
||||
console.log('Params: ' + e.params)
|
||||
console.log('Duration: ' + e.duration + 'ms')
|
||||
}
|
||||
})
|
||||
}
|
||||
return prisma
|
||||
})()
|
||||
|
||||
export default global.prisma
|
||||
if (process.env.NODE_ENV === 'development') global.prisma = prisma
|
||||
|
||||
export default prisma
|
||||
|
|
|
@ -6,10 +6,16 @@ import typeDefs from '../../api/typeDefs'
|
|||
import { getSession } from 'next-auth/client'
|
||||
import search from '../../api/search'
|
||||
|
||||
global.apolloServer ||= new ApolloServer({
|
||||
const plugin = {
|
||||
serverWillStart (ctx) {
|
||||
console.log('gql server starting up')
|
||||
}
|
||||
}
|
||||
|
||||
const apolloServer = new ApolloServer({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
tracing: true,
|
||||
plugins: [plugin],
|
||||
context: async ({ req }) => {
|
||||
const session = await getSession({ req })
|
||||
return {
|
||||
|
@ -23,10 +29,4 @@ global.apolloServer ||= new ApolloServer({
|
|||
}
|
||||
})
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false
|
||||
}
|
||||
}
|
||||
|
||||
export default global.apolloServer.createHandler({ path: '/api/graphql' })
|
||||
module.exports = apolloServer.start().then(() => apolloServer.createHandler({ path: '/api/graphql' }))
|
||||
|
|
Loading…
Reference in New Issue