How to setup Lambda Server?

Hello, I follow the doc to configure lambda but fail.

docker-composer.yaml

services:

  dgraph:

    image: dgraph/standalone:latest

    environment:

      DGRAPH_ALPHA_GRAPHQL_LAMBDA_URL: "http://dgraph_lambda:8686/graphql-worker"

    ports:

      - "8080:8080"

      - "9080:9080"

      - "8000:8000"

    volumes:

      - dgraph:/dgraph

  dgraph_lambda:

    image: dgraph/dgraph-lambda:latest

    ports:

      - "8686:8686"

    environment:

      DGRAPH_URL: http://dgraph:8080

    volumes:

      - ./gql/script.js:/app/script/script.js

volumes:

  dgraph: {}

script.js

const results = await dql.query(`query queryAuthor($name: string) {

        queryAuthor(func: type(Author)) @filter(eq(Author.name, $name)) {

            name: Author.name

            dob: Author.dob

            reputation: Author.reputation

        }

    }`, {"$name": args.name})

    return results.data.queryAuthor

}

self.addGraphQLResolvers({

    "Query.authorsByName": authorsByName,

})

schema

type Author {
    id: ID!
    name: String! @search(by: [hash, trigram])
    dob: DateTime
    reputation: Float
}

type Query {
    authorsByName(name: String!): [Author] @lambda
}

add data

mutation{
	addAuthor(input:{name:"Ann Author", dob:"2000-01-01T00:00:00Z",reputation:6.6}){
		author{
			name
			reputation
			dob
		}
	}
}

when I query

query {
	authorsByName(name: "Ann Author") {
		name
		dob
		reputation
	}
}

It will return errors

{
	"errors": [
		{
			"message": "Evaluation of custom field failed because external request returned an error: unexpected error with: 400 for field: authorsByName within type: Query.",
			"locations": [
				{
					"line": 2,
					"column": 2
				}
			]
		}
	],
	"data": {
		"authorsByName": []
	},
	"extensions": {
		"tracing": {
			"version": 1,
			"startTime": "2022-01-18T14:50:43.9974995Z",
			"endTime": "2022-01-18T14:50:43.9991573Z",
			"duration": 1657800
		}
	}
}

I use

docker compose up -d

setup the container, then modify script.js to as above. and I inspect into the lambda container, make sure the modified script has updated to the container.
and I checked the schema within insomnia make sure the schema is the right one.
I also restart to lambda container.

I think maybe I have ignored some step. any help will be thanks.

This may not be it, but I don’t see where you defined authorsByName in the script.

Hello, sorry. I correct the script to the right one as in docs

async function authorsByName({args, dql}) {
    const results = await dql.query(`query queryAuthor($name: string) {
        queryAuthor(func: type(Author)) @filter(eq(Author.name, $name)) {
            name: Author.name
            dob: Author.dob
            reputation: Author.reputation
        }
    }`, {"$name": args.name})
    return results.data.queryAuthor
}

self.addGraphQLResolvers({
    "Query.authorsByName": authorsByName,
})

but the same error happen

{
	"errors": [
		{
			"message": "Evaluation of custom field failed because external request returned an error: unexpected error with: 400 for field: authorsByName within type: Query.",
			"locations": [
				{
					"line": 2,
					"column": 2
				}
			]
		}
	],
	"data": {
		"authorsByName": []
	},
	"extensions": {
		"tracing": {
			"version": 1,
			"startTime": "2022-01-19T00:28:15.442368Z",
			"endTime": "2022-01-19T00:28:15.4435905Z",
			"duration": 1222600
		}
	}
}

This is an example from docs

https://dgraph.io/docs/graphql/lambda/query/

Hello, I found another solution. The Zion (v21.12) has already included lambda server in itself.
You have to do nothing but only update your script via admin piont(localhost:8080/admin). There is not docment yet.

mutation{
	updateLambdaScript(input:{set:{script:"""self.addGraphQLResolvers({  "City.current_weather": getCurrentWeather,	}); async function getWeather(name, state, country) {  let url = `https://api.openweathermap.org/data/2.5/weather?q=${name},${state},${country}&appid=944f9bf37b47e039c6466b185de46269`;  let response = await fetch(url);  let data = await response.json();  return data.weather[0].description;}async function getCurrentWeather({ parent }) {  const { name, state, country } = parent;  var weather = getWeather(name, state, country);  return weather;}"""
	
	}}){
		lambdaScript{script}
	}
}

But I don’t find the way like schema which post the schema file as binary. So wait for the detail doc to come.

I still can’t fix error for my self host lamp server.

1 Like

I modified the docker-compose.yaml as following, it work well in dgraph/standalone: v21.03.2, and return the right response.

version: '3.7'
services:
  dgraph:
    image: dgraph/standalone:v21.03.2
    environment: 
      DGRAPH_ALPHA_GRAPHQL: lambda-url=http://lambda:8686/graphql-worker
    ports:
      - 8080:8080
      - 9080:9080
      - 8000:8000
    volumes:
      - ~/dgraph:/dgraph
    # command: dgraph alpha --security whitelist=0.0.0.0/0
    #   --graphql lambda-url=http://lambda:8686/graphql-worker
  lambda:    
    image: dgraph/dgraph-lambda:latest
    ports:
      - 8686:8686
    depends_on:
      - dgraph
    volumes:
      - ./gql/script.js:/app/script/script.js:ro
    environment:
      DGRAPH_URL: http://dgraph:8080
      # INTEGRATION_TEST: true

But I want to configure to use dgraph/dgraph:v21.12.0

version: '3.7'
services:
  zero:
    image: dgraph/dgraph:latest
    volumes:
      - ./tmp/data:/dgraph
    ports:
      - 5080:5080
      - 6080:6080
    restart: on-failure
    command: dgraph zero --my=zero:5080
  dgraph:
    image: dgraph/dgraph:latest
    # environment: 
    #   DGRAPH_ALPHA_GRAPHQL: lambda-url=http://lambda:8686/graphql-worker
    ports:
      - 8080:8080
      - 9080:9080
      - 8000:8000
    volumes:
      - ./tmp/data:/dgraph
    command: dgraph alpha --my=dgraph:7080 --cache size-mb=8096 --zero=zero:5080 --security whitelist=0.0.0.0/0 --lambda url=http://lambda:8686/graphql-worker
  lambda:    
    image: dgraph/dgraph-lambda:latest
    ports:
      - 8686:8686
    depends_on:
      - dgraph
    volumes:
      - ./gql/script.js:/app/script/script.js:ro
    environment:
      DGRAPH_URL: http://dgraph:8080
      # INTEGRATION_TEST: true

and lambda server will throw an Typeerror

TypeError: Cannot read property '0' of undefined

    at getWeather (evalmachine.<anonymous>:11:22)

    at processTicksAndRejections (internal/process/task_queues.js:95:5)

    at async Promise.all (index 0)

    at /app/src/evaluate-script.ts:115:22

    at /app/src/script-to-express.ts:22:22

The same script.js I use in both.

Maybe there is some different from v21.03.2 and v21.12.0.

Any my testing code is from this post

Bring RESTful Data Into Your GraphQL Responses Using Dgraph Lambda - Dgraph Blog