Skip to content

Commit bd2b20d

Browse files
Add dotenv-cli as a dev-dependency to help run the connector with .env files, and update README instructions (#36)
1 parent ebad66f commit bd2b20d

File tree

4 files changed

+67
-45
lines changed

4 files changed

+67
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Changes to be included in the next upcoming release
1010
- Added a default .gitignore that ignores node_modules in the connector template ([#34](https://github.com/hasura/ndc-nodejs-lambda/pull/34))
1111
- Updated to NDC TypeScript SDK to v5.0.0 ([#35](https://github.com/hasura/ndc-nodejs-lambda/pull/35))
1212
- The BigInt scalar type now uses the biginteger type representation
13+
- Added `dotenv-cli` to the dev dependencies of the connector's default package.json to help with using .env files ([#36](https://github.com/hasura/ndc-nodejs-lambda/pull/36))
1314

1415
## [1.4.0] - 2024-05-08
1516
- Removed type inference recursion limit ([#33](https://github.com/hasura/ndc-nodejs-lambda/pull/33)). This enables the use of very nested object graphs.

README.md

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,72 @@
22

33
The Node.js Lambda connector allows you to expose TypeScript functions as NDC functions/procedures for use in your Hasura DDN subgraphs.
44

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-
85
## 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:
1053

1154
```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
1656
```
1757

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:
1959

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`.
2171

2272
### Functions
2373
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> {
339389
```
340390

341391
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

connector-definition/template/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
},
1010
"dependencies": {
1111
"@hasura/ndc-lambda-sdk": "*"
12+
},
13+
"devDependencies": {
14+
"dotenv-cli": "^7.4.2"
1215
}
1316
}

yeoman-generator/src/app/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export default class extends Generator {
9191
},
9292
"dependencies": {
9393
"@hasura/ndc-lambda-sdk": packageManifest.version
94+
},
95+
"devDependencies": {
96+
"dotenv-cli": "^7.4.2"
9497
}
9598
})
9699
}

0 commit comments

Comments
 (0)