|
1 |
| -### Hasura Python Lambda Connector |
| 1 | +# Hasura Python Lambda Connector |
| 2 | +<a href="https://www.python.org/"><img src="https://github.com/hasura/ndc-python-lambda/blob/main/docs/logo.svg" align="right" width="200"></a> |
| 3 | + |
| 4 | +[](https://hasura.io/connectors/python) |
| 5 | +[](https://hasura.io/connectors/python) |
| 6 | +[](https://github.com/hasura/ndc-python-lambda/blob/main/LICENSE.txt) |
| 7 | +[](https://github.com/hasura/ndc-python-lambda/blob/main/README.md) |
2 | 8 |
|
3 | 9 | This connector allows you to write Python code and call it using Hasura!
|
4 | 10 |
|
5 | 11 | With Hasura, you can integrate -- and even host -- this business logic directly with Hasura DDN and your API.
|
6 | 12 |
|
7 | 13 | You can handle custom business logic using the Python Lambda data connector. Using this connector, you can transform or enrich data before it reaches your customers, or perform any other business logic you may need.
|
8 | 14 |
|
9 |
| -You can then integrate these functions as individual commands in your metadata and API. |
10 |
| -This process enables you to simplify client applications and speed up your backend development! |
| 15 | +You can then integrate these functions as individual commands in your metadata and API. This process enables you to simplify client applications and speed up your backend development! |
11 | 16 |
|
12 |
| -## Setting up the Python Lambda connector |
| 17 | +This connector is built using the [Python Data Connector SDK](https://github.com/hasura/ndc-sdk-python) and implements the [Data Connector Spec](https://github.com/hasura/ndc-spec). |
13 | 18 |
|
14 |
| -### Prerequisites: |
15 |
| -In order to follow along with this guide, you will need: |
16 |
| -* [The DDN CLI, VS Code extension, and Docker installed](https://hasura.io/docs/3.0/getting-started/build/prerequisites/) |
17 |
| -* Python version `>= 3.11` |
| 19 | +## Before you get Started |
18 | 20 |
|
19 |
| -In this guide we will setup a new Hasura DDN project from scratch. |
| 21 | +1. The [DDN CLI](https://hasura.io/docs/3.0/cli/installation) and [Docker](https://docs.docker.com/engine/install/) installed |
| 22 | +2. A [supergraph](https://hasura.io/docs/3.0/getting-started/init-supergraph) |
| 23 | +3. A [subgraph](https://hasura.io/docs/3.0/getting-started/init-subgraph) |
20 | 24 |
|
21 |
| -### Step-by-step guide |
| 25 | +The steps below explain how to Initialize and configure a connector for local development. You can learn how to deploy a connector — after it's been configured — [here](https://hasura.io/docs/3.0/getting-started/deployment/deploy-a-connector). |
22 | 26 |
|
23 |
| -Create a new directory that will contain your Hasura project and change directories into it. |
| 27 | +## Using the Python connector |
24 | 28 |
|
25 |
| -```mkdir ddn && cd ddn``` |
| 29 | +### Step 1: Authenticate your CLI session |
26 | 30 |
|
27 |
| -Create a new supergraph: |
| 31 | +```bash |
| 32 | +ddn auth login |
| 33 | +``` |
28 | 34 |
|
29 |
| -```ddn supergraph init --dir .``` |
| 35 | +### Step 2: Configure the connector |
30 | 36 |
|
31 |
| -Start a watch session, additionally split of a new terminal to continue running commands from. |
| 37 | +Once you have an initialized supergraph and subgraph, run the initialization command in interactive mode while providing a name for the connector in the prompt: |
32 | 38 |
|
33 |
| -```HASURA_DDN_PAT=$(ddn auth print-pat) docker compose up --build --watch``` |
| 39 | +```bash |
| 40 | +ddn connector init python -i |
| 41 | +``` |
34 | 42 |
|
35 |
| -In the new terminal, perform a local build: |
| 43 | +#### Step 2.1: Choose the `hasura/python` option from the list |
36 | 44 |
|
37 |
| -```ddn supergraph build local --output-dir engine``` |
| 45 | +#### Step 2.2: Choose a port for the connector |
38 | 46 |
|
39 |
| -Initialize a subgraph: |
| 47 | +The CLI will ask for a specific port to run the connector on. Choose a port that is not already in use or use the default suggested port. |
40 | 48 |
|
41 |
| -``` |
42 |
| -ddn subgraph init python \ |
43 |
| - --dir python \ |
44 |
| - --target-supergraph supergraph.local.yaml \ |
45 |
| - --target-supergraph supergraph.cloud.yaml |
46 |
| -``` |
| 49 | +### Step 3: Introspect the connector |
47 | 50 |
|
48 |
| -Initialize a Python connector: |
| 51 | +Introspecting the connector will generate a `config.json` file and a `python.hml` file. |
49 | 52 |
|
50 |
| -``` |
51 |
| -ddn connector init python \ |
52 |
| - --subgraph python/subgraph.yaml \ |
53 |
| - --hub-connector hasura/python \ |
54 |
| - --configure-port 8085 \ |
55 |
| - --add-to-compose-file compose.yaml |
| 53 | +```bash |
| 54 | +ddn connector introspect python |
56 | 55 | ```
|
57 | 56 |
|
58 |
| -In the `.env.local` you will need to remove the `HASURA_CONNECTOR_PORT` variable which is set to `8085`. This is because the connector will run on that port but the docker-mapping is set to map 8085 -> 8080. |
| 57 | +### Step 4: Add your resources |
59 | 58 |
|
60 |
| -Before: |
61 |
| -``` |
62 |
| -OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://local.hasura.dev:4317 |
63 |
| -OTEL_SERVICE_NAME=python_python |
64 |
| -HASURA_CONNECTOR_PORT=8085 |
65 |
| -``` |
| 59 | +You can add the models, commands, and relationships to your API by tracking them which generates the HML files. |
66 | 60 |
|
67 |
| -After: |
68 |
| -``` |
69 |
| -OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://local.hasura.dev:4317 |
70 |
| -OTEL_SERVICE_NAME=python_python |
| 61 | +```bash |
| 62 | +ddn connector-link add-resources python |
71 | 63 | ```
|
72 | 64 |
|
73 |
| -Add the connector link: |
| 65 | +### Step 5: Run your connector |
74 | 66 |
|
75 |
| -``` |
76 |
| -ddn connector-link add python \ |
77 |
| - --subgraph python/subgraph.yaml \ |
78 |
| - --configure-host http://local.hasura.dev:8085 \ |
79 |
| - --target-env-file python/.env.python.local |
80 |
| -``` |
| 67 | +You can run your connector locally, or include it in the docker setup. |
81 | 68 |
|
82 |
| -Stop the watch session using Ctrl-C and restart it. |
| 69 | +#### Run the connector in Docker |
83 | 70 |
|
84 |
| -``` |
85 |
| -HASURA_DDN_PAT=$(ddn auth print-pat) docker compose up --build --watch |
| 71 | +To include your connector in the docker setup, include its compose file at the top of your supergraph `compose.yaml` file like this: |
| 72 | + |
| 73 | +```yaml |
| 74 | +include: |
| 75 | + - path: app/connector/python/compose.yaml |
86 | 76 | ```
|
87 | 77 |
|
88 |
| -Once the connector is running, you can update the connector-link. |
| 78 | +#### Run the connector locally |
89 | 79 |
|
90 |
| -``` |
91 |
| -ddn connector-link update python \ |
92 |
| - --subgraph python/subgraph.yaml \ |
93 |
| - --env-file python/.env.python.local \ |
94 |
| - --add-all-resources |
95 |
| -``` |
| 80 | +To run your connector outside of Docker first go into the connector directory: |
96 | 81 |
|
97 |
| -Push the build to the locally running engine: |
| 82 | +`cd app/connector/python` |
98 | 83 |
|
99 |
| -``` |
100 |
| -ddn supergraph build local \ |
101 |
| - --output-dir engine \ |
102 |
| - --subgraph-env-file python:python/.env.python.local |
103 |
| -``` |
| 84 | +Install the requirements: |
104 | 85 |
|
105 |
| -Now you should be able to write your code in the `functions.py` file, and each time you make changes and save the connector should automatically restart inside the watch session, you'll then need to track those changes and push them to engine. |
| 86 | +`pip3 install -r requirements.txt` |
106 | 87 |
|
107 |
| -You can do that by re-running the above commands: |
| 88 | +Then run the connector locally: |
108 | 89 |
|
109 |
| -To track the changes: |
| 90 | +```ddn connector setenv --connector connector.yaml -- python3 functions.py serve``` |
110 | 91 |
|
111 |
| -``` |
112 |
| -ddn connector-link update python \ |
113 |
| - --subgraph python/subgraph.yaml \ |
114 |
| - --env-file python/.env.python.local \ |
115 |
| - --add-all-resources |
116 |
| -``` |
| 92 | +## Documentation |
117 | 93 |
|
118 |
| -To push these changes to engine: |
| 94 | +View the full documentation for the Python Lambda connector [here](https://github.com/hasura/ndc-python-lambda/blob/main/docs/index.md). |
119 | 95 |
|
120 |
| -``` |
121 |
| -ddn supergraph build local \ |
122 |
| - --output-dir engine \ |
123 |
| - --subgraph-env-file python:python/.env.python.local |
124 |
| -``` |
| 96 | +## Contributing |
| 97 | + |
| 98 | +Check out our [contributing guide](https://github.com/hasura/ndc-python-lambda/blob/main/docs/contributing.md) for more details. |
| 99 | + |
| 100 | +## License |
| 101 | + |
| 102 | +The Turso connector is available under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). |
0 commit comments