Lambda
Introduction
For Shared and Free backends, a Dgraph lambda script is uniquely identified by a deployment id associated with the backend. You can identify your backend and the associated deployment id by using the List Backends API. In case of a Dedicated, and multi-tenant backend, an additional key, the tenant id is also required to uniquely identify a lambda script. This tenant id key is ignored when used in the context of Shared and Free backends.
As a first step, you will need to identify your backend.
List Deployment and Get Lambda Script
Please use the List Backends API to identify your backend, as well as get the lambda script deployed. In order to list the backends, you will need to pass a Bearer token as an Authorization
header. This token is generated by logging in via the Login query first.
Cloud Endpoint
https://cerebro.cloud.dgraph.io/graphql
API Command
query{
deployments {
uid
name
zone
subdomain
url
tenantID
lambdaScript
}
}
Arguments
None
Example
#!/usr/bin/env bash
CEREBRO_JWT="<cerebro-jwt-from-login-query>"
curl "https://cerebro.cloud.dgraph.io/graphql" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${CEREBRO_JWT}" \
--data-binary '{"query":"query{\n deployments {\n uid\n name\n zone\n subdomain\n url\n tenantID\n lambdaScript\n }\n}","variables":{}}' \
--compressed
{
"data": {
"deployments": [
{
"uid": "0x6238",
"name": "OrderJourney",
"zone": "us-west-2",
"subdomain": "vacuous-wash",
"url": "vacuous-wash.us-west-2.aws.cloud.dgraph.io",
"tenantID": 107,
"lambdaScript": "Ly8gWW91IGNhbiB0eXBlL3Bhc3RlIHlvdXIgc2NyaXB0IGhlcmUKY29uc3QgTmFtZVJlc29sdmVyID0gKHtwYXJlbnQ6IHtuYW1lfX0pID0+IGBNeSBuYW1lIGlzICR7bmFtZX0uYAoKc2VsZi5hZGRHcmFwaFFMUmVzb2x2ZXJzKHsKICAgICJQZXJzb24ubmFtZSI6IE5hbWVSZXNvbHZlcgp9KQ=="
}
]
}
}
The uid
field in the response of the query is to be used as the deployment id. In the above example, the deployment id for the backend with name “OrderJourney” is “0x6238”. The field lambdaScript
contains the lambda script in the form of a Base64 encoded string.
Decode the Base64 encoded lambdaScript
In order to decode the Base64 encoded string into the actual lambda code, please use the command as shown below.
$ echo "Ly8gWW91IGNhbiB0eXBlL3Bhc3RlIHlvdXIgc2NyaXB0IGhlcmUKY29uc3QgTmFtZVJlc29sdmVyID0gKHtwYXJlbnQ6IHtuYW1lfX0pID0+IGBNeSBuYW1lIGlzICR7bmFtZX0uYAoKc2VsZi5hZGRHcmFwaFFMUmVzb2x2ZXJzKHsKICAgICJQZXJzb24ubmFtZSI6IE5hbWVSZXNvbHZlcgp9KQ==" | base64 -d
Output
// You can type/paste your script here
const NameResolver = ({parent: {name}}) => `My name is ${name}.`
self.addGraphQLResolvers({
"Person.name": NameResolver
})
Lambda Logs
You can fetch the logs for your lambda by using the getLambdaLogs
query.
Cloud Endpoint
https://cerebro.cloud.dgraph.io/graphql
API Command
query GetLambdaLogs($lambdaLogsInput: LambdaLogsInput!) {
getLambdaLogs(input: $lambdaLogsInput)
}
Arguments
lambdaLogsInput
: a LambdaLogsInput objectlambdaLogsInput.deploymentID
: the deployment UID returned from List BackendslambdaLogsInput.tenantID
: In case of a multi-tenant, and dedicated backend, you will need to pass the tenant Id as welllambdaLogsInput.start
: start timelambdaLogsInput.end
: end time
Example
#!/usr/bin/env bash
CEREBRO_JWT="<cerebro-jwt-from-login-query>"
curl "https://cerebro.cloud.dgraph.io/graphql" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${CEREBRO_JWT}" \
--data-binary '{"query":"query GetLambdaLogs($input: LambdaLogsInput!) {\n getLambdaLogs(input: $input)\n}","variables":{"input":{"deploymentID":"0xf0ffe9"}}}' \
--compressed
{
"data": {
"getLambdaLogs": [
"2021-04-16 19:03:54.009209524 +0000 UTC Server Listening on port 8686!",
"2021-04-16 19:03:54.202216548 +0000 UTC Server Listening on port 8686!",
"2021-04-16 19:03:54.51171317 +0000 UTC Server Listening on port 8686!",
"2021-04-16 19:03:54.707496343 +0000 UTC Server Listening on port 8686!"
]
}
}
Update or Delete Lambda
You can update or delete your lambda by using the updateLambda
mutation. You will need to pass a deployment id to uniquely identify your lambda. In case your backend is multi-tenant, you will need to pass the tenant Id as well.
In order to update your lambda, you would need to convert your lamdba script into a Base64 encoded format and send it as the lambdaScript
argument.
In order to delete your lambda, you can simply send in an empty string in the lambdaScript
argument.
Cloud Endpoint
https://cerebro.cloud.dgraph.io/graphql
API Command
mutation updateLambda($input: UpdateLambdaInput!){
updateLambda(input: $input)
}
Arguments
updateLambdaInput
: an UpdateLambdaInput objectupdateLambdaInput.deploymentID
: the deployment UID returned from List BackendsupdateLambdaInput.tenantID
: the tenant ID in case your backend is a dedicated, and multi-tenant backend. In case you have any other type of backend, you can leave it as the default0
valueupdateLambdaInput.lambdaScript
: the base64-encoded Javascript string containing your Lambda Resolver
Example
- Create your Lambda Resolver script
//your lambda resolver
- Base64 encode your script
$ echo "//your lambda resolver" | base64
Ly95b3VyIGxhbWJkYSByZXNvbHZlcgo=
- Send in a cURL request
#!/usr/bin/env bash
CEREBRO_JWT="<cerebro-jwt-from-login-query>"
curl "https://cerebro.cloud.dgraph.io/graphql" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${CEREBRO_JWT}" \
--data-binary '{"query":"mutation updateLambda($input: UpdateLambdaInput!){\n updateLambda(input: $input)\n}","variables":{"input":{"deploymentID":"0x6238","tenantID":0,"lambdaScript":"Ly95b3VyIGxhbWJkYSByZXNvbHZlcgo="}}}' \
--compressed
{
"data": {
"updateLambda": "Successfully added the lambda function"
}
}