Skip to content

Commit e0eae09

Browse files
committed
Update the query writing section
1 parent e4a098f commit e0eae09

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

docs/guides/tutorials/fastapi_gelai_searchbot.rst

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -615,45 +615,48 @@ Let's verify it by running:
615615
Writing queries
616616
---------------
617617
618-
.. note::
619-
add links to documentation
620-
621-
.. note::
622-
we're assuming knowledge of EdgeQL here. If a refresher is needed, add link
618+
With schema in place, it's time to focus on getting the data in and out of the
619+
database.
623620
621+
In this tutorial we're going to write queries using :ref:`EdgeQL
622+
<_ref_intro_edgeql>` and then use :ref:`codegen <_edgedb-python-codegen>` to
623+
generate typesafe function that we can plug directly into out Python code. If
624+
you are completely unfamiliar with EdgeQL, now is a good time to check out the
625+
basics before proceeding.
624626
625-
First, let's create a directory inside `app` called `queries` where we're going
626-
to put all of the EdgeQL-related stuff.
627+
Let's move on. First, create a directory inside `app` called `queries`. This is
628+
where we're going to put all of the EdgeQL-related stuff.
627629
628-
Let's start simple. We're going to write a query that fetches all of the users.
629-
In `queries` create a file named `get_users.edgeql` and put the following query
630-
in there:
630+
We're going to start by writing a query that fetches all of the users. In
631+
`queries` create a file named `get_users.edgeql` and put the following query in
632+
there:
631633
632634
.. code-block:: edgeql
633635
:caption: app/queries/get_users.edgeql
634636
635637
select User { name };
636638
637-
638639
Now run the code generator from the shell:
639640
640641
.. code-block:: bash
641642
$ gel-py
642643
643644
It's going to automatically locate the `.edgeql` file and generate types for it.
644-
Once that is done, let's use those types to create the endpoint in ``main.py``:
645+
We can inspect generated code in `app.queries/get_users_async_edgeql.py`. Once
646+
that is done, let's use those types to create the endpoint in `main.py`:
645647
646648
.. code-block:: python
647649
from edgedb import create_async_client
648650
from .queries.get_users_async_edgeql import get_users as get_users_query, GetUsersResult
651+
652+
649653
gel_client = create_async_client()
650654
651655
@app.get("/users")
652656
async def get_users() -> list[GetUsersResult]:
653657
return await get_users_query(gel_client)
654658
655-
With that, we've added our first CRUD endpoint! Let's verify it works as
656-
expected:
659+
Let's verify it that works as expected:
657660
658661
.. code-block:: bash
659662
$ curl -X 'GET' \
@@ -682,17 +685,19 @@ username. In order to do that, we need to write a new query in a separate file
682685
select User { name }
683686
filter .name = <str>$name;
684687
685-
After that, we will run the code generator again by calling `gel-py`.
686-
In the app, we are going to reuse the same endpoint that fetches the list of all
687-
users. From now on, if the user calls it without any arguments (e.g.
688+
After that, we will run the code generator again by calling `gel-py`. In the
689+
app, we are going to reuse the same endpoint that fetches the list of all users.
690+
From now on, if the user calls it without any arguments (e.g.
688691
`http://127.0.0.1/users`), they are going to receive the list of all users, same
689692
as before. But if they pass a username as a query argument like this:
690693
`http://127.0.0.1/users?username=bob`, the system will attempt to fetch a user
691694
named `bob`.
692695
693696
In order to achieve this, we're going to need to add a `Query`-type argument to
694-
our endpoint function. It's default value is going to be `None`, which will
695-
enable us to implement out conditional logic:
697+
our endpoint function. You can learn more about how to configure this type of
698+
arguments in `FastAPI's docs
699+
<https://fastapi.tiangolo.com/tutorial/query-params/>`_. It's default value is
700+
going to be `None`, which will enable us to implement our conditional logic:
696701
697702
.. code-block:: python
698703
:caption: app/main.py
@@ -748,11 +753,13 @@ it and run code generation.
748753
name
749754
}
750755
751-
.. note::
752-
trickery with the insert wrapped in select
756+
Note that in this query we've wrapped the `insert` in a `select` statement. This
757+
is a common pattern in EdgeQL, that can be used whenever you would like to get
758+
something other than object ID when you just inserted it.
753759
754-
For this, we're going to add a new endpoint. Note that this one has the same
755-
name `/users`, but is for the POST HTTP method.
760+
In order to integrate this query into our app, we're going to add a new
761+
endpoint. Note that this one has the same name `/users`, but is for the POST
762+
HTTP method.
756763
757764
.. code-block:: python
758765
from gel import ConstraintViolationError
@@ -1082,20 +1089,6 @@ In reality this workflow would've been handled by the frontend, providing the
10821089
user with a nice inteface to interact with. But even without one we're built a
10831090
fully functional chatbot already!
10841091
1085-
.. note::
1086-
Describe how the post message kind of inherits the search functionality
1087-
1088-
.. note::
1089-
Modify the generate too so it's history aware.
1090-
1091-
.. note::
1092-
Add a fold of some kind to streamline the text
1093-
1094-
.. note::
1095-
Explain that for each query we want to create a separate file inside the
1096-
folder, and that we're doing it to use codegen. Explain what problem codegen
1097-
is supposed to solve for us.
1098-
10991092
11001093
Generating a Google search query
11011094
--------------------------------

0 commit comments

Comments
 (0)