|
2 | 2 |
|
3 | 3 | The Node.js Lambda connector allows you to expose TypeScript functions as NDC functions/procedures for use in your Hasura DDN subgraphs.
|
4 | 4 |
|
5 |
| -> [!IMPORTANT] |
6 |
| -> Hasura DDN Alpha users should use 0.x versions of the `ndc-lambda-sdk`. v1.x versions of the `ndc-lambda-sdk` support the Hasura DDN Beta. |
7 |
| -
|
8 | 5 | ## How to Use
|
9 |
| -First, ensure you have Node.js v20+ installed. Then, create a directory into which you will create your functions using the `hasura-ndc-nodejs-lambda` Yeoman template. |
| 6 | +> [!TIP] |
| 7 | +> The following instructions are just a quick summary of how to use the Node.js Lambda connector. |
| 8 | +> To see it in use in a wider Hasura DDN project, and to understand the underlying DDN concepts, please check out the [Hasura DDN Getting Started Guide](https://hasura.io/docs/3.0/getting-started/overview/). |
| 9 | +> The Node.js Lambda connector is used in the business logic part of that tutorial. |
| 10 | +
|
| 11 | +First, ensure you have Node.js v20+ installed and an existing Hasura DDN project created (see the [Hasura DDN Getting Started Guide](https://hasura.io/docs/3.0/getting-started/overview/)). Then, initialize the connector into that project: |
| 12 | + |
| 13 | +```bash |
| 14 | +ddn connector init my_ts --subgraph my_subgraph --hub-connector hasura/nodejs |
| 15 | +``` |
| 16 | + |
| 17 | +This will generate the necessary files into the `my_subgraph/connector/my_ts` directory. This creates a `functions.ts` file in which you will write your functions, and a `package.json` with the `ndc-lambda-sdk` installed into it. |
| 18 | + |
| 19 | +Restore all the npm packages required to run the connector by running inside the connector's directory: |
| 20 | + |
| 21 | +```bash |
| 22 | +npm install |
| 23 | +``` |
| 24 | + |
| 25 | +You may wish to change the port the connector runs on to one that is unused (the default is 8080). You can do so by changing the `.env.local` file and adding: |
| 26 | + |
| 27 | +``` |
| 28 | +HASURA_CONNECTOR_PORT=<port> |
| 29 | +``` |
| 30 | + |
| 31 | +To run the connector with these environment environment variables applied, you can run the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +npx dotenv -e .env.local -- npm run watch |
| 35 | +``` |
| 36 | + |
| 37 | +This starts the connector in watch mode, which watches for code changes and restarts the connector when they are detected. `npm run start` can be used instead to just start the connector without watching for changes. Both `start` and `watch` are defined in the `package.json`'s scripts section and use the `ndc-lambda-sdk` to host your `functions.ts` file. |
| 38 | + |
| 39 | +To add a `DataConnectorLink` to link the connector into the wider Hasura DDN project, run: |
| 40 | + |
| 41 | +```bash |
| 42 | +ddn connector-link add my_ts |
| 43 | +``` |
| 44 | + |
| 45 | +Then, update the values in your subgraph's `.env.my_subgraph` file to include this connector. |
| 46 | + |
| 47 | +``` |
| 48 | +MY_SUBGRAPH_MY_TS_READ_URL=http://local.hasura.dev:<port> |
| 49 | +MY_SUBGRAPH_MY_TS_WRITE_URL=http://local.hasura.dev:<port> |
| 50 | +``` |
| 51 | + |
| 52 | +Once you have written your functions, and while the connector is running, you can update that `DataConnectorLink` and add all the new functions to your subgraph by running: |
10 | 53 |
|
11 | 54 | ```bash
|
12 |
| -mkdir my-functions |
13 |
| -cd my-functions |
14 |
| -npm install -g generator-hasura-ndc-nodejs-lambda |
15 |
| -npx yo hasura-ndc-nodejs-lambda |
| 55 | +ddn connector-link update my_ts --subgraph my_subgraph --add-all-resources |
16 | 56 | ```
|
17 | 57 |
|
18 |
| -This creates a `functions.ts` file in which you will write your functions, and a `package.json` with the `ndc-lambda-sdk` installed into it. |
| 58 | +To make a local build of your supergraph you can run: |
19 | 59 |
|
20 |
| -The `package.config` has been created with `start` and `watch` scripts. These use the SDK to host your `functions.ts` file. You can start your connector with `npm start`, or if you'd like it to automatically restart as you change your code, use `npm run watch`. |
| 60 | +```bash |
| 61 | +ddn supergraph build local --output-dir ./engine |
| 62 | +``` |
| 63 | + |
| 64 | +You can then run that build locally for testing by starting the engine and other connectors in the DDN project using Docker Compose: |
| 65 | + |
| 66 | +```bash |
| 67 | +HASURA_DDN_PAT=$(ddn auth print-pat) docker compose -f docker-compose.hasura.yaml watch |
| 68 | +``` |
| 69 | + |
| 70 | +You can view and query that local instance using the Hasura Graphiql Explorer by navigating to `https://console.hasura.io/local/graphql?url=http://localhost:3000`. |
21 | 71 |
|
22 | 72 | ### Functions
|
23 | 73 | Any functions exported from `functions.ts` are made available as NDC functions/procedures to use in your Hasura metadata and expose as GraphQL fields in queries or mutation.
|
@@ -339,38 +389,3 @@ export async function doSomething(): Promise<string> {
|
339 | 389 | ```
|
340 | 390 |
|
341 | 391 | The span will be wrapped around the function you pass to `sdk.withActiveSpan`. The function can optionally be an async function that returns a Promise, and if so, the span will be ended when the Promise resolves.
|
342 |
| - |
343 |
| -## Deploying with `hasura3 connector create` |
344 |
| - |
345 |
| -You will need: |
346 |
| - |
347 |
| -* [Hasura v3 CLI](https://hasura.io/docs/3.0/cli/installation/) (with a logged-in session) |
348 |
| -* [Hasura v3 CLI Connector Plugin](https://hasura.io/docs/latest/hasura-cli/connector-plugin/) |
349 |
| -* (Optionally) A value to use with `SERVICE_TOKEN_SECRET` |
350 |
| -* a TypeScript sources directory. E.g. `--volume ./my_functions_directory:/functions` |
351 |
| - |
352 |
| -First, ensure you have deleted your `node_modules` directory from inside your sources directory, since that is unnecessary to deploy. |
353 |
| - |
354 |
| -Then, create the connector: |
355 |
| - |
356 |
| -```bash |
357 |
| -hasura3 connector create my-cool-connector:v1 \ |
358 |
| - --github-repo-url https://github.com/hasura/ndc-nodejs-lambda/tree/main \ |
359 |
| - --config-file <(echo '{}') \ |
360 |
| - --volume ./my_functions_directory:/functions \ |
361 |
| - --env SERVICE_TOKEN_SECRET=MY-SERVICE-TOKEN # (optional) |
362 |
| -``` |
363 |
| - |
364 |
| -*Note: Even though you can use the "main" branch to deploy the latest connector features, see the [Hasura Connector Hub](https://hasura.io/connectors/nodejs-lambda) for verified release tags* |
365 |
| - |
366 |
| -Monitor the deployment status by name - this will indicate in-progress, complete, or failed status: |
367 |
| - |
368 |
| -> hasura3 connector status my-cool-connector:v1 |
369 |
| -
|
370 |
| -List all your connectors with their deployed URLs: |
371 |
| - |
372 |
| -> hasura3 connector list |
373 |
| -
|
374 |
| -View logs from your running connector: |
375 |
| - |
376 |
| -> hasura3 connector logs my-cool-connector:v1 |
|
0 commit comments