ryanswapp

ryanswapp

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:

Showing Posts 22 to 13

ryanswapp

ryanswapp OP

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.

johninvictus

johninvictus

@ryanswapp
Why would you recommend someone to use GraphQl,
I have never used it but am wondering how could I create a login system with it etc

Ps: am a currently learning Elixir

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

piyushcoader

piyushcoader

can’t wait to see it man. and i wish i can contribute as well

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey there, this is a great question. Most of the stuff you see with GraphQL is using it to power an API. However we have an experimental project we hope to release very very soon that lets you use graphql to power totally ordinary Phoenix views.

piyushcoader

piyushcoader

great stuff man loved it. just a noob question . can we use phoenix template with graphql?

hlx

hlx

Thanks! Will test this out

ryanswapp

ryanswapp OP

Thanks! Good question… I was just thinking about this myself. I haven’t yet tried this out but I think it would work. You could do something like this:

# Another File
defmodule MyApp.Schema.Query.User do
  object :user_queries do 
    field :users, list_of(:user) do
      resolve &MyApp.UserResolver.all/2
    end

    field :user, type: :user do
      arg :id, non_null(:id)
      resolve &MyApp.UserResolver.find/2
    end
  end
end

# Schema file
defmodule MyApp.Schema do
  import_types MyApp.Schema.Query.User
  query do
    import_fields :user_queries
  end
end
hlx

hlx

Hey Ryan, great article!

I’m trying to hack together an Absinthe app (without Phoenix) and I was wondering how you would split the Schema (into multiple files) before it becomes to big.

I’m using an Electron based client, graphiql-app, because it allow me to set custom headers (e.g. for authentication)

Oxyrus

Oxyrus

Seems like by default, the apollo client makes the requests to “/graphql”, Ryan instead, set it to “/api”. that was all :smiley:

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews