a few perf enhancements + gql slowlogs

This commit is contained in:
keyan 2022-04-28 13:11:05 -05:00
parent 8e0aaab161
commit 934c5021a9
5 changed files with 36 additions and 16 deletions

View File

@ -167,10 +167,10 @@ export default {
return getItem(user, { id: user.bioId }, { models }) return getItem(user, { id: user.bioId }, { models })
}, },
hasInvites: async (user, args, { models }) => { hasInvites: async (user, args, { models }) => {
const anInvite = await models.invite.findFirst({ const invite = await models.invite.findFirst({
where: { userId: user.id } where: { userId: user.id }
}) })
return !!anInvite return !!invite
}, },
hasNewNotes: async (user, args, { me, models }) => { hasNewNotes: async (user, args, { me, models }) => {
const lastChecked = user.checkedNotesAt || new Date(0) const lastChecked = user.checkedNotesAt || new Date(0)
@ -183,7 +183,6 @@ export default {
JOIN "ItemAct" on "ItemAct"."itemId" = "Item".id JOIN "ItemAct" on "ItemAct"."itemId" = "Item".id
WHERE "ItemAct"."userId" <> $1 WHERE "ItemAct"."userId" <> $1
AND "ItemAct".created_at > $2 AND "ItemAct".created_at > $2
AND "ItemAct".act <> 'BOOST'
AND "Item"."userId" = $1 AND "Item"."userId" = $1
LIMIT 1`, user.id, lastChecked) LIMIT 1`, user.id, lastChecked)
if (votes.length > 0) { if (votes.length > 0) {

View File

@ -47,6 +47,7 @@ export default gql`
hasInvites: Boolean! hasInvites: Boolean!
tipDefault: Int! tipDefault: Int!
bio: Item bio: Item
bioId: Int
sats: Int! sats: Int!
upvotePopover: Boolean! upvotePopover: Boolean!
tipPopover: Boolean! tipPopover: Boolean!

View File

@ -48,7 +48,7 @@ export default function Header ({ sub }) {
<Link href={'/' + me?.name} passHref> <Link href={'/' + me?.name} passHref>
<NavDropdown.Item> <NavDropdown.Item>
profile profile
{me && !me.bio && {me && !me.bioId &&
<div className='p-1 d-inline-block bg-secondary ml-1'> <div className='p-1 d-inline-block bg-secondary ml-1'>
<span className='invisible'>{' '}</span> <span className='invisible'>{' '}</span>
</div>} </div>}
@ -78,7 +78,7 @@ export default function Header ({ sub }) {
<NavDropdown.Divider /> <NavDropdown.Divider />
<NavDropdown.Item onClick={() => signOut({ callbackUrl: '/' })}>logout</NavDropdown.Item> <NavDropdown.Item onClick={() => signOut({ callbackUrl: '/' })}>logout</NavDropdown.Item>
</NavDropdown> </NavDropdown>
{me && !me.bio && {me && !me.bioId &&
<span className='position-absolute p-1 bg-secondary' style={{ top: '5px', right: '0px' }}> <span className='position-absolute p-1 bg-secondary' style={{ top: '5px', right: '0px' }}>
<span className='invisible'>{' '}</span> <span className='invisible'>{' '}</span>
</span>} </span>}

View File

@ -13,9 +13,7 @@ export const ME = gql`
freeComments freeComments
hasNewNotes hasNewNotes
tipDefault tipDefault
bio { bioId
id
}
hasInvites hasInvites
upvotePopover upvotePopover
tipPopover tipPopover

View File

@ -6,16 +6,32 @@ import typeDefs from '../../api/typeDefs'
import { getSession } from 'next-auth/client' import { getSession } from 'next-auth/client'
import search from '../../api/search' import search from '../../api/search'
const plugin = {
serverWillStart (ctx) {
console.log('gql server starting up')
}
}
const apolloServer = new ApolloServer({ const apolloServer = new ApolloServer({
typeDefs, typeDefs,
resolvers, resolvers,
plugins: [plugin], 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 }) => { context: async ({ req }) => {
const session = await getSession({ req }) const session = await getSession({ req })
return { return {
@ -29,4 +45,10 @@ const apolloServer = new ApolloServer({
} }
}) })
module.exports = apolloServer.start().then(() => apolloServer.createHandler({ path: '/api/graphql' })) export const config = {
api: {
bodyParser: false
}
}
export default apolloServer.createHandler({ path: '/api/graphql' })