You are looking at the docs for an older version of Dgraph (v21.03). The latest version is v24.0.
Ask a Question

Lambda Server

In this article you’ll learn how to setup a Dgraph database with a lambda server.

Dgraph Lambda

Dgraph Lambda is a serverless platform for running JavaScript on Dgraph and Dgraph Cloud.

You can download the latest version or review the implementation in our open-source repository.

Running with Docker

To run a Dgraph Lambda server with Docker:

docker run -it --rm -p 8686:8686 -v /path/to/script.js:/app/script/script.js -e DGRAPH_URL=http://host.docker.internal:8080 dgraph/dgraph-lambda
Note host.docker.internal doesn’t work on older versions of Docker on Linux. You can use DGRAPH_URL= instead.

Adding libraries

If you would like to add libraries to Dgraph Lambda, use webpack --target=webworker to compile your script.

Working with TypeScript

You can import @slash-graphql/lambda-types to get types for addGraphQLResolver and addGraphQLMultiParentResolver.

Dgraph Alpha

To set up Dgraph Alpha, you need to define the --graphql superflag’s lambda-url option, which is used to set the URL of the lambda server. All the @lambda fields will be resolved through the lambda functions implemented on the given lambda server.

For example:

dgraph alpha --graphql lambda-url=http://localhost:8686/graphql-worker

Then test it out with the following curl command:

curl localhost:8686/graphql-worker -H "Content-Type: application/json" -d '{"resolver":"MyType.customField","parent":[{"customField":"Dgraph Labs"}]}'

Docker settings

If you’re using Docker, you need to add the --graphql superflag’s lambda-url option to your Alpha configuration. For example:

    command: /gobin/dgraph alpha --zero=zero1:5180 -o 100 --expose_trace --trace ratio=1.0
      --profile_mode block --block_rate 10 --logtostderr -v=2
      --security whitelist=,, --my=alpha1:7180
      --graphql lambda-url=http://lambda:8686/graphql-worker

Next, you need to add the Dgraph Lambda server configuration, and map the JavaScript file that contains the code for lambda functions to the /app/script/script.js file. Remember to set the DGRAPH_URL environment variable to your Alpha server.

Here’s a complete Docker example that uses the base Dgraph image and adds Lambda server support:

    image: dgraph/standalone:latest
      DGRAPH_ALPHA_GRAPHQL_LAMBDA_URL: "http://dgraph_lambda:8686/graphql-worker"
      - "8080:8080"
      - "9080:9080"
      - "8000:8000"
      - dgraph:/dgraph

    image: dgraph/dgraph-lambda:latest

      - "8686:8686"
      DGRAPH_URL: http://dgraph:8080
      - ./gql/script.js:/app/script/script.js:ro

  dgraph: {}