Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qgadrian/graphito
GraphQL client for Elixir
https://github.com/qgadrian/graphito
elixir elixir-library graphql graphql-client hex
Last synced: about 1 month ago
JSON representation
GraphQL client for Elixir
- Host: GitHub
- URL: https://github.com/qgadrian/graphito
- Owner: qgadrian
- License: mit
- Created: 2018-05-07T17:59:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-20T09:46:21.000Z (about 6 years ago)
- Last Synced: 2024-10-01T04:40:58.026Z (about 2 months ago)
- Topics: elixir, elixir-library, graphql, graphql-client, hex
- Language: Elixir
- Size: 51.8 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Coverage Status](https://coveralls.io/repos/github/qgadrian/graphito/badge.svg?branch=master)](https://coveralls.io/github/qgadrian/graphito?branch=master)
[![Hex version](https://img.shields.io/hexpm/v/sippet.svg "Hex version")](https://hex.pm/packages/graphito)
[![Hex Docs](https://img.shields.io/badge/hex-docs-9768d1.svg)](https://hexdocs.pm/graphito)
[![Build Status](https://travis-ci.org/qgadrian/graphito.svg?branch=master)](https://travis-ci.org/qgadrian/graphito.svg?branch=master)
[![Deps Status](https://beta.hexfaktor.org/badge/all/github/qgadrian/graphito.svg)](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.2.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"}]}
```A query response can be mapper to a given struct:
```elixir
iex> Graphito.run("""
query {
jedis {
name
friends {
name
}
}
}
""", as: %Jedi{friends: %Jedi{}})%Graphito.Response{data: [%Jedi{name: "Luke", friends: [%Jedi{name: "Yoda"}]}, %Jedi{name: "Leia", friends: [%Jedi{name: "Hans"}]}]}, status: 200, errors: nil, headers: [{"content-type", "application/json"}]}
```If an operation fails the errors are parsed and 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"}]}
iex> Graphito.run("""
query {
jedis {
lightsaber
}
}
""")%Graphito.Response{data: nil, status: 200, errors: [%{"message" => "Third party server timeout", "code" => 503}], 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: []}
```