stacker.news/pages/api/graphql.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-03-25 19:29:24 +00:00
import { ApolloServer } from 'apollo-server-micro'
import resolvers from '../../api/resolvers'
import models from '../../api/models'
2021-04-29 21:58:43 +00:00
import lnd from '../../api/lnd'
2021-03-25 19:29:24 +00:00
import typeDefs from '../../api/typeDefs'
2021-04-12 18:05:09 +00:00
import { getSession } from 'next-auth/client'
2022-01-26 15:35:14 +00:00
import search from '../../api/search'
2023-01-18 18:49:20 +00:00
import slashtags from '../../api/slashtags'
2021-03-25 19:29:24 +00:00
2022-04-27 22:06:42 +00:00
const apolloServer = new ApolloServer({
2021-03-25 19:29:24 +00:00
typeDefs,
resolvers,
2022-04-28 18:11:05 +00:00
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 && info.parentType.name !== 'User') {
2022-04-28 18:11:05 +00:00
console.log(`Field ${info.parentType.name}.${info.fieldName} took ${ms}ms`)
}
if (error) {
console.log(`It failed with ${error}`)
}
}
}
}
}
}
}
}],
2021-04-12 18:05:09 +00:00
context: async ({ req }) => {
const session = await getSession({ req })
return {
models,
2021-04-29 21:58:43 +00:00
lnd,
2022-04-21 22:50:02 +00:00
me: session
? session.user
2022-04-21 22:50:02 +00:00
: null,
2023-01-18 18:49:20 +00:00
search,
slashtags
2021-04-12 18:05:09 +00:00
}
}
2021-03-25 19:29:24 +00:00
})
2022-04-28 18:11:05 +00:00
export const config = {
api: {
bodyParser: false
}
}
2022-11-06 17:28:58 +00:00
const startServer = apolloServer.start()
export default async function handler (req, res) {
await startServer
await apolloServer.createHandler({
path: '/api/graphql'
})(req, res)
}