stacker.news/pages/api/graphql.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

import { ApolloServer } from '@apollo/server'
import { startServerAndCreateNextHandler } from '@as-integrations/next'
2021-03-25 19:29:24 +00:00
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 {
2023-07-27 00:18:42 +00:00
executionDidStart () {
2022-04-28 18:11:05 +00:00
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(`Field ${info.parentType.name}.${info.fieldName} failed with ${error}`)
2022-04-28 18:11:05 +00:00
}
}
2023-07-27 00:18:42 +00:00
},
async executionDidEnd (err) {
if (err) {
console.error('hey bud', err)
}
2022-04-28 18:11:05 +00:00
}
}
}
}
}
}]
})
export default startServerAndCreateNextHandler(apolloServer, {
context: async (req) => {
2021-04-12 18:05:09 +00:00
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
})