Grouping Queries

If you pass a single file or directory without nesting in the from_path function, then your EdgeQL queries will be placed directly in the returned queries as attributes.

For example, if there is a single file with queries named queries.edgeql:

# name: create-new-person!
# Create new person with passing first and lst names.
INSERT Person {
    first_name := <str>$first_name,
    last_name := <str>$last_name,
}

# name: select-movies-by-year
# Get movies by release year.
SELECT Movie {
    title,
    year,
    director: {
        last_name,
    },
    actors: {
        first_name,
        last_name,
    }
}
FILTER .year = <int64>$year

or a directory named edgeql with 2 edgeql/persons.edgeql and edgeql/movies.edgeql files:

edgeql/persons.edgeql:

# name: create-new-person!
# Create new person with passing first and lst names.
INSERT Person {
    first_name := <str>$first_name,
    last_name := <str>$last_name,
}

edgeql/movies.edgeql

# name: select-movies-by-year
# Get movies by release year.
SELECT Movie {
    title,
    year,
    director: {
        last_name,
    },
    actors: {
        first_name,
        last_name,
    }
}
FILTER .year = <int64>$year

Then working python code will look like this:

import edgedb
import edgeql_queries

# or edgeql_queries.from_path('./edgeql', async_driver=False) for directory
queries = edgeql_queries.from_path("./queries.edgeql", async_driver=False)

conn = edgedb.connect()

queries.create_new_person(conn, first_name="Keanu", last_name="Reeves")

But if you pass a directory with subdirectories that contain .edgeql files, then these queries will be placed as subqueries in the returned queries.

For example, if there is the following directory tree:

edgeql/
├── persons
│   └── persons.edgeql
└── movies
    └── movies.edgeql

Then edgeql-queries will generate 2 subgroups for the returned queries, and the working code will look like this:

import edgedb
import edgeql_queries

queries = edgeql_queries.from_path("./edgeql", async_driver=False)

conn = edgedb.connect()

movies = queries.movies.select_movies_by_year(conn, year=2020)

print(movies)