From 934c5021a9b6d15edd1f3aa15d2d90a5a21f96f9 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 28 Apr 2022 13:11:05 -0500 Subject: [PATCH] a few perf enhancements + gql slowlogs --- api/resolvers/user.js | 5 ++--- api/typeDefs/user.js | 1 + components/header.js | 4 ++-- fragments/users.js | 4 +--- pages/api/graphql.js | 38 ++++++++++++++++++++++++++++++-------- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 7f7ee057..7a53c4d9 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -167,10 +167,10 @@ export default { return getItem(user, { id: user.bioId }, { models }) }, hasInvites: async (user, args, { models }) => { - const anInvite = await models.invite.findFirst({ + const invite = await models.invite.findFirst({ where: { userId: user.id } }) - return !!anInvite + return !!invite }, hasNewNotes: async (user, args, { me, models }) => { const lastChecked = user.checkedNotesAt || new Date(0) @@ -183,7 +183,6 @@ export default { JOIN "ItemAct" on "ItemAct"."itemId" = "Item".id WHERE "ItemAct"."userId" <> $1 AND "ItemAct".created_at > $2 - AND "ItemAct".act <> 'BOOST' AND "Item"."userId" = $1 LIMIT 1`, user.id, lastChecked) if (votes.length > 0) { diff --git a/api/typeDefs/user.js b/api/typeDefs/user.js index 9ae7a878..83404857 100644 --- a/api/typeDefs/user.js +++ b/api/typeDefs/user.js @@ -47,6 +47,7 @@ export default gql` hasInvites: Boolean! tipDefault: Int! bio: Item + bioId: Int sats: Int! upvotePopover: Boolean! tipPopover: Boolean! diff --git a/components/header.js b/components/header.js index c92d25c8..da953045 100644 --- a/components/header.js +++ b/components/header.js @@ -48,7 +48,7 @@ export default function Header ({ sub }) { profile - {me && !me.bio && + {me && !me.bioId &&
{' '}
} @@ -78,7 +78,7 @@ export default function Header ({ sub }) { signOut({ callbackUrl: '/' })}>logout - {me && !me.bio && + {me && !me.bioId && {' '} } diff --git a/fragments/users.js b/fragments/users.js index 35d21abf..d41371ba 100644 --- a/fragments/users.js +++ b/fragments/users.js @@ -13,9 +13,7 @@ export const ME = gql` freeComments hasNewNotes tipDefault - bio { - id - } + bioId hasInvites upvotePopover tipPopover diff --git a/pages/api/graphql.js b/pages/api/graphql.js index 26d8c26d..eb88478a 100644 --- a/pages/api/graphql.js +++ b/pages/api/graphql.js @@ -6,16 +6,32 @@ import typeDefs from '../../api/typeDefs' import { getSession } from 'next-auth/client' import search from '../../api/search' -const plugin = { - serverWillStart (ctx) { - console.log('gql server starting up') - } -} - const apolloServer = new ApolloServer({ typeDefs, 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 }) => { const session = await getSession({ req }) 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' })