Skip to content

Commit 7df99fa

Browse files
committed
Add documentation
1 parent 911ca81 commit 7df99fa

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

.vale/styles/Infrahub/branded-terms-case-swap.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ swap:
1313
(?:[Gg]itlab): GitLab
1414
(?:gitpod): GitPod
1515
(?:grafana): Grafana
16-
(?:[^/][Gg]raphql): GraphQL
1716
(?:[Ii]nflux[Dd]b): InfluxDB
1817
infrahub(?:\s|$): Infrahub
1918
(?:jinja2): Jinja2
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# `infrahubctl graphql`
2+
3+
Various GraphQL related commands.
4+
5+
**Usage**:
6+
7+
```console
8+
$ infrahubctl graphql [OPTIONS] COMMAND [ARGS]...
9+
```
10+
11+
**Options**:
12+
13+
* `--install-completion`: Install completion for the current shell.
14+
* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.
15+
* `--help`: Show this message and exit.
16+
17+
**Commands**:
18+
19+
* `export-schema`: Export the GraphQL schema to a file.
20+
* `generate-return-types`: Create Pydantic Models for GraphQL query...
21+
22+
## `infrahubctl graphql export-schema`
23+
24+
Export the GraphQL schema to a file.
25+
26+
**Usage**:
27+
28+
```console
29+
$ infrahubctl graphql export-schema [OPTIONS]
30+
```
31+
32+
**Options**:
33+
34+
* `--destination PATH`: Path to the GraphQL schema file. [default: schema.graphql]
35+
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
36+
* `--help`: Show this message and exit.
37+
38+
## `infrahubctl graphql generate-return-types`
39+
40+
Create Pydantic Models for GraphQL query return types
41+
42+
**Usage**:
43+
44+
```console
45+
$ infrahubctl graphql generate-return-types [OPTIONS] [QUERY]
46+
```
47+
48+
**Arguments**:
49+
50+
* `[QUERY]`: Location of the GraphQL query file(s). [default: /Users/damien/projects/infrahub-sdk-python-pink]
51+
52+
**Options**:
53+
54+
* `--schema PATH`: Path to the GraphQL schema file. [default: schema.graphql]
55+
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
56+
* `--help`: Show this message and exit.

docs/docs/python-sdk/guides/python-typing.mdx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,51 @@ my_object = client.get(MyOwnObject, name__value="example")
101101
```
102102

103103
> if you don't have your own Python module, it's possible to use relative path by having the `procotols.py` in the same directory as your script/transform/generator
104+
105+
## Generating Pydantic models from GraphQL queries
106+
107+
When working with GraphQL queries, you can generate type-safe Pydantic models that correspond to your query return types. This provides excellent type safety and IDE support for your GraphQL operations.
108+
109+
### Why use generated return types?
110+
111+
Generated Pydantic models from GraphQL queries offer several important benefits:
112+
113+
- **Type Safety**: Catch type errors at development time instead of runtime
114+
- **IDE Support**: Get autocomplete, type hints, and better IntelliSense in your IDE
115+
- **Documentation**: Generated models serve as living documentation of your GraphQL API
116+
- **Validation**: Automatic validation of query responses against the expected schema
117+
118+
### Generating return types
119+
120+
Use the `infrahubctl graphql generate-return-types` command to create Pydantic models from your GraphQL queries:
121+
122+
```shell
123+
# Generate models for queries in current directory
124+
infrahubctl graphql generate-return-types
125+
126+
# Generate models for specific query files
127+
infrahubctl graphql generate-return-types queries/get_devices.gql
128+
```
129+
130+
> You can also export the GraphQL schema first using the `infrahubctl graphql export-schema` command:
131+
132+
### Example workflow
133+
134+
1. **Create your GraphQL queries** in `.gql` files:
135+
136+
2. **Generate the Pydantic models**:
137+
138+
```shell
139+
infrahubctl graphql generate-return-types queries/
140+
```
141+
142+
The command will generate the Python file per query based on the name of the query.
143+
144+
3. **Use the generated models** in your Python code
145+
146+
```python
147+
from .queries.get_devices import GetDevicesQuery
148+
149+
response = await client.execute_graphql(query=MY_QUERY)
150+
data = GetDevicesQuery(**response)
151+
```

0 commit comments

Comments
 (0)