Skip to content

Commit 565d086

Browse files
authored
Lambdas use accessJwt - fixes #42 (#43)
graphql, dql.query and dql.mutate helper functions must pass the accessJWT token in the header "X-Dgraph-AccessToken" to get the requests on /graphql /query and /mutate working in ACL enabled and multi-tenant scenarii.
1 parent e049d5c commit 565d086

16 files changed

+5038
-5155
lines changed

.vscode/launch.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "attach",
7+
"name": "Attach to Process",
8+
"port": 9230
9+
}
10+
]
11+
}

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:14-alpine as build
1+
FROM node:20-alpine as build
22

33
RUN apk add python3 make g++
44
WORKDIR /app
@@ -8,12 +8,12 @@ RUN npm install
88
COPY . .
99
ARG nodeEnv=production
1010
ENV NODE_ENV $nodeEnv
11-
RUN npm run build && if [[ "$nodeEnv" == "production" ]]; then mv node_modules/node-webcrypto-ossl tmp && rm -rf node_modules && mkdir node_modules && mv tmp node_modules/node-webcrypto-ossl && npm install --no-optional; fi
11+
RUN npm run build
1212

1313
# Used just for tests
1414
ENTRYPOINT [ "npm", "run" ]
1515

16-
FROM node:14-alpine
16+
FROM node:20-alpine
1717
ENV NODE_ENV production
1818
RUN adduser app -h /app -D
1919
USER app

Readme.md

+39-23
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,53 @@ self.addMultiParentGraphQLResolvers({
3636

3737
## Running Locally
3838

39-
First launch dgraph and load it with the todo schema (and add a todo or two).
40-
41-
```graphql
42-
type User {
43-
id: ID!
44-
firstName: String!
45-
lastName: String!
46-
fullName: String @lambda
47-
}
39+
Create a "local-lambda" docker image
40+
```
41+
docker build -t local-lambda .
42+
```
43+
### option 1 - run the lambda server alone with
44+
```
45+
docker run -d -p 8686:8686 local-lambda
46+
```
47+
You can perform a basic test using curl:
4848

49-
type Todo {
50-
id: ID!
51-
title: String
52-
}
49+
```bash
50+
curl localhost:8686/graphql-worker -H "Content-Type: application/json" -d '{"resolver":"User.fullName","parents":[{"firstName":"Dgraph","lastName":"Labs"}]}'
51+
```
5352

54-
type Query {
55-
todoTitles: [String] @lambda
56-
}
53+
54+
### option 2 - use one of the scripts in dgraph/contrib/local-test
55+
56+
- change the lambda image used in the script to use local-lambda for your tests
57+
- check the script you want to load. To run the lambda jest, load the script provided in this repo.
58+
59+
For testing, update exosystem.config.js
60+
61+
- add node_args: ["--inspect"], to enable debugging.
62+
- set instances to 1, to be sure that the port 9230 will be on the only instance your are testing.
63+
64+
in VS code, attach to the docker container, and then launch the debug confugration "Attach to Process"
65+
66+
67+
### Tests
68+
run
5769
```
70+
docker-compose up
71+
```
72+
to get an cluster with Dgraph zero, alpha and lambda server
73+
74+
run
5875

59-
```bash
60-
# host.docker.internal may not work on old versions of docker
61-
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
6276
```
77+
export DGRAPH_URL=http://localhost:8080
6378
64-
Note for linux: host.docker.internal doesn't work on older versions of docker on linux. You can use `DGRAPH_URL=http://172.17.0.1:8080` instead
79+
export INTEGRATION_TEST=true
6580
66-
Then test it out with the following curls
67-
```bash
68-
curl localhost:8686/graphql-worker -H "Content-Type: application/json" -d '{"resolver":"User.fullName","parents":[{"firstName":"Dgraph","lastName":"Labs"}]}'
81+
npm test
6982
```
83+
to execute the tests.
84+
85+
7086

7187
## Environment
7288

acl/hmac_secret_file

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f77dca9a74915c339bff019ffefefb36

docker-compose.yml

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
version: '3.4'
22
services:
3-
dgraph-lambda:
3+
lambda:
44
build:
55
context: .
6-
target: build
76
args:
87
nodeEnv: development
8+
volumes:
9+
- ./script/script.js:/app/script/script.js:ro
10+
environment:
11+
- DGRAPH_URL=http://host.docker.internal:8080
12+
- MAX_MEMORY_LIMIT=256M
913
ports:
10-
- "8686:8686"
14+
- 8686:8686
1115
depends_on:
12-
- dgraph
16+
- alpha
17+
# Dgraph Zero controls the cluster
18+
zero:
19+
image: dgraph/dgraph:latest
20+
container_name: lambda_dgraph_zero
21+
ports:
22+
- 5080:5080
23+
- 6080:6080
24+
command: dgraph zero --my=zero:5080 --logtostderr -v=2 --telemetry sentry=false
25+
restart: unless-stopped
26+
# Dgraph Alpha hosts the graph and indexes
27+
alpha:
28+
image: dgraph/dgraph:latest
29+
container_name: lambda_dgraph_alpha
30+
volumes:
31+
- ./acl:/config/acl
32+
ports:
33+
- 8080:8080
34+
- 9080:9080
35+
command: >
36+
dgraph alpha --my=alpha:7080 --zero=zero:5080
37+
--security whitelist=0.0.0.0/0
38+
--logtostderr -v=2
39+
--graphql lambda-url=http://host.docker.internal:8686/graphql-worker
40+
--telemetry sentry=false
1341
environment:
14-
DGRAPH_URL: http://dgraph:8080
15-
INTEGRATION_TEST: "true"
16-
dgraph:
17-
image: "dgraph/standalone:v21.03.2"
42+
DGRAPH_ALPHA_ACL: secret-file=/config/acl/hmac_secret_file
43+
restart: unless-stopped
44+
45+

ecosystem.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ module.exports = {
2020
},
2121
],
2222
};
23+
// add the option node_args: ["--inspect"], to enable debugging.

0 commit comments

Comments
 (0)