Apollo Server Function Timeout

Hi there!

I’ve setup an Apollo Server connected to a Prisma Cloud instance. It’s all pretty basic, based on the apollo netlify lambda example.

It’s all living in a monorepo with some other packages. I’ve managed to deploy the frontend and functions properly (I have a different function without graphql or anything fancy that works), but when calling to my /.netlify/functions/graphql I receive a timeout after 10.01s:

errorMessage: "2019-05-28T18:41:16.990Z 001f18d7-ed67-4591-8106-53934df574f2 Task timed out after 10.01 seconds"

I have no idea how to debug, it works on my machine!

Any help greatly appreciated!

Hi,

Is the code for your function public? if so then please share it. Also note that by default functions timeout after 10 seconds so it sounds like there’s a method in your function that’s not exiting and is instead timing out.

Hi Gerald,

Thanks for the reply.

My code is not entirely public, but here’s the graphql function code:

const { ApolloServer } = require('apollo-server-lambda')
import { importSchema } from 'graphql-import'
import { prisma } from '../../../database/generated/prisma-client'
import { Repo } from './resolvers/Repo'
import { Deploy } from './resolvers/Deploy'
import { Branch } from './resolvers/Branch'
import { Query } from './resolvers/Query'
import { Mutation } from './resolvers/Mutation'
const path = require('path')

const typeDefs = importSchema(path.resolve('./src/lambda/schema.graphql'))

const resolvers = {
  Repo,
  Deploy,
  Branch,
  Query,
  Mutation,
}

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ context }) => {
    let user
    if (context.clientContext.user) {
      user = prisma.user({ sub: context.clientContext.user.sub })
    }

    return {
      db: prisma,
      user,
    }
  },
  introspection: true,
  playground: true,
})

exports.handler = server.createHandler()

The resolvers are all pretty basic resolvers, like so:

import { RepoResolvers } from '../../generated/graphql-server'
import { MyContext } from '../context'

export const Repo: RepoResolvers = {
  id: (parent, args, ctx) => parent.id,
  name: (parent, args, ctx) => parent.name,
  branches: ({ id }, args, ctx: MyContext) => {
    return ctx.db.repo({ id }).branches()
  },
}

Except the Mutation one has a bit of a nested query:

import { MutationResolvers } from '../../generated/graphql-server'

export const Mutation: MutationResolvers = {
  upsertDeploy: async (
    root,
    { deployId, branchName, url, status, repoName },
    ctx,
  ) => {
    let [repo] = await ctx.db.repoes({
      where: {
        name: repoName,
      },
    })

    const [branch] = await ctx.db.branches({
      where: {
        name: branchName,
        repo: {
          id: (repo && repo.id) || '0',
        },
      },
    })

    await ctx.db.upsertRepo({
      where: {
        id: (repo && repo.id) || '0',
      },
      create: {
        name: repoName,
        branches: {
          create: {
            name: branchName,
            deploys: {
              create: {
                deployId,
                url,
                status,
              },
            },
          },
        },
      },
      update: {
        branches: {
          upsert: {
            where: {
              id: (branch && branch.id) || '0',
            },
            create: {
              name: branchName,
              deploys: {
                create: {
                  deployId,
                  url,
                  status,
                },
              },
            },
            update: {
              deploys: {
                upsert: {
                  where: {
                    deployId,
                  },
                  create: {
                    deployId,
                    url,
                    status,
                  },
                  update: {
                    url,
                    status,
                  },
                },
              },
            },
          },
        },
      },
    })

    return ctx.db.deploy({ deployId })
  },
}

But it works for me locally, and I’m not even calling it in my function that returns the timeout.

Locally running netlify-lambda serve with this code tells me the functions run within a few 100ms. I’ve tried putting a few console.logs in the first file but I’m not getting any of those in the Netlify admin logs.

Let me know if you need more! Thanks.

Update: not sure what I did (I think nothing), but I’m no longer receiving the timeout error.

I now receive an error saying it can’t find the schema.graphql I’m importing using graph-import, a derp from my side! I don’t know if this was causing the timeout, but it does return a HTTP 502 for some reason.

Anyway I can continue debugging!

Keep us updated, @RobertBroersma!

Hey Perry,

Still not sure where the timeout came from, but I switched from using the runtime importSchema graphql-import to the graphql-import-loader for webpack, which includes the schema during compile-time :ok_hand:

That fixed it for me!

1 Like

Perfect - thanks so much for closing the loop. This is super helpful for other users!

1 Like