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 ![]()
First Post!
jeffdeville
Most Liked
vic
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
I apologize for not seeing this sooner. Somehow it got past me! Pretty late but here are a few reasons why I like GraphQL.
-
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.
-
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.
-
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
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.
Popular in Discussions
Other popular topics
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









