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

Copy Markdown View Source

Functional builder for OData query parameters.

Builds $select, $filter, $expand, $top, $skip, $orderby, $count, and $search query parameters for Microsoft Graph API requests.

Examples

alias GraphApi.OData

query = OData.new()
  |> OData.select(["displayName", "mail", "id"])
  |> OData.filter("department eq 'Engineering'")
  |> OData.top(25)
  |> OData.orderby("displayName")

params = OData.to_params(query)
# %{"$select" => "displayName,mail,id", "$filter" => "department eq 'Engineering'", ...}

The $filter value is a raw string — Graph filter syntax is too varied for a DSL.

Summary

Functions

Sets $count to true, requesting an inline count of matching resources.

Sets $expand fields. Accepts a list of relationship names.

Sets the $filter expression.

Sets the $filter expression using a schema module and keyword conditions.

Creates a new empty OData query builder.

Sets $orderby expression.

Sets $search expression.

Sets $select fields. Accepts a list of field name strings.

Sets $skip (offset). Must be a non-negative integer.

Converts the OData query builder to a map of query parameters.

Sets $top (page size). Must be a positive integer.

Types

t()

@type t() :: %GraphApi.OData{
  count: boolean() | nil,
  expand: [String.t()] | nil,
  filter: String.t() | nil,
  orderby: String.t() | nil,
  search: String.t() | nil,
  select: [String.t()] | nil,
  skip: non_neg_integer() | nil,
  top: pos_integer() | nil
}

Functions

count(query)

@spec count(t()) :: t()

Sets $count to true, requesting an inline count of matching resources.

Examples

OData.new() |> OData.count()

expand(query, fields)

@spec expand(t(), [String.t()]) :: t()

Sets $expand fields. Accepts a list of relationship names.

Examples

OData.new() |> OData.expand(["manager", "directReports"])

filter(query, filter_builder)

@spec filter(t(), String.t() | GraphApi.OData.Filter.t()) :: t()

Sets the $filter expression.

Accepts a raw OData filter string, a Filter struct, or a schema module with keyword conditions for schema-aware filtering.

Examples

# Raw string
OData.new() |> OData.filter("startsWith(displayName, 'A')")

# Schema-aware keyword syntax (equality, AND-ed)
OData.new() |> OData.filter(User, department: "Engineering", account_enabled: true)

# Filter builder struct
filter = Filter.new(User) |> Filter.where(:display_name, :starts_with, "A")
OData.new() |> OData.filter(filter)

filter(query, schema, conditions)

@spec filter(t(), module(), keyword()) :: t()

Sets the $filter expression using a schema module and keyword conditions.

Each keyword is a snake_case field name from the schema, matched with eq. Multiple conditions are combined with and.

Examples

alias GraphApi.Schema.User

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

new()

@spec new() :: t()

Creates a new empty OData query builder.

orderby(query, expression)

@spec orderby(t(), String.t()) :: t()

Sets $orderby expression.

Examples

OData.new() |> OData.orderby("displayName desc")

search(query, expression)

@spec search(t(), String.t()) :: t()

Sets $search expression.

Examples

OData.new() |> OData.search(""displayName:John"")

select(query, fields)

@spec select(t(), [String.t()]) :: t()

Sets $select fields. Accepts a list of field name strings.

Examples

OData.new() |> OData.select(["displayName", "mail"])

skip(query, n)

@spec skip(t(), non_neg_integer()) :: t()

Sets $skip (offset). Must be a non-negative integer.

Examples

OData.new() |> OData.skip(50)

to_params(query)

@spec to_params(t()) :: map()

Converts the OData query builder to a map of query parameters.

Only includes parameters that have been set (non-nil). Lists are joined with commas. Booleans are converted to strings.

Examples

OData.new()
|> OData.select(["id", "displayName"])
|> OData.top(10)
|> OData.to_params()
#=> %{"$select" => "id,displayName", "$top" => "10"}

top(query, n)

@spec top(t(), pos_integer()) :: t()

Sets $top (page size). Must be a positive integer.

Examples

OData.new() |> OData.top(25)