Build Netlify lambda function + Apollo gateway together

I’ve been hosting for an e-commerce startup project and building the whole app with Netilfy. I really like Netlify’s simplicity :slight_smile:
As a matter of fact, I’m actually an entry full-stack dev and facing a lot of challenges these days as we integrating apollo graphql.
So here is one of the challenges:
I’m not sure if this is the right way of building multiple lambda functions. (have two functions: product, gateway each has its own folder and package.json)

"scripts": {
    "start": "run-p start:**",
    "start:app": "react-scripts start",
    "start:lambda-product": "netlify-lambda serve src/functions/products -p 9001",
    "start:lambda-gateway": "netlify-lambda serve src/functions/gateway",
    "build": "run-p build:**",
    "build:app": "react-scripts build",
    "build:lambda-product": "netlify-lambda build src/functions/products",
    "build:lambda-gateway": "netlify-lambda build src/functions/gateway",
    "test": "react-scripts test",
    "debug": "react-scripts --inspect-brk test --runInBand --no-cache",
    "eject": "react-scripts eject",
    "dev": "node --inspect ./node_modules/.bin/netlify dev"

This setup anyhow builds and serves both functions(had to add -p since netlify lambda doesn’t’ automatically serve multiple functions in different port)

Is there a way to build in on script like build:lambda: "netlify-lambda build without building each function?

Also, I assume that when it’s actually being deployed Netlify doesn’t use my serve script but uses it’s own proxying. Is that correct?

Hey @peter-wd-1,
In terms of deploying your functions, our buildbot will automatically bundle your functions if you have a setup like this, where this is your root directory:

   |__ App.js
   |__ product.js
   |__ gateway.js
package.json <--- includes dependencies for functions

and netlify.toml declares the functions directory:

   functions = "functions-dir/"

So you can use netlify-lambda for local testing, but I did want to let you know that it’s not necessary for deploying to production. Once you deploy your functions, you can call them from the client side like this (from the Apollo docs):

// src/App.js
import ApolloClient from "apollo-boost";
const client = new ApolloClient({
  uri: "/.netlify/functions/graphql"

Let us know if that helps or if you have follow-up questions! And if there’s a question about a specific/site deploy, please share a URL to those so we can take a look :slight_smile:

He @jen
Thank you for your detailed response :slight_smile:
I’ve been working out with this issue for almost a week… Now I’ve set the whole thing working smoothly with Netlify Dev(not on production deploy though).
I’ve decided to use the script build strategy because as the functions getting lager I thought it would be more navigable to have its own folders for each function:

├── App.js
└── functions
    ├── federated-graphql-a
    │   ├── federated-graphql-a.js
    │   ├── util
    │   │   └── helper.js
    │   ├── node_modules
    │   └── package.json
    ├── federated-graphql-b
    │   ├── federated-graphql-b.js
    │   ├── node_modules
    │   └── package.json
    └── graphql-gateway
        ├── graphql-gateway.js
        ├── node_modules
        └── package.json

I’m creating a repo for setting up gateway & netlify lambda. It’s running on here.
That said, I have an issue when its deployed because Netlify deploys only one function(here with the folder structure above would be federated-graphql-a). The rest of two functions are not deployed.

here is package.json I would use:

"scripts": {
    "start": "run-p start:**",
    "start:app": "react-scripts start",
    "start:lambda-pokemon": "netlify-lambda serve src/functions/federated-graphql-a -p 9001",
    "start:lambda-simple": "netlify-lambda serve src/functions/federated-graphql-b -p 9002",
    "start:lambda-gateway": "netlify-lambda serve src/functions/graphql-gateway",
    "build": "run-p build:**",
    "build:app": "react-scripts build",
    "build:lambda-simple": "netlify-lambda build src/functions/federated-graphql-a",
    "build:lambda-pokemon": "netlify-lambda build src/functions/federated-graphql-b",
    "build:lambda-gateway": "netlify-lambda build src/functions/graphql-gateway"

On actually production of the repo, Scripts are all executed and log tells me that It did it. It’s not served somehow
Here is the deploy log:

10:49:23 AM: ┌────────────────────────────────────────────────────────────┐

10:49:23 AM: │ 2. onPostBuild command from @netlify/plugin-functions-core │

10:49:23 AM: └────────────────────────────────────────────────────────────┘

10:49:23 AM: ​

10:49:23 AM: Packaging Functions from functions_build directory:

10:49:23 AM: - federated-graphql-pokemon.js

10:49:32 AM: ​

10:49:32 AM: (@netlify/plugin-functions-core onPostBuild completed in 8.8s)

I’ve checked function-deploy-test as well and I think did the same thing to bundle the functions.

Hey @peter-wd-1,
TL;DR if you just want the working site and repo :slight_smile:

The longer story
There are a couple different issues happening at once and the best resource I can point you to is this extensive conversation about different ways you can build functions (zip and ship, manually zip, netlify-lambda):

I’d really recommend reviewing that if you want to get this working with netlify-lambda. I believe the core of the issue is that your function dependencies are not being automatically bundled with the netlify-lambda build command; you will need to install the function dependencies, one command at a time or with a script.

Hope you find this helpful! Let us know if you have any other questions.

1 Like

Tested just now, working very well!! Thank you so much for the repo. Didn’t’ expect such a practical answer :slight_smile:

1 Like