Building a REST API with EdgeDB and FastAPI — Tutorials | EdgeDB Docs
- Example code is at edgedb-examples/fastapi-crud at main · edgedb/edgedb-examples · GitHub
- Will also need updating
Implementing Codegen
- Circle back to example code in the examples repo, generating the code to replace the hard-coded queries
- When introducing endpoints in the tutorial, start with the queries replaced by pseudo-code comments to indicate where generated code will be called
edgedb-py --fileto generate- Note that I’m using the
--fileoption to generate a single file from all the queries for convenience in what’s a relatively simple app - For a more complex app with many queries in different directories, just
edgedb-pyto generate a file per query might be a better choice.
- Note that I’m using the
Connect to the database
https://www.edgedb.com/docs/guides/tutorials/rest_apis_with_fastapi#connect-to-the-database
However, the database is currently schemaless.
- We just applied a schema to it via
edgedb project init- Maybe instead, I can show how to inspect the schema via
\ds
- Maybe instead, I can show how to inspect the schema via
Schema design
https://www.edgedb.com/docs/guides/tutorials/rest_apis_with_fastapi#schema-design
Along with the inherited type, the user type also defines a concrete required property called
name.
- Maybe “Alongside the inherited property, …”
We impose two constraints on this property: names should be unique and shorter than 50 characters.
- We impose two constraints on this property: names should be unique (
constraint exclusive) and shorter than 50 characters (constraint max_len_value(50).
We also define an
Eventtype that extends theAuditableabstract type. It also contains some additional concrete properties and links:address,schedule, and an optional link calledhostthat corresponds to aUser.
- In addition to
User, we define anEventtype that extends theAuditableabstract type with a few concrete properties and links: …
User APIs
https://www.edgedb.com/docs/guides/tutorials/rest_apis_with_fastapi#user-apis
- POST endpoint code tries to unpack return value from query, but that no longer works with the generated code. Generated query returns a single value that does not need to be unpacked.
- Many endpoints that could only operate on a single user are specced to return iterables. Changed them to return single values
- GET to users will now return:
- an array if no name is specified
- a single response if a name of an existing user is specified
- a 404 with an error if a named that doesn’t exist is specified
Event APIs
https://www.edgedb.com/docs/guides/tutorials/rest_apis_with_fastapi#event-apis
- Same changes as the User APIs
- Create event
httpxcommand does not copy entire command. Stops copy after the opening curly brace in the JSON.
Browse the endpoints using the native OpenAPI doc
- Add copy/pasteable request bodies for PUT events test via interactive docs
- Better frame screenshots to show “the good stuff”