ryanswapp

ryanswapp

Phoenix GraphQL Tutorial with Absinthe

I wrote up a little tutorial on setting up a GraphQL server in Phoenix with Absinthe. Here is a link https://ryanswapp.com/2016/11/29/phoenix-graphql-tutorial-with-absinthe/

This is a starting point that I plan to build on. I’ll hopefully have a post up explaining how I’ve done authentication with Absinthe and maybe move into channels and the frontend. Absinthe is great! If you haven’t given it a shot I recommend checking it out :slight_smile:

First Post!

jeffdeville

jeffdeville

Thanks Ryan! Added to my reading list.

Most Liked

vic

vic

Asdf Core Team

For those interested on using Absinthe with Apollo client via Phoenix channels, I just released a custom Apollo networkInterface just for that: apollo-phoenix-websocket

ryanswapp

ryanswapp

I apologize for not seeing this sooner. Somehow it got past me! Pretty late but here are a few reasons why I like GraphQL.

  1. One HTTP Request - GraphQL makes only one HTTP request. Using another pattern like REST you would generally have to make a number of HTTP requests to get data from different resources. With GraphQL, you send one query to the server and it sends back all the data you need without having to make multiple requests.

  2. Type Checking - In GraphQL you define the fields and types that can be queried or that can be input into a mutation. As a result, you get data validation out of the box. You don’t have to do any type checking on mutation arguments because GraphQL already does it for you.

  3. Querying Nested Data - Let’s say you have an app that lawyers use to manage their cases. Each lawyer can have many cases. The case has a foreign key property called lawyer_id. With GraphQL, I can set up a lawyer field that takes the lawyer_id on a case and requests the lawyer’s data. So, I could query for a case like this:

{
  case {
    lawyer {
      first_name,
      last_name
    }
}

This comes in really handy with complex schemas.

There are a quite a few more benefits but I think the best way to identify them is to build something. Let me know if you have any questions.

ryanswapp

ryanswapp

GraphQL also returns a json response. You will likely want to use a client library to send the GraphQL requests though (similar to how you’d use a HTTP client library). There are a few libraries out there at this point to help make sending requests easier (although it’s easy by itself since you just send a string representing your query). You could use Lokka, Apollo, and others. Lokka is pretty basic and is useful for sending queries. Apollo is more full featured and does caching, etc… Just search for GraphQL clients in whatever frontend tech you use and there’s likely to be quite a few.

Where Next?

Popular in Discussions Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19814 150
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14403 124
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement