Query Single Object
Query Single Object With +
:¶
If you know that your query can return either a single object or None
, you can put +
at the end of
its name and then edgeql-queries
will use the .query_single
method from driver for this query.
For example, here is the code that directly uses the python driver:
import edgedb
conn = edgedb.connect("edgedb://edgedb@localhost/edgedb")
user = conn.query_single(
"""
SELECT Person {
username
}
FILTER .ip = <str>$user_ip
LIMIT 1
""",
user_ip="127.0.0.1",
)
if user is None:
print("oops, no user was found")
else:
print("user:", user.username)
And this is the code that uses edgeql-queries
:
queries.edgeql
:
# name: select-person-by-ip+
# Query user by IP.
SELECT Person {
username
}
FILTER .ip = <str>$user_ip
LIMIT 1
And python code:
import edgedb
from edgeql_queries import from_path
conn = edgedb.connect()
queries = from_path("./queries.edgeql", async_driver=False)
user = queries.select_person_by_ip(conn, user_ip="127.0.0.1")
if user is None:
print("oops, no user was found")
else:
print("user:", user.username)