Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/repository-dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
uses: actions/checkout@v5

- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v3
uses: peter-evans/repository-dispatch@v4
with:
token: ${{ secrets.GH_UPDATE_PACKAGE_OTTO }}
# if matrix.repo contains a slash, use it literally; otherwise look up the secret named by matrix.repo
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20
1 change: 0 additions & 1 deletion .vale/styles/Infrahub/branded-terms-case-swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ swap:
(?:[Gg]itlab): GitLab
(?:gitpod): GitPod
(?:grafana): Grafana
(?:[^/][Gg]raphql): GraphQL
(?:[Ii]nflux[Dd]b): InfluxDB
infrahub(?:\s|$): Infrahub
(?:jinja2): Jinja2
Expand Down
1 change: 1 addition & 0 deletions changelog/+gql-command.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `infrahubctl graphql` commands to export schema and generate Pydantic types from GraphQL queries
56 changes: 56 additions & 0 deletions docs/docs/infrahubctl/infrahubctl-graphql.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# `infrahubctl graphql`

Various GraphQL related commands.

**Usage**:

```console
$ infrahubctl graphql [OPTIONS] COMMAND [ARGS]...
```

**Options**:

* `--install-completion`: Install completion for the current shell.
* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.
* `--help`: Show this message and exit.

**Commands**:

* `export-schema`: Export the GraphQL schema to a file.
* `generate-return-types`: Create Pydantic Models for GraphQL query...

## `infrahubctl graphql export-schema`

Export the GraphQL schema to a file.

**Usage**:

```console
$ infrahubctl graphql export-schema [OPTIONS]
```

**Options**:

* `--destination PATH`: Path to the GraphQL schema file. [default: schema.graphql]
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
* `--help`: Show this message and exit.

## `infrahubctl graphql generate-return-types`

Create Pydantic Models for GraphQL query return types

**Usage**:

```console
$ infrahubctl graphql generate-return-types [OPTIONS] [QUERY]
```

**Arguments**:

* `[QUERY]`: Location of the GraphQL query file(s). Defaults to current directory if not specified.

**Options**:

* `--schema PATH`: Path to the GraphQL schema file. [default: schema.graphql]
* `--config-file TEXT`: [env var: INFRAHUBCTL_CONFIG; default: infrahubctl.toml]
* `--help`: Show this message and exit.
50 changes: 49 additions & 1 deletion docs/docs/python-sdk/guides/python-typing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,52 @@ from lib.protocols import MyOwnObject
my_object = client.get(MyOwnObject, name__value="example")
```

> 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
> if you don't have your own Python module, it's possible to use relative path by having the `protocols.py` in the same directory as your script/transform/generator

## Generating Pydantic models from GraphQL queries

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.

### Why use generated return types?

Generated Pydantic models from GraphQL queries offer several important benefits:

- **Type Safety**: Catch type errors during development time instead of at runtime
- **IDE Support**: Get autocomplete, type hints, and better IntelliSense in your IDE
- **Documentation**: Generated models serve as living documentation of your GraphQL API
- **Validation**: Automatic validation of query responses against the expected schema

### Generating return types

Use the `infrahubctl graphql generate-return-types` command to create Pydantic models from your GraphQL queries:

```shell
# Generate models for queries in current directory
infrahubctl graphql generate-return-types

# Generate models for specific query files
infrahubctl graphql generate-return-types queries/get_devices.gql
```

> You can also export the GraphQL schema first using the `infrahubctl graphql export-schema` command:

### Example workflow

1. **Create your GraphQL queries** in `.gql` files:

2. **Generate the Pydantic models**:

```shell
infrahubctl graphql generate-return-types queries/
```

The command will generate the Python file per query based on the name of the query.

3. **Use the generated models** in your Python code

```python
from .queries.get_devices import GetDevicesQuery

response = await client.execute_graphql(query=MY_QUERY)
data = GetDevicesQuery(**response)
```
2 changes: 1 addition & 1 deletion docs/docs/python-sdk/guides/resource-manager.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ We will use `allocate_next_ip_address` to allocated an IP address out of the `Co

We provided a data argument to method. This data argument is used to pass attributes and relationships that we want to set on the allocated resource. In this case we are setting to the description of the allocated IP Address.

You can allocate resources in an idempotent way by passing an identifier argument to the allocation method. This identifier links the resource pool with the allocated resource allowing us to create idempotent allocation behavior. This is crucial when you want to allocate resources in an idempotent way using [generators](https://github.com/opsmill/infrahub-docs/blob/main/docs/docs/topics/generator).
You can allocate resources in an idempotent way by passing an identifier argument to the allocation method. This identifier links the resource pool with the allocated resource allowing us to create idempotent allocation behavior. This is crucial when you want to allocate resources in an idempotent way using [generators](https://docs.infrahub.app/topics/generator).

In this example we are executing the `allocate_next_ip_address` method 2 times, using the same identifier.

Expand Down
4 changes: 3 additions & 1 deletion docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const config: Config = {
organizationName: 'opsmill',
projectName: 'infrahub-sdk-python',
onBrokenLinks: 'throw',
onBrokenMarkdownLinks: 'warn',
onDuplicateRoutes: "throw",
// Even if you don't use internationalization, you can use this field to set
// useful metadata like html lang. For example, if your site is Chinese, you
Expand Down Expand Up @@ -123,6 +122,9 @@ const config: Config = {
});
return transformedContent;
},
hooks: {
onBrokenMarkdownLinks: 'warn',
},
},
};

Expand Down
Loading