Improve Typing Of Queries
Since Queries are dynamically generated at runtime, they cannot provide type hints for your editors/IDEs or linters. But you can write typings for your queries yourself.
queries.edgeql
:
# name: select-users-by-last-name
# Select all users that have same last name.
SELECT Person {
first_name,
last_name,
}
FILTER .last_name = <str>$last_name
# name: select-user-by-id!
# Select single user's last name by it's id.
SELECT Person {
first_name,
last_name
}
FILTER .id = <uuid>$user_id
# name: create-keanu-reeves*
# Create new user.
INSERT Person {
first_name := "Keanu",
last_name := "Reeves",
}
And python code:
from typing import Protocol, Set, Union, cast
from uuid import UUID
import edgedb
import edgeql_queries
class Person(Protocol):
id: UUID
first_name: str
last_name: str
class Queries:
async def select_users_by_last_name(
self,
executor: edgedb.AsyncIOExecutor,
last_name: str,
) -> Set[Person]:
...
async def select_user_by_id(
self,
executor: edgedb.AsyncIOExecutor,
user_id: UUID,
) -> Person:
...
async def create_keanu_reeves(self, executor: edgedb.AsyncIOExecutor) -> None:
...
queries = cast(Queries, edgeql_queries.from_path("./queries.edgeql"))