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

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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

We're in Beta

About us Mission Statement