From bffb448aa1b4a81672f9293f1bf54780af2e95a0 Mon Sep 17 00:00:00 2001 From: Bilal Mahmoud Date: Fri, 18 Aug 2023 10:59:37 +0200 Subject: [PATCH] H-454: Update Python OpenAPI generation to include filters (#2932) Co-authored-by: Tim Diekmann --- apps/hash-graph/.justfile | 1 + .../python/graph_sdk/filter/path.py | 4 ++++ libs/@local/hash-graph-sdk/python/package.json | 2 ++ .../python/scripts/codegen_blocking.sh | 18 ++++++++++++++++++ .../python/scripts/codegen_filter.sh | 18 ++++++++++++++++++ libs/@local/hash-graph-sdk/python/turbo.json | 11 +++++++++++ 6 files changed, 54 insertions(+) create mode 100755 libs/@local/hash-graph-sdk/python/scripts/codegen_blocking.sh create mode 100755 libs/@local/hash-graph-sdk/python/scripts/codegen_filter.sh diff --git a/apps/hash-graph/.justfile b/apps/hash-graph/.justfile index b469ebf2fcd..e6c9d8d0e96 100755 --- a/apps/hash-graph/.justfile +++ b/apps/hash-graph/.justfile @@ -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: diff --git a/libs/@local/hash-graph-sdk/python/graph_sdk/filter/path.py b/libs/@local/hash-graph-sdk/python/graph_sdk/filter/path.py index af6c73f1ee8..fea8d473a01 100644 --- a/libs/@local/hash-graph-sdk/python/graph_sdk/filter/path.py +++ b/libs/@local/hash-graph-sdk/python/graph_sdk/filter/path.py @@ -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.""" diff --git a/libs/@local/hash-graph-sdk/python/package.json b/libs/@local/hash-graph-sdk/python/package.json index e8a4675e2f3..21a46929ca7 100644 --- a/libs/@local/hash-graph-sdk/python/package.json +++ b/libs/@local/hash-graph-sdk/python/package.json @@ -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", diff --git a/libs/@local/hash-graph-sdk/python/scripts/codegen_blocking.sh b/libs/@local/hash-graph-sdk/python/scripts/codegen_blocking.sh new file mode 100755 index 00000000000..8a30aa8f03f --- /dev/null +++ b/libs/@local/hash-graph-sdk/python/scripts/codegen_blocking.sh @@ -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" diff --git a/libs/@local/hash-graph-sdk/python/scripts/codegen_filter.sh b/libs/@local/hash-graph-sdk/python/scripts/codegen_filter.sh new file mode 100755 index 00000000000..04329397a85 --- /dev/null +++ b/libs/@local/hash-graph-sdk/python/scripts/codegen_filter.sh @@ -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" diff --git a/libs/@local/hash-graph-sdk/python/turbo.json b/libs/@local/hash-graph-sdk/python/turbo.json index 8ff639ae1db..07e2a049737 100644 --- a/libs/@local/hash-graph-sdk/python/turbo.json +++ b/libs/@local/hash-graph-sdk/python/turbo.json @@ -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*"],