diff --git a/.gitignore b/.gitignore index 4a42061..48b8bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -.vscode/ vendor/ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..7f96888 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "HasuraHQ.hasura", + "denoland.vscode-deno" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..99f2846 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "deno.enable": true, + "deno.lint": true, + "deno.unstable": false +} diff --git a/Dockerfile b/Dockerfile index 074d9a4..65632e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,9 @@ - # See https://github.com/hasura/ndc-typescript-deno/tags for the latest tag to use via FROM # It is recommended that you pin a release tag rather than using `main` FROM ghcr.io/hasura/ndc-typescript-deno:main -COPY ./functions:/functions -RUN EARLY_ENTRYPOINT_EXIT=true ./entrypoint.sh /app/ndc-typescript-deno --configuration /placeholder-config.json +COPY ./functions /functions/src + +# Pre-cache inference results and dependencies +RUN PRECACHE_ONLY=true /app/entrypoint.sh diff --git a/README.md b/README.md index 8d4c5f1..35da584 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,32 @@ This repository provides a example Typescript function that can be used with the To deploy from the Hasura CLI do -``` +```bash hasura3 connector create sendgrid:deno:v1 \ --github-repo-url https://github.com/hasura/ndc-typescript-deno/tree/main \ - --config-file config.json \ + --config-file <(echo '{}') \ --volume ./functions:/functions \ --env SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY' \ --env SERVICE_TOKEN_SECRET='MY-PRESHARED-HASURA-TOKEN' ``` +Note that you will need the `connector` plugin installed into Hasura CLI to do the above. + +```bash +hasura3 plugin install connector +``` + ## Deploying to Hasura Cloud -Update your `metadata.hml` using the [Hasura VSCode LSP](https://marketplace.visualstudio.com/items?itemName=HasuraHQ.hasura) +First, create a Hasura DDN project if you don't have one already: + +```bash +hasura3 project create +``` + +Take the project name returned by that command (or use your existing project name) and set it in the `hasura.yaml` file. + +Then, update your `subgraphs/default/sendgrid.hml` using the [Hasura VSCode LSP](https://marketplace.visualstudio.com/items?itemName=HasuraHQ.hasura) Add your deployed connector's URL to the `DataSource` section. @@ -28,16 +42,10 @@ Run the code actions: > Hasura: Track all collections... ``` -Find an existing project or create one: - -``` -hasura3 cloud project create -``` - Then use your metadata referencing your connector to create a build: -``` -hasura3 cloud build create --project-id MY-PROJECT-ID --metadata-file metadata.hml +```bash +hasura3 build create ``` ## Using as a Stand-Alone Connector @@ -51,10 +59,10 @@ This means that you don't need to specify any functions, etc. This can be a pattern you can use to quickly provide connectors that can be shared by the Hasura community and on the [Connector Hub](https://hasura.io/connectors) -``` +```bash hasura3 connector create sendgrid:deno:standalone:v1 \ --github-repo-url https://github.com/hasura/ndc-sendgrid-deno/tree/main \ - --config-file config.json \ + --config-file <(echo '{}') \ --env SENDGRID_API_KEY='YOUR_SENDGRID_API_KEY' \ --env SERVICE_TOKEN_SECRET='MY-PRESHARED-HASURA-TOKEN' -``` \ No newline at end of file +``` diff --git a/build-profile.yaml b/build-profile.yaml new file mode 100644 index 0000000..7b605e8 --- /dev/null +++ b/build-profile.yaml @@ -0,0 +1,19 @@ +version: 2 +spec: + environment: default + mode: replace + supergraph: + resources: + - supergraph/* + + # Subgraphs can be used to group certain objects of your metadata + # in a way that makes sense for you and your team + # You can have multiple HML files belong to a subgraph + # and you can have multiple subgraphs in a project + # We have created a default subgraph for you, and added a HML file to it to get you started + # Please note that default is a special subgraph that is created for every project + # and it cannot be deleted + subgraphs: + - name: default + resources: + - subgraphs/default/**/*.hml diff --git a/config.json b/config.json deleted file mode 100644 index 6f31cf5..0000000 --- a/config.json +++ /dev/null @@ -1 +0,0 @@ -{ } \ No newline at end of file diff --git a/functions/index.ts b/functions/index.ts index e5d70b1..d8c9109 100644 --- a/functions/index.ts +++ b/functions/index.ts @@ -1,8 +1,13 @@ -import { sendSimpleMail, IResult } from 'https://deno.land/x/sendgrid@0.0.3/mod.ts'; +import { sendSimpleMail } from 'https://deno.land/x/sendgrid@0.0.3/mod.ts'; + +type Result = { + success: boolean, + errors: string[] +} /** * Uses the SendGrid API to send an email. - * + * * @param subject The email subject * @param to The address to send the email to * @param from Who to list as the sender of the email @@ -10,7 +15,7 @@ import { sendSimpleMail, IResult } from 'https://deno.land/x/sendgrid@0.0.3/mod. * @param html The HTML content of the email * @returns Success or errors */ -export async function send(subject: string, to: string, from: string, plain: string, html: string ): Promise { +export async function send(subject: string, to: string, from: string, plain: string, html: string ): Promise { const API_KEY = Deno.env.get("SENDGRID_API_KEY"); if(! API_KEY) { @@ -32,5 +37,8 @@ export async function send(subject: string, to: string, from: string, plain: str }, ); - return response; + return { + success: response.success, + errors: response.errors ?? [] + }; } diff --git a/hasura.yaml b/hasura.yaml new file mode 100644 index 0000000..ae10333 --- /dev/null +++ b/hasura.yaml @@ -0,0 +1,15 @@ +version: 1 +# TODO: Put your project name here +project: your-project-name + +# A build profile specifies your projects environment +# and the association of subgraphs to HML files +# You can create different build profiles for different environments, +# like production and staging, which may reference environment specific auth configs +# We have created one for you to get you started +buildProfiles: + - build-profile.yaml + +# Default build profile refers to build profile +# that is used when you create a build and don't specify the build profile +defaultBuildProfile: build-profile.yaml diff --git a/metadata.hml b/metadata.hml deleted file mode 100644 index 9b3f34b..0000000 --- a/metadata.hml +++ /dev/null @@ -1,15 +0,0 @@ -kind: "AuthConfig" -allowRoleEmulationFor: "admin" -webhook: - mode: "POST" - webhookUrl: "https://auth.pro.hasura.io/webhook/ddn?role=admin" ---- -kind: DataSource -name: sendgrid -dataConnectorUrl: 'http://my-connector-url' -auth: - type: "Bearer" - token: "my-secret-token" - -# Run> Hasura: Refresh data source -# Run> Hasura: Track all collections... \ No newline at end of file diff --git a/subgraphs/default/sendgrid.hml b/subgraphs/default/sendgrid.hml new file mode 100644 index 0000000..f203705 --- /dev/null +++ b/subgraphs/default/sendgrid.hml @@ -0,0 +1,12 @@ +kind: DataConnector +version: v1 +definition: + name: sendgrid + url: + singleUrl: 'http://my-connector-url' + headers: + Authentication: + value: Bearer MY-PRESHARED-HASURA-TOKEN + +# Run> Hasura: Refresh data connector +# Run> Hasura: Track all collections... diff --git a/supergraph/auth-config.hml b/supergraph/auth-config.hml new file mode 100644 index 0000000..e53e7cc --- /dev/null +++ b/supergraph/auth-config.hml @@ -0,0 +1,8 @@ +kind: AuthConfig +version: v1 +definition: + allowRoleEmulationBy: admin + mode: + webhook: + method: Post + url: https://auth.pro.hasura.io/webhook/ddn?role=admin diff --git a/supergraph/compatibility-config.hml b/supergraph/compatibility-config.hml new file mode 100644 index 0000000..b0da4c6 --- /dev/null +++ b/supergraph/compatibility-config.hml @@ -0,0 +1,2 @@ +kind: CompatibilityConfig +date: 2023-11-29