Skip to content

Commit 50e5a99

Browse files
Data Modeling Commands Section Rename and Reformat (#925)
Co-authored-by: Rob Dominguez <[email protected]>
1 parent ff18558 commit 50e5a99

11 files changed

+276
-60
lines changed

docs/data-modeling/command.mdx

+51-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
sidebar_position: 4
3-
sidebar_label: "Commands modify data"
4-
description: "Commands allow you to modify data in your data source."
3+
sidebar_label: "Commands act on data"
4+
description: "Commands allow you to act on data in your data source."
55
toc_max_heading_level: 4
66
keywords:
77
- command
@@ -13,17 +13,20 @@ import TabItem from "@theme/TabItem";
1313
import Thumbnail from "@site/src/components/Thumbnail";
1414

1515
import CommandCreateClickHouse from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-create-tutorial.mdx";
16-
import CommandCreateClickHouseNativeMutationTutorial from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-create-native-mutation-tutorial.mdx";
17-
import CommandCreateClickHouseNativeMutationHowTo from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-create-native-mutation-how-to.mdx";
16+
import CommandCreateClickHouseNativeOperationHowTo from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-create-native-operation-how-to.mdx";
17+
import CommandCreateClickHouseNativeOperationTutorial from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-create-native-operation-tutorial.mdx";
1818
import CommandCreateLambdaGo from "@site/docs/data-modeling/partials/lambda-connectors/go/_command-create-tutorial.mdx";
19+
import CommandCreateLambdaGoNativeOperationHowTo from "@site/docs/data-modeling/partials/lambda-connectors/go/_command-create-native-operation-how-to.mdx";
1920
import CommandCreateLambdaPython from "@site/docs/data-modeling/partials/lambda-connectors/python/_command-create-tutorial.mdx";
21+
import CommandCreateLambdaPythonNativeOperationHowTo from "@site/docs/data-modeling/partials/lambda-connectors/python/_command-create-native-operation-how-to.mdx";
2022
import CommandCreateLambdaTypescript from "@site/docs/data-modeling/partials/lambda-connectors/typescript/_command-create-tutorial.mdx";
23+
import CommandCreateLambdaTypescriptNativeOperationHowTo from "@site/docs/data-modeling/partials/lambda-connectors/typescript/_command-create-native-operation-how-to.mdx";
2124
import CommandCreateMongoDB from "@site/docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-tutorial.mdx";
22-
import CommandCreateMongoDBNativeMutationTutorial from "@site/docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-native-mutation-tutorial.mdx";
23-
import CommandCreateMongoDBNativeMutationHowTo from "@site/docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-native-mutation-how-to.mdx";
25+
import CommandCreateMongoDBNativeOperationHowTo from "@site/docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-native-operation-how-to.mdx";
26+
import CommandCreateMongoDBNativeOperationTutorial from "@site/docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-native-operation-tutorial.mdx";
2427
import CommandCreatePostgreSQL from "@site/docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-tutorial.mdx";
25-
import CommandCreatePostgreSQLNativeMutationTutorial from "@site/docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-mutation-tutorial.mdx";
26-
import CommandCreatePostgreSQLNativeMutationHowTo from "@site/docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-mutation-how-to.mdx";
28+
import CommandCreatePostgreSQLNativeOperationHowTo from "@site/docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-operation-how-to.mdx";
29+
import CommandCreatePostgreSQLNativeOperationTutorial from "@site/docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-operation-tutorial.mdx";
2730
import CommandUpdateClickHouse from "@site/docs/data-modeling/partials/classic-connectors/clickHouse/_command-update-tutorial.mdx";
2831
import CommandUpdateLambdaGo from "@site/docs/data-modeling/partials/lambda-connectors/go/_command-update-tutorial.mdx";
2932
import CommandUpdateLambdaPython from "@site/docs/data-modeling/partials/lambda-connectors/python/_command-update-tutorial.mdx";
@@ -36,7 +39,7 @@ import CommandUpdatePostgreSQL from "@site/docs/data-modeling/partials/classic-c
3639
## Introduction
3740

3841
In DDN, commands represent operations in your API that can be executed such as those which modify data in your data
39-
sources, (inserts, updates, deletes), or custom business logic operations.
42+
sources, (inserts, updates, deletes), or that perform complex read operations or execute custom business logic.
4043

4144
## Lifecycle
4245

@@ -59,6 +62,9 @@ that point.
5962

6063
### From a source operation
6164

65+
Some data connectors support the ability to introspect your data source to discover the commands that can be added to
66+
your supergraph automatically.
67+
6268
```bash title="Introspect your data source:"
6369
ddn connector introspect <connector_name>
6470
```
@@ -79,17 +85,36 @@ ddn command add <connector_link_name> "*"
7985

8086
This will add commands with their accompanying metadata definitions to your metadata.
8187

82-
### Via native mutation {#via-native-mutation-how-to}
88+
### Via native operations {#via-native-operations-how-to}
89+
90+
Some data connectors support the ability to add commands via native operations so that you can add any operation that is
91+
not supported by the automatic introspection process.
92+
93+
For classic database connectors, this will be native query code for that source. This can be, for example, a more
94+
complex read operation or a way to run custom business logic, which can be exposed as queries or mutations in the
95+
GraphQL API.
96+
97+
For Lambda connectors, eg: (TypeScript, Go, Python, etc) this will be a function (read-only) or procedure (mutation or
98+
other side-effects) that can also be exposed as a query or mutation in the GraphQL API.
8399

84100
<Tabs groupId="source-preference" className="api-tabs">
85101
<TabItem value="PostgreSQL" label="PostgreSQL">
86-
<CommandCreatePostgreSQLNativeMutationHowTo />
102+
<CommandCreatePostgreSQLNativeOperationHowTo />
87103
</TabItem>
88104
<TabItem value="MongoDB" label="MongoDB">
89-
<CommandCreateMongoDBNativeMutationHowTo />
105+
<CommandCreateMongoDBNativeOperationHowTo />
90106
</TabItem>
91107
<TabItem value="ClickHouse" label="ClickHouse">
92-
<CommandCreateClickHouseNativeMutationHowTo />
108+
<CommandCreateClickHouseNativeOperationHowTo />
109+
</TabItem>
110+
<TabItem value="TypeScript" label="Node.js TypeScript">
111+
<CommandCreateLambdaTypescriptNativeOperationHowTo />
112+
</TabItem>
113+
<TabItem value="Python" label="Python">
114+
<CommandCreateLambdaPythonNativeOperationHowTo />
115+
</TabItem>
116+
<TabItem value="Go" label="Go">
117+
<CommandCreateLambdaGoNativeOperationHowTo />
93118
</TabItem>
94119
</Tabs>
95120

@@ -146,28 +171,28 @@ To modify data, you first need to create a command that maps to a specific opera
146171
<TabItem value="ClickHouse" label="ClickHouse">
147172
<CommandCreateClickHouse />
148173
</TabItem>
149-
<TabItem value="TypeScript" label="Node.js TypeScript">
150-
<CommandCreateLambdaTypescript />
151-
</TabItem>
152-
<TabItem value="Python" label="Python">
153-
<CommandCreateLambdaPython />
154-
</TabItem>
155-
<TabItem value="Go" label="Go">
156-
<CommandCreateLambdaGo />
157-
</TabItem>
158174
</Tabs>
159175

160-
#### Via native mutation {#via-native-mutation-tutorials}
176+
#### Via native operations {#via-native-operation-tutorials}
161177

162178
<Tabs groupId="source-preference" className="api-tabs">
163179
<TabItem value="PostgreSQL" label="PostgreSQL">
164-
<CommandCreatePostgreSQLNativeMutationTutorial />
180+
<CommandCreatePostgreSQLNativeOperationTutorial />
165181
</TabItem>
166182
<TabItem value="MongoDB" label="MongoDB">
167-
<CommandCreateMongoDBNativeMutationTutorial />
183+
<CommandCreateMongoDBNativeOperationTutorial />
168184
</TabItem>
169185
<TabItem value="ClickHouse" label="ClickHouse">
170-
<CommandCreateClickHouseNativeMutationTutorial />
186+
<CommandCreateClickHouseNativeOperationTutorial />
187+
</TabItem>
188+
<TabItem value="TypeScript" label="Node.js TypeScript">
189+
<CommandCreateLambdaTypescript />
190+
</TabItem>
191+
<TabItem value="Python" label="Python">
192+
<CommandCreateLambdaPython />
193+
</TabItem>
194+
<TabItem value="Go" label="Go">
195+
<CommandCreateLambdaGo />
171196
</TabItem>
172197
</Tabs>
173198

docs/data-modeling/partials/classic-connectors/mongoDB/_command-create-native-mutation-how-to.mdx

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
In MongoDB, you can create a [native operation](/reference/connectors/mongodb/native-operations/index.mdx) by using an
2+
[aggregation pipeline](https://www.mongodb.com/docs/manual/core/aggregation-pipeline/) in a JSON file by adding it to
3+
your connector's directory.
4+
5+
See the syntax for MongoDB native operations [here](/reference/connectors/mongodb/native-operations/syntax.mdx).
6+
7+
Store your native operation in an appropriate directory, possibly named for the operation type, in your connector's
8+
directory.
9+
10+
```sh title="Introspect your MongoDB instance:"
11+
ddn connector introspect <connector_name>
12+
```
13+
14+
```sh title="Show the found resources:"
15+
ddn connector show-resources <connector_name>
16+
```
17+
18+
```sh title="Then, add your model:"
19+
ddn command add <connector_name> <operation_name>
20+
```
21+
22+
```title="Build and serve your supergraph API:"
23+
ddn supergraph build local
24+
ddn run docker-start
25+
```
26+
27+
Now in your console you can run the operation with GraphQL.
28+
29+
See the tutorial for creating a native operation in MongoDB [here](#via-native-operation-tutorials).

docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-mutation-how-to.mdx renamed to docs/data-modeling/partials/classic-connectors/postgreSQL/_command-create-native-operation-how-to.mdx

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
The process of creating a native mutation for PostgreSQL is the following.
1+
The process of creating a native operation for PostgreSQL is the following.
22

3-
Within your connector's directory, you can add a new file with a `.sql` extension to define a native mutation.
3+
Within your connector's directory, you can add a new file with a `.sql` extension to define a native operation.
44

5-
Then, use the PostgreSQL connector's plugin to add the native query to your connector's configuration:
5+
Then, use the PostgreSQL connector's plugin to add the native operation to your connector's configuration:
66

7-
```sh title="Then, use the PostgreSQL connector's plugin to add the native mutation to your connector's configuration:"
7+
```sh title="Then, use the PostgreSQL connector's plugin to add the native operation to your connector's configuration:"
88
ddn connector plugin \
99
--connector <subgraph_name>/<path-to-your-connector>/connector.yaml \
1010
-- \
1111
native-operation create \
12-
--operation-path <subgraph_name>/<path-to-your-connector>/native-operations/mutations/<mutation_name>.sql \
12+
--operation-path <subgraph_name>/<path-to-your-connector>/native-operations/<operation_type>/<operation_name>.sql \
1313
--kind mutation
1414
```
1515

16+
By specifying the `--kind mutation` flag, you are indicating that the operation is a mutation. If you specify
17+
`--kind query`, the operation will be a query.
18+
1619
```sh title="Introspect your PostgreSQL instance:"
1720
ddn connector introspect <connector_name>
1821
```
@@ -32,4 +35,4 @@ ddn run docker-start
3235

3336
Now in your console you can run the mutation with GraphQL.
3437

35-
See the tutorial for creating a native mutation in PostgreSQL [here](#via-native-mutation-tutorials).
38+
See the tutorial for creating a native mutation in PostgreSQL [here](#via-native-operation-tutorials).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
You can run whatever arbitrary code you want in your Go connector and expose it as a GraphQL mutation or query in your
2+
supergraph API.
3+
4+
```sh title="Initialize a new connector and select hasura/go from the list:"
5+
ddn connector init my_go -i
6+
```
7+
8+
```go title="Replace the functions.go contents with your own custom code:"
9+
package functions
10+
11+
import (
12+
"context"
13+
14+
"hasura-ndc.dev/ndc-go/types"
15+
)
16+
17+
// InputArguments represents the input of the native operation.
18+
type InputArguments struct {
19+
MyInput string `json:"myInput"`
20+
}
21+
22+
// OutputResult represents the output of the native operation.
23+
type OutputResult struct {
24+
MyOutput string `json:"myOutput"`
25+
}
26+
27+
// ProcedureCustomCode is a native operation that can be called from the API.
28+
func ProcedureCustomCode(ctx context.Context, state *types.State, arguments *InputArguments) (*OutputResult, error) {
29+
// Do something with the input
30+
return &OutputResult{
31+
MyOutput: "My output",
32+
}, nil
33+
}
34+
```
35+
36+
Using the prefix `Procedure` ensures ProcedureCustomCode() is exposed as a mutation in our API. Prefixing with
37+
`Function` identifies it as a function to be exposed as a query in your API.
38+
39+
Both have typed input arguments and return strings, which the connector will use to generate the corresponding GraphQL
40+
schema.
41+
42+
```bash title="Introspect the connector:"
43+
ddn connector introspect my_go
44+
```
45+
46+
```bash title="Track the function:"
47+
ddn command add my_go customCode
48+
```
49+
50+
```bash title="Create a new build:"
51+
ddn supergraph build local
52+
```
53+
54+
```bash title="Start your services:"
55+
ddn run docker-start
56+
```
57+
58+
```bash title="Open your development console:"
59+
ddn console --local
60+
```
61+
62+
```graphql title="You can now query your native operation:"
63+
query MyCustomCode {
64+
customCode(myInput: "My input")
65+
}
66+
```
67+
68+
```json title="You will see a response like this:"
69+
{
70+
"data": {
71+
"myCustomCode": "My output"
72+
}
73+
}
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
You can run whatever arbitrary code you want in your Python connector and expose it as a GraphQL mutation or query in
2+
your supergraph API.
3+
4+
```sh title="Initalize a new connector and select hasura/python from the list:"
5+
ddn connector init my_py -i
6+
```
7+
8+
```py title="Replace the functions.py contents with your own custom code:"
9+
from hasura_ndc import start
10+
from hasura_ndc.function_connector import FunctionConnector
11+
12+
connector = FunctionConnector()
13+
14+
@connector.register_query
15+
def my_custom_code(my_input: str) -> str:
16+
# Do something with the input
17+
return "My output"
18+
19+
if __name__ == "__main__":
20+
start(connector)
21+
```
22+
23+
By adding the `@connector.register_query` decorator, we are indicating that this function is to be exposed as an NDC
24+
function which will ultimately show up as a GraphQL query. If you use `@connector.register_mutation` instead, the
25+
function will be exposed as an NDC procedure which will be a GraphQL mutation.
26+
27+
```bash title="Introspect the connector:"
28+
ddn connector introspect my_py
29+
```
30+
31+
```bash title="Track the function:"
32+
ddn command add my_py my_custom_code
33+
```
34+
35+
```bash title="Create a new build:"
36+
ddn supergraph build local
37+
```
38+
39+
```bash title="Start your services:"
40+
ddn run docker-start
41+
```
42+
43+
```bash title="Open your development console:"
44+
ddn console --local
45+
```
46+
47+
```graphql title="You can now query your native operation:"
48+
query MyCustomCode {
49+
myCustomCode(myInput: "My input")
50+
}
51+
```
52+
53+
```json title="You will see a response like this:"
54+
{
55+
"data": {
56+
"myCustomCode": "My output"
57+
}
58+
}
59+
```

0 commit comments

Comments
 (0)