[](https://coveralls.io/github/qgadrian/graphito?branch=master)
[](https://hex.pm/packages/graphito)
[](https://hexdocs.pm/graphito)
[](https://travis-ci.org/qgadrian/graphito.svg?branch=master)
[](https://beta.hexfaktor.org/github/qgadrian/graphito)
# Graphito
[GraphQL](https://graphql.org/) client for Elixir.
## Table of Contents
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
## Installation
Add to dependencies in your `mix.exs` file...
```elixir
def deps do
[{:graphito, "~> 0.1.0"}]
end
```
...and run:
```bash
mix deps.get
```
## Configuration
You will have to configure a url for the GraphQL server.
```elixir
config :graphito,
url: "a_graphql_host"
```
Additionally, headers can be configured and they will be sent in all requests.
```elixir
config :graphito,
headers: [{"this_header", "is_always_to_be_send"}]
```
## Usage
Run any GraphQL operation (a query or mutation):
```elixir
iex> Graphito.run("""
query {
jedis {
name
}
}
""")
%Graphito.Response{data: %{"jedis" => [%{"name" => "luke"}]}, status: 200, errors: nil, headers: [{"content-type", "application/json"}]}
```
If an operation fails the errors are returned:
```elixir
iex> Graphito.run("""
query {
jedis {
lightzaber
}
}
""")
%Graphito.Response{data: nil, status: 200, errors: [%{"message" => "Cannot query field \"lightzaber\" on type \"Jedi\". Did you mean \"lightsaber\"?"}], headers: [{"content-type", "application/json"}]}
```
If something fails an error is returned:
```elixir
iex> Graphito.run("""
query {
jedis {
lightzaber
}
}
""")
%Graphito.Response.Error{reason: :timeout, errors: [%{"message" => "Failed to fetch GraphQL response"}], headers: []}
```