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

Hi @futuregerald - I realize this is an old topic, but you mentioned that the default timeout for functions was 10 seconds…if I wanted to increase that to 15 seconds, can I do that?

hi simon, are you a pro tier customer? we can bump up your functions timeout to 26 secs if yes. If you are not pro, you might consider upgrading to get the longer timeout.

Let us know which netlify site (either API ID or site name, not the custom domain) this is regarding, too.

HI Perry

I’m not a Pro tier yet…I’m a one-person operation still developing without wanting/needing users yet…but hoping to really go live with something in a few months, so will probably upgrade then.

App Id is ed91e416-971c-419c-9c42-b091b8351ee1

[url is https://deepdeepsheet.com if you want to take a look - it’s a spreadsheet for words, (not numbers), that allows you to easily create subsheets from a parent sheet (hence the name). Still under development.]

Thanks!
Simon

1 Like

sounds awesome! if you’d like to upgrade to test, and then downgrade until you are ready to hit the bigtime, we can do that also, it would just incur a one-month pro charge ($19).

Hi @perry

I just made another post about my issue as I’m thinking that needing more than 10 seconds is not really the problem, but I would like to try upgrading for a month then downgrading again to test…anything I need to do in particular to accomplish that?

thanks
Simon

hi simon,

if you go ahead and upgrade to Pro Tier in our UI, and then let me know here, we can set your function timeout to 26 seconds and see if that helps!

Thanks @perry just upgraded - if you could increase the timeout to 26 seconds, that’d be great

hey there, you are now at 26 seconds. i hope this helps!

Thanks, @perry
FYI for anyone reading this in future: initially I thought that my service hadn’t been updated to the 26 second timeout because I still was getting 10s timeout errors…but then I updated & redeployed my code (just to add some more logging) and then I saw it was allowing longer than 10s

…and that solved the issue! Looks like some calls related to OAuth with Google were taking a bit over 11 seconds. Thanks for your help on this, Perry

cheers
Simon

1 Like