Skip to content

Commit

Permalink
H-454: Update Python OpenAPI generation to include filters (#2932)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Diekmann <[email protected]>
  • Loading branch information
indietyp and TimDiekmann authored Aug 18, 2023
1 parent d159f62 commit bffb448
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/hash-graph/.justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ run *arguments:
generate-openapi-specs:
cargo run --bin hash-graph -- server --write-openapi-specs
just yarn codegen --filter @local/hash-graph-client-python
just yarn codegen --filter @local/hash-graph-sdk-python

[private]
test *arguments:
Expand Down
4 changes: 4 additions & 0 deletions libs/@local/hash-graph-sdk/python/graph_sdk/filter/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ def inherits_from(self) -> Path:
"""Return the path to the inherits_from attribute of an entity type."""
return self.path.push(EntityTypeQueryToken.inherits_from)

def children(self) -> Path:
"""Return the path to the children attribute of an entity type."""
return self.path.push(EntityTypeQueryToken.children)


class EntityQueryPath(AbstractQueryPath):
"""A query path for an entity."""
Expand Down
2 changes: 2 additions & 0 deletions libs/@local/hash-graph-sdk/python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"description": "Type definitions for the type system and entity validation based on schemas",
"scripts": {
"build": "poetry build --no-interaction",
"codegen:blocking": "./scripts/codegen_blocking.sh",
"codegen:filter": "./scripts/codegen_filter.sh",
"fix:black": "poetry run black graph_sdk",
"fix:lock-files": "poetry lock --no-update",
"fix:ruff": "poetry run ruff graph_sdk --fix",
Expand Down
18 changes: 18 additions & 0 deletions libs/@local/hash-graph-sdk/python/scripts/codegen_blocking.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail

# Thanks to: https://stackoverflow.com/a/246128/9077988
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )

poetry run python "$DIR/generate_blocking.py"

# We need to run black twice because ruff changes the output
poetry run black "$DIR/../graph_sdk/client/blocking.py"
poetry run ruff --fix "$DIR/../graph_sdk/client/blocking.py" || true
poetry run black "$DIR/../graph_sdk/client/blocking.py"
18 changes: 18 additions & 0 deletions libs/@local/hash-graph-sdk/python/scripts/codegen_filter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail

# Thanks to: https://stackoverflow.com/a/246128/9077988
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )

poetry run python "$DIR/generate_filter.py"

# We need to run black twice because ruff changes the output
poetry run black "$DIR/../graph_sdk/client/blocking.py"
poetry run ruff --fix "$DIR/../graph_sdk/filter/path.py" || true
poetry run black "$DIR/../graph_sdk/filter/path.py"
11 changes: 11 additions & 0 deletions libs/@local/hash-graph-sdk/python/turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"extends": ["//"],
"pipeline": {
"codegen:blocking": {
"inputs": ["./graph_sdk/client/concurrent.py"],
"outputs": ["./graph_sdk/client/blocking.py"]
},
"codegen:filter": {
"inputs": ["../../../../apps/hash-graph/openapi/openapi.json"],
"outputs": ["./graph_sdk/filter/path.py"]
},
"codegen": {
"dependsOn": ["codegen:blocking", "codegen:filter"]
},
"build": {
"dependsOn": ["^build", "codegen"],
"inputs": ["./**/*.py", "pyproject.toml", "poetry.lock", "LICENSE*"],
Expand Down

0 comments on commit bffb448

Please sign in to comment.