# `GraphApi.Batch`
[🔗](https://github.com/keenmate/microsoft_graph/blob/v1.0.0-rc.1/lib/graph_api/batch.ex#L1)

JSON batch requests for Microsoft Graph API.

Allows sending up to 20 requests in a single HTTP call via `POST /$batch`.
Build batch requests using `_query` functions from resource modules.

## Examples

    alias GraphApi.{Batch, OData, Users, Groups}
    alias GraphApi.Schema.{User, Group}

    query = OData.new() |> OData.select(["id", "displayName"]) |> OData.top(5)

    {:ok, responses} =
      Batch.new()
      |> Batch.add("1", Users.list_query(query: query, as: User))
      |> Batch.add("2", Groups.get_query("group-id", as: Group))
      |> Batch.add("3", Users.create_query(%{"displayName" => "New"}, as: User))
      |> Batch.execute(client: client)

    %{status: 200, body: %{"value" => users}} = Batch.get(responses, "1")

# `t`

```elixir
@type t() :: %GraphApi.Batch{entries: [GraphApi.Batch.Entry.t()]}
```

# `add`

```elixir
@spec add(t(), String.t(), GraphApi.Batch.Request.t(), keyword()) :: t()
```

Adds a request to the batch with the given ID.

## Options

* `:depends_on` - List of request IDs that must complete before this one.

## Examples

    Batch.new()
    |> Batch.add("1", Users.list_query(as: User))
    |> Batch.add("2", Users.create_query(attrs, as: User), depends_on: ["1"])

# `execute`

```elixir
@spec execute(
  t(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}
```

Executes the batch, sending all requests in a single `POST /$batch` call.

Returns `{:ok, [response]}` where each response has `:id`, `:status`, `:headers`, and `:body`.
Response bodies are automatically cast using the `:as` schema specified on each request.

## Options

Same as other resource functions: `:client`, `:access_token`, `:api_version`

# `get`

```elixir
@spec get([map()], String.t()) :: map() | nil
```

Finds a response by its request ID.

## Examples

    {:ok, responses} = Batch.execute(batch, client: client)
    %{status: 200, body: user} = Batch.get(responses, "1")

# `new`

```elixir
@spec new() :: t()
```

Creates a new empty batch.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
