Skip to content

Commit 7144c93

Browse files
committed
Add an example for OpenWhisk
1 parent de38931 commit 7144c93

File tree

9 files changed

+264
-0
lines changed

9 files changed

+264
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mesh
2+
node_modules
3+
dist
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sources:
2+
- name: SWAPI
3+
handler:
4+
graphql:
5+
endpoint: https://swapi-graphql.netlify.app/.netlify/functions/index
6+
7+
serve:
8+
endpoint: /api/v1/web/guest/mesh/swapi/graphql
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# An Example Project for OpenWhisk
2+
3+
## Files
4+
5+
- `src/index.ts` is the handler function for the OpenWhisk action that uses GraphQL Mesh's platform agnostic HTTP Handler
6+
- `.meshrc.yml` is the configuration file for GraphQL Mesh
7+
- `package.json` is the package.json file that contains all the dependencies and scripts
8+
- `build.js` is the code file that runs `ESBuild` to create a bundle for OpenWhisk deployment
9+
10+
## Configuring the project
11+
12+
GraphQL Mesh needs to be aware of the path of the OpenWhisk action endpoint.
13+
So you need to configure `serve.endpoint` in `.meshrc.yml`;
14+
15+
```yaml
16+
serve:
17+
# This is the full path to your endpoint
18+
# In the following endpoint, we assume you have created a package with `wsk package create mesh`
19+
endpoint: /api/v1/web/guest/mesh/swapi/graphql
20+
```
21+
22+
You also need to update the paths inside `index.ts` and `package.json` to match your OpenWhisk action name.
23+
24+
## Building the project for deployment
25+
26+
You can see an example script to bundle the project with `ESBuild` in `build.js`.
27+
`yarn build` will build the artifacts of GraphQL Mesh first then bundle all the code needed for the OpenWhisk action by taking `index.ts` as an endpoint.
28+
29+
You can find the bundle in `dist/index.js` and deploy it either `yarn deploy` or manually with `wsk` like `wsk action update /guest/mesh/swapi --kind nodejs:16 dist/index.js`.
30+
31+
## Running the project locally
32+
33+
`start` command will start Mesh server to mimic your API endpoint locally without deployment;
34+
35+
```bash
36+
yarn start
37+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { build } = require('esbuild');
2+
3+
build({
4+
entryPoints: ['./src/index.ts'],
5+
outfile: 'dist/index.js',
6+
format: 'cjs',
7+
minify: false,
8+
bundle: true,
9+
platform: 'node',
10+
target: 'node16',
11+
}).catch(e => {
12+
console.error(e);
13+
process.exit(1);
14+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "openwhisk-example",
3+
"version": "0.0.0",
4+
"description": "OpenWhisk Example for GraphQL Mesh",
5+
"author": "Arda TANRIKULU <[email protected]>",
6+
"license": "MIT",
7+
"main": "dist/index.js",
8+
"scripts": {
9+
"start": "mesh dev",
10+
"build": "mesh build && node build.js",
11+
"deploy": "wsk action update /guest/mesh/swapi dist/index.js --docker openwhisk/action-nodejs-v16 --web raw"
12+
},
13+
"dependencies": {
14+
"esbuild": "0.15.10",
15+
"graphql": "16.6.0",
16+
"@graphql-mesh/cli": "0.78.29",
17+
"@graphql-mesh/graphql": "0.31.21",
18+
"typescript": "4.8.4"
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createBuiltMeshHTTPHandler } from '../.mesh';
2+
3+
const httpHandler = createBuiltMeshHTTPHandler();
4+
5+
export const main = async function (params) {
6+
const response = await httpHandler.fetch(`http://localhost/api/v1/web/guest/mesh/swapi${params.__ow_path}`, {
7+
method: params.__ow_method,
8+
headers: params.__ow_headers,
9+
body: params.__ow_body ? Buffer.from(params.__ow_body, 'base64') : undefined,
10+
});
11+
12+
const headers = {};
13+
14+
response.headers.forEach((value, key) => {
15+
headers[key] = value;
16+
});
17+
18+
const body = await (headers['content-type'].includes('json') ? response.json() : response.text());
19+
20+
return {
21+
statusCode: response.status,
22+
body,
23+
headers,
24+
};
25+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"target": "esnext",
5+
"module": "commonjs",
6+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
7+
"lib": ["esnext"],
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"skipLibCheck": true
11+
},
12+
"files": ["src/index.ts"],
13+
"exclude": ["node_modules"]
14+
}

website/src/pages/docs/getting-started/deploy-mesh-gateway.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ You can see the following examples for more details:
104104

105105
Also you can see how to setup *KV* as a cache storage in GraphQL Mesh [here](/docs/cache/cfwKv).
106106

107+
### Deploy Mesh on Apache OpenWhisk
108+
109+
You can see our example that shows how to setup an OpenWhisk action for GraphQL Mesh.
110+
[OpenWhisk Example](https://github.com/Urigo/graphql-mesh/tree/master/examples/openwhisk-example)
111+
107112
## Deploy on Node.js
108113

109114
### Mesh as an Express route

yarn.lock

Lines changed: 138 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)