Building a REST API with EdgeDB and FastAPI — Tutorials | EdgeDB Docs

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 --file to generate
    • Note that I’m using the --file option 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-py to generate a file per query might be a better choice.

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

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 Event type that extends the Auditable abstract type. It also contains some additional concrete properties and links: address, schedule, and an optional link called host that corresponds to a User.

  • In addition to User, we define an Event type that extends the Auditable abstract 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 httpx command does not copy entire command. Stops copy after the opening curly brace in the JSON.

Browse the endpoints using the native OpenAPI doc

https://www.edgedb.com/docs/guides/tutorials/rest_apis_with_fastapi#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”