Something to help you consume that sweet, sweet absinthe.
A GraphQL client library that formats and sends queries and mutations to your GraphQL server while also validating those queries and mutations against a schema at compile time. (documentation)
The package can be installed by adding goblet to your list of dependencies in mix.exs:
defdepsdo[{:goblet,"~> 0.1.4"}]endOptionally, add import_deps: [:goblet] to your .formatter.exs. This stops parentheses from being auto-injected around fragments.
defmoduleMyGobletdouseGoblet,from: "./path/to/schema.json"defprocess(query,_ctx)doHTTPoison.post!("https://api.myserver.example/graphql",Jason.encode!(query),[{"Content-Type","application/json"},{"Authorization","Bearer TOKEN_GOES_HERE"}])|>Map.get(:body)|>Jason.decode!()|>Map.get("data")endenddefmoduleQueriesdouseMyGobletquery"FetchUser"do# Supports variable pinning with ^user(id: ^id)doidnamefriends(first: 3)doidnameprofilePic# inline fragments work too...on: "BestFriend"dosecretHandshakeendend# Alias fields using the @as directive@as"moreFriends"friends(first: 15)doidnameendendendend# Invoke queries by their elixir-ized names: FetchUser -> fetch_userQueries.fetch_user(%{id: "abc"})# %{# "operationName" => "FetchUser",# "query" =># "query FetchUser($id: ID!) {user(id: $id) {id name friends(first: 3){id name profilePic} moreFriends:friends(first:15){id name}}}",# "variables" => %{id: "abc"}# }Queries.fetch_user(%{id: "abc"},ctx)# formats the query like above and then passes it, along with ctx, into your process functionTo validate your queries, Goblet needs to be passed a schema.json file which represents the GraphQL schema of the server you are making requests against. You can fetch your schema by sending the query generated by Goblet.Introspection.query to your server. Make sure that the json file you end up with has a __schema key at the root. Here's an example of how one might use it:
query=Goblet.Introspection.query()schema=HTTPoison.post!("https://api.myserver.example/graphql",Jason.encode!(query),[{"Content-Type","application/json"},{"Authorization","Bearer TOKEN_GOES_HERE"}])|>Map.get(:body)|>Jason.decode!()|>Map.get("data")|>Jason.encode!()File.write!("./schema.json",schema)