Skip to content

Parsing

Definition for parsing functions for queries.

Queries that are acceptable by this library should follow some rules:

  1. They should be named.
  2. Their names should not contain symbols that could not be used in Python identifier(except for - symbol since it would be converted into _)
  3. They can have special symbols after their names that will change how this queries will be executed:
    • *: query will be executed as script with using .execute method from driver.
    • +: query will return a single object or None and executed with .query_single method from driver.
    • !: query will always return a single object and executed with .query_required_single method from driver.
    • empty: common query that will return a set of objects and will be executed with .query method from driver.

An example query that can be successfuly parsed:

    # name: select-user-by-username!
    # Find user by username and return it.
    SELECT User {
        username,
        bio,
        is_active
    }
    FILTER .username = <str>$username
    LIMIT 1

get_query_name_and_operation(name)

Return query name and operation from query headers.

Parameters:

Name Type Description Default
name str

raw query name with operator from which final name and operation should be extracted.

required

Returns:

Type Description
Tuple[str, edgeql_queries.models.EdgeQLOperationType]

Query name and operation type

Exceptions:

Type Description
EdgeQLParsingError

if header is in wrong format or name could not be converted into Python identificator.

Source code in edgeql_queries/parsing.py
def get_query_name_and_operation(name: str) -> Tuple[str, EdgeQLOperationType]:
    """Return query name and operation from query headers.

    Arguments:
        name: raw query name with operator from which
            final name and operation should be extracted.

    Returns:
        Query name and [operation type][edgeql_queries.models.EdgeQLOperationType]

    Raises:
        EdgeQLParsingError: if header is in wrong format or name could not be
            converted into Python identificator.
    """
    name = name.replace("-", "_")

    operation_suffix = ""
    for suffix in _OPERATION_SUFFFIXES_TO_TYPES:  # pragma: no branch
        if name.endswith(suffix):
            operation_suffix = suffix
            break

    if operation_suffix:
        query_name = name[: -len(operation_suffix)]
    else:
        query_name = name

    if not VALID_QUERY_NAME_PATTERN.match(query_name):
        raise EdgeQLParsingError(
            'name must be convertable to valid python variable, got "{0}"'.format(
                query_name,
            ),
        )

    return (
        query_name,
        _OPERATION_SUFFFIXES_TO_TYPES[operation_suffix],
    )

parse_query_from_string(raw_name, query_body)

Parse EdgeQL query string into [edgeql_queries.models.Query].

Parameters:

Name Type Description Default
raw_name str

query name with operation.

required
query_body str

EdgeQL query.

required

Returns:

Type Description
Query

[edgeql_queries.models.Query] that will be later added for creating executors.

Source code in edgeql_queries/parsing.py
def parse_query_from_string(raw_name: str, query_body: str) -> Query:
    """Parse EdgeQL query string into [edgeql_queries.models.Query].

    Arguments:
        raw_name: query name with operation.
        query_body: EdgeQL query.

    Returns:
        [edgeql_queries.models.Query] that will be later added for creating executors.
    """
    query_name, operation_type = get_query_name_and_operation(raw_name)

    return Query(query_name, operation_type, query_body)