GraphApi.OData.Filter (GraphApi v1.0.0-rc.1)

Copy Markdown View Source

Schema-aware OData filter builder.

Translates snake_case Elixir field names to camelCase API field names using schema module field mappings, and builds valid OData $filter expressions.

Simple keyword syntax

For equality conditions combined with and:

OData.new()
|> OData.filter(User, department: "Engineering", account_enabled: true)
# => $filter=department eq 'Engineering' and accountEnabled eq true

Builder syntax

For complex filters with different operators:

alias GraphApi.OData.Filter

filter =
  Filter.new(User)
  |> Filter.where(:display_name, :starts_with, "A")
  |> Filter.where(:account_enabled, :eq, true)
  |> Filter.or_where(:department, :eq, "Sales")

OData.new() |> OData.filter(filter)

Supported operators

  • :eq, :ne — equality / inequality
  • :gt, :lt, :ge, :le — comparison
  • :starts_with, :ends_with, :contains — string functions
  • :in — collection membership
  • :is_nil — null check (use true for eq null, false for ne null)

Summary

Functions

Builds a filter string from simple keyword conditions (all AND-ed with eq).

Creates a new filter builder for the given schema module.

Adds an OR condition to the filter.

Converts the filter builder to an OData filter string.

Adds an AND condition to the filter.

Types

operator()

@type operator() ::
  :eq
  | :ne
  | :gt
  | :lt
  | :ge
  | :le
  | :starts_with
  | :ends_with
  | :contains
  | :in
  | :is_nil

t()

@type t() :: %GraphApi.OData.Filter{
  clauses: [{:and | :or, atom(), operator(), term()}],
  schema: module()
}

Functions

from_keywords(schema, conditions)

@spec from_keywords(
  module(),
  keyword()
) :: String.t()

Builds a filter string from simple keyword conditions (all AND-ed with eq).

Used by OData.filter/3. Each key is a snake_case field name, each value is compared with eq.

Examples

Filter.from_keywords(User, department: "Engineering", account_enabled: true)
#=> "department eq 'Engineering' and accountEnabled eq true"

new(schema)

@spec new(module()) :: t()

Creates a new filter builder for the given schema module.

or_where(filter, field, op, value)

@spec or_where(t(), atom(), operator(), term()) :: t()

Adds an OR condition to the filter.

Examples

Filter.new(User)
|> Filter.where(:department, :eq, "Engineering")
|> Filter.or_where(:department, :eq, "Sales")

to_string(filter)

@spec to_string(t()) :: String.t()

Converts the filter builder to an OData filter string.

Examples

Filter.new(User)
|> Filter.where(:display_name, :starts_with, "A")
|> Filter.where(:account_enabled, :eq, true)
|> Filter.to_string()
#=> "startsWith(displayName,'A') and accountEnabled eq true"

where(filter, field, op, value)

@spec where(t(), atom(), operator(), term()) :: t()

Adds an AND condition to the filter.

Examples

Filter.new(User)
|> Filter.where(:display_name, :eq, "Alice")
|> Filter.where(:account_enabled, :eq, true)