GraphQL client for Elixir.
Add to dependencies in your mix.exs
file...
def deps do
[{:graphito, "~> 0.2.0"}]
end
...and run:
mix deps.get
You will have to configure a url for the GraphQL server.
config :graphito,
url: "a_graphql_host"
Additionally, headers can be configured and they will be sent in all requests.
config :graphito,
headers: [{"this_header", "is_always_to_be_send"}]
Run any GraphQL operation (a query or mutation):
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:
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:
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:
iex> Graphito.run("""
query {
jedis {
lightzaber
}
}
""")
%Graphito.Response.Error{reason: :timeout, errors: [%{"message" => "Failed to fetch GraphQL response"}], headers: []}