Skip to content

Commit 2d710ce

Browse files
authored
Added lambda examples (#232)
* added lambda examples * added cloudflare workers example
1 parent 66c8d2c commit 2d710ce

File tree

27 files changed

+1144
-293
lines changed

27 files changed

+1144
-293
lines changed

.changeset/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"baseBranch": "main",
88
"updateInternalDependencies": "patch",
99
"ignore": [
10+
"@envelop-examples/*",
1011
"@envelop/testing",
1112
"@envelop-examples/graphql-ws",
1213
"@envelop-examples/apollo-server",

examples/apollo-server/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"license": "MIT",
88
"dependencies": {
99
"@envelop/core": "*",
10-
"apollo-server": "2.21.2",
11-
"@graphql-tools/schema": "7.1.3"
10+
"apollo-server": "2.25.0",
11+
"@graphql-tools/schema": "7.1.5"
1212
},
1313
"devDependencies": {
14-
"@types/node": "14.14.35",
15-
"ts-node": "9.1.1",
16-
"typescript": "4.2.3"
14+
"@types/node": "15.6.1",
15+
"ts-node": "10.0.0",
16+
"typescript": "4.3.2"
1717
},
1818
"scripts": {
1919
"start": "ts-node index.ts"

examples/azure-functions/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Envelop basic example with Azure Functions
2+
3+
This example demonstrate how to implement the basic GraphQL flow with Envelop, GraphQL-Helix and Azure Functions.

examples/azure-functions/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { envelop, useLogger, useSchema, useTiming } from '@envelop/core';
2+
import { makeExecutableSchema } from '@graphql-tools/schema';
3+
import { AzureFunction, Context, HttpRequest } from '@azure/functions';
4+
import { getGraphQLParameters, processRequest, Response } from 'graphql-helix';
5+
6+
const schema = makeExecutableSchema({
7+
typeDefs: /* GraphQL */ `
8+
type Query {
9+
hello: String!
10+
}
11+
`,
12+
resolvers: {
13+
Query: {
14+
hello: () => 'World',
15+
},
16+
},
17+
});
18+
19+
const getEnveloped = envelop({
20+
plugins: [useSchema(schema), useLogger(), useTiming()],
21+
});
22+
23+
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
24+
const { parse, validate, contextFactory, execute, schema } = getEnveloped();
25+
const request = {
26+
body: req.body,
27+
headers: req.headers,
28+
method: req.method,
29+
query: req.query,
30+
};
31+
32+
const { operationName, query, variables } = getGraphQLParameters(request);
33+
const result = (await processRequest({
34+
operationName,
35+
query,
36+
variables,
37+
request,
38+
schema,
39+
parse,
40+
validate,
41+
execute,
42+
contextFactory,
43+
})) as Response<any, any>;
44+
45+
context.res = {
46+
status: 200,
47+
headers: result.headers.reduce((prev, item) => ({ ...prev, [item.name]: item.value }), {}),
48+
body: JSON.stringify(result.payload),
49+
};
50+
};
51+
52+
export default httpTrigger;

examples/azure-functions/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@envelop-examples/azure-functions",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "index.js",
6+
"author": "Dotan Simha",
7+
"license": "MIT",
8+
"dependencies": {
9+
"graphql-helix": "1.6.1",
10+
"@envelop/core": "*",
11+
"@graphql-tools/schema": "7.1.5"
12+
},
13+
"devDependencies": {
14+
"@azure/functions": "1.2.3",
15+
"@types/node": "15.6.1",
16+
"ts-node": "10.0.0",
17+
"typescript": "4.3.2"
18+
},
19+
"scripts": {
20+
"compile": "tsc"
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"esModuleInterop": true,
5+
"target": "es2020",
6+
"lib": ["es2020"],
7+
"module": "commonjs",
8+
"moduleResolution": "node",
9+
"sourceMap": true,
10+
"experimentalDecorators": true,
11+
"emitDecoratorMetadata": true,
12+
"outDir": "dist",
13+
"allowUnreachableCode": false,
14+
"allowUnusedLabels": false,
15+
"alwaysStrict": true,
16+
"noImplicitAny": false,
17+
"noImplicitReturns": true,
18+
"noImplicitThis": true,
19+
"noUnusedLocals": false,
20+
"noUnusedParameters": false,
21+
"importHelpers": true,
22+
"skipLibCheck": true
23+
},
24+
"include": ["."],
25+
"exclude": ["node_modules"]
26+
}

examples/cloudflare-workers/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Envelop basic example with Cloudflare Workers
2+
3+
This example demonstrate how to implement the basic GraphQL flow with Envelop, GraphQL-Helix and Cloudflare Workers.

examples/cloudflare-workers/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { envelop, useLogger, useSchema, useTiming } from '@envelop/core';
2+
import { makeExecutableSchema } from '@graphql-tools/schema';
3+
import { getGraphQLParameters, processRequest, Response } from 'graphql-helix';
4+
import { Router } from 'worktop';
5+
import { listen } from 'worktop/cache';
6+
7+
const router = new Router();
8+
9+
const schema = makeExecutableSchema({
10+
typeDefs: /* GraphQL */ `
11+
type Query {
12+
hello: String!
13+
}
14+
`,
15+
resolvers: {
16+
Query: {
17+
hello: () => 'World',
18+
},
19+
},
20+
});
21+
22+
const getEnveloped = envelop({
23+
plugins: [useSchema(schema), useLogger(), useTiming()],
24+
});
25+
26+
router.add('POST', '/graphql', async (req, res) => {
27+
const { parse, validate, contextFactory, execute, schema } = getEnveloped();
28+
const request = {
29+
body: req.body,
30+
headers: req.headers,
31+
method: req.method,
32+
query: req.query,
33+
};
34+
35+
const { operationName, query, variables } = getGraphQLParameters(request);
36+
const result = (await processRequest({
37+
operationName,
38+
query,
39+
variables,
40+
request,
41+
schema,
42+
parse,
43+
validate,
44+
execute,
45+
contextFactory,
46+
})) as Response<any, any>;
47+
48+
res.send(
49+
result.status,
50+
result.payload,
51+
result.headers.reduce((prev, item) => ({ ...prev, [item.name]: item.value }), {})
52+
);
53+
});
54+
listen(router.run);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@envelop-examples/cloudflare-workers",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "index.js",
6+
"author": "Dotan Simha",
7+
"license": "MIT",
8+
"dependencies": {
9+
"graphql-helix": "1.6.1",
10+
"@envelop/core": "*",
11+
"@graphql-tools/schema": "7.1.5",
12+
"worktop": "0.7.0"
13+
},
14+
"devDependencies": {
15+
"@cloudflare/workers-types": "2.2.2",
16+
"@types/node": "15.6.1",
17+
"ts-node": "10.0.0",
18+
"typescript": "4.3.2"
19+
},
20+
"scripts": {
21+
"compile": "tsc"
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": true,
4+
"esModuleInterop": true,
5+
"target": "es2020",
6+
"lib": ["es2020"],
7+
"module": "commonjs",
8+
"moduleResolution": "node",
9+
"sourceMap": true,
10+
"experimentalDecorators": true,
11+
"emitDecoratorMetadata": true,
12+
"outDir": "dist",
13+
"allowUnreachableCode": false,
14+
"allowUnusedLabels": false,
15+
"alwaysStrict": true,
16+
"noImplicitAny": false,
17+
"noImplicitReturns": true,
18+
"noImplicitThis": true,
19+
"noUnusedLocals": false,
20+
"noUnusedParameters": false,
21+
"importHelpers": true,
22+
"skipLibCheck": true
23+
},
24+
"include": ["."],
25+
"exclude": ["node_modules"]
26+
}

0 commit comments

Comments
 (0)