menu
up-caret
down-caret

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

# Apollo Server

All things Apollo Server.

Trending conversations
👋 Welcome to the Apollo community!
@hwillson · 4d
What's up with the docs?
@boris-da-web-dev · 23d
How to use refresh tokens? Looking for a complete guide or repo for Apollo…
@stupid-sexy-jake · 2h
How do you guys handle authentication?
@lewix · 31d
We will be getting a new API for apollo-react using hooks soon?
@travers · 19d

How to run Apollo Server Lambda locally?

Apollo/Apollo Server · February 19, 2019 at 2:29pm

How to run Apollo Server Lambda locally?

February 19, 2019 at 2:29pm

I have an existing Apollo Server project, but I am working on moving it over to Lambda. I love that I can keep the exact same code and just swap out the server.

However, I know there are "limitations" when running lambda locally and createHandler expects lambda.APIGatewayProxyEvent and lambda.Context.

Serverless lets you run the command sls invoke local and pass data, but when I run it locally it expects a POST request and some additional data. So I created a JSON file and placed the following:

{
"resource": "/",
"path": "/graphql",
"httpMethod": "POST"
}

This lets me run the following command:

serverless invoke local --function graphql --path data/graphql/queries/hello-world.json

which then complains about not having a body and returns the following:

{
"body": "POST body missing.",
"statusCode": 500,
"headers": {}
}

This leaves me with a few major questions:

If Lambda is supposed to run in one off requests, how do you handle local development?

With the nature of Lambda, it takes an event, executes and completes, should I have a check for the env and run a different handler? Ideally when I am more comfortable with node I am going to switch back to TDD and this will be less of an issue. However, right now learning GraphQL and node has me moving a little slower than I would prefer.

Can the documentation from Apollo GraphQL be updated

Maybe some additional documentation on the Apollo Server Lambda page would be helpful (especially for people new to node like myself)? I know this straddles the fence between the serverless framework and Apollo GraphQL but I think its needed (if I figure it out on my own and find a repo for the docs I will make a PR).


February 19, 2019 at 4:21pm

Hi Jason, I doing exactly that. But to run a locals serverless environment you should start with sls offline

  • reply
  • like

For this you need at least the serverless-offline-plugin

like-fill
1
  • reply
  • like

The approach you were taking is not bad, but running the invoke local for every request is time consuming and not very practical

like-fill
1
  • reply
  • like

Let me know if you need more detailed steps for making it work. Good luck! 👍🏻

  • reply
  • like

Alright @cdelgado I owe you a beer/coffee/drink of choice if I ever run into you!

  • reply
  • like

This might be another simple answer @cdelgado but how would I access envrionment variables from inside the function? I have them defined in the function inside serverless.yml but I am at a loss on how to call those inside of the lambda function.

  • reply
  • like

Hey, luckily that's easy too, in the custom section of the serverless.yml you can define vars and access them from within your code with process.env.var_name

  • reply
  • like

so, for instance, the stage could be set in the yaml file as follows: custom: stage: ${opt:stage, self:provider.stage}

  • reply
  • like

and be referenced in your files (in my case using typescript), like this: const getStage = () => { return (typeof process.env.stage !== 'undefined') ? process.env.stage : 'no_stage' }

  • reply
  • like

hope you can go on from here, any doubts, just ask :)

  • reply
  • like

I thought it would be the normal process.env, I defined those in custom in the serverless.yml but then ran the serverless offline and when I console log process.env its undefined

  • reply
  • like

February 20, 2019 at 8:16am

That shouldn't be like that, can you post your serverless.yml file?

  • reply
  • like

Also, you can always attach a debugger to the serverless process, as it's a nodejs server running locally

  • reply
  • like

I think you may need to list the variables in an environment section of your serverless.yml. I highly recommend putting them in the custom section first and then referring to those definitions in your environment section.

  • reply
  • like

To be clear on the environment section you can set it on the top level or you could set it on a per function basis (inside your graphql function definition)

  • reply
  • like

February 21, 2019 at 4:58pm

I'm using serverless offline as well, do either of you guys have issues with running out of memory? Particularly loading the schema seems to leak a ton of memory.

  • reply
  • like

I have not had any issues with it running out of memory...

  • reply
  • like

How large is your schema?

  • reply
  • like

not that big, maybe 30 types

  • reply
  • like

that seems to leak memory, but really every request leaks

  • reply
  • like

yeah.. there is something funny with something then. I have way more types than that and don't have that issue...

  • reply
  • like

what does you lambda function look like?

  • reply
  • like

are you using typescript?

  • reply
  • like

no...

  • reply
  • like

do you think it has something to do with your typings?

  • reply
  • like
Show more messages