Reading Time: 2 minutes
I love my Rails, but this is something I am really interested in.
It all started with a little bookkeeping app written using Phoenix as an API and React as a view layer. Has now turned into an extremely deep rabbit hole. After completing the first features of the app, I had the question “These states in these components are getting out of hand, I wonder if there’s something to help me?” This lead to Redux, one object to hold the state of the entire app. Then lead to the question, “Is there a better way to handle the data request to the API?” This lead to GraphQL which lead to Absinthe which made sense since I am using Phoenix as my API.
I think the best way to learn is to build something simple but not trivial. So let us begin with an e-commerce store that sales widgets.
Users >> Carts >> Widgets
Users can have carts to place widgets in them, etc.
I am going to break this down to 3 to 4 articles:
Part 1: Introduction to Phoenix, Absinthe, and Guardian – explaining the pieces.
Part 2: Implementation of the Phoenix/Absinthe API.
This is how you go about creating the API.
Part 3: Implementation of React/Redux with Apollo client.
We will create the frontend while still inside Phoenix, no additional steps Webpack will handle it.
Part 4: Additional nice things to have like: Elasticsearch, a Pub/Sub feature, etc. This one is a maybe.
Code can be found here.
What is Phoenix?
Phoenix is a web development framework similar in vein to Ruby on Rails. Phoenix is written in Elixir. Elixir is a functional, concurrent, and general-purpose programming language. Phoenix follows the MVC pattern and was developed to create highly performant and scalable web applications.
Laymen terms: You use it to build websites fast and efficiently.
What is GraphQL?
GraphQL was developed by Facebook in 2012 and released to the public in 2015. It is a data query and manipulation language for APIs.
Simply: An efficient and flexible way to get data, by eliminating unwanted data. I define what data I need from the endpoint.
What is Absinthe GraphQL?
Absinthe is “The GraphQL toolkit for Elixir.” Since Phoenix is written in Elixir and most APIs are web APIs. This just makes sense.
If you are familiar with Devise in the Ruby world Guardian is similar. It is an authenication library for Elixir applications.
Apollo GraphQL (Client)