stacker.news/pages/api/graphql.js

55 lines
1.4 KiB
JavaScript

import { ApolloServer } from 'apollo-server-micro'
import resolvers from '../../api/resolvers'
import models from '../../api/models'
import lnd from '../../api/lnd'
import typeDefs from '../../api/typeDefs'
import { getSession } from 'next-auth/client'
import search from '../../api/search'
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
plugins: [{
requestDidStart (initialRequestContext) {
return {
executionDidStart (executionRequestContext) {
return {
willResolveField ({ source, args, context, info }) {
const start = process.hrtime.bigint()
return (error, result) => {
const end = process.hrtime.bigint()
const ms = (end - start) / 1000000n
if (ms > 20) {
console.log(`Field ${info.parentType.name}.${info.fieldName} took ${ms}ms`)
}
if (error) {
console.log(`It failed with ${error}`)
}
}
}
}
}
}
}
}],
context: async ({ req }) => {
const session = await getSession({ req })
return {
models,
lnd,
me: session
? await models.user.findUnique({ where: { id: session.user?.id } })
: null,
search
}
}
})
export const config = {
api: {
bodyParser: false
}
}
export default apolloServer.createHandler({ path: '/api/graphql' })