Elixir, Phoenix, and GraphQL(Absinthe)
source link: https://dannypsnl.github.io/blog/2021/07/06/cs/elixir-phoenix-graphql/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Elixir, Phoenix, and GraphQL(Absinthe)
Long time ago, I play Elixir and write down an article about Phoenix. Time has passed, now I'm really working with Elixir XD. Today, I want to show how to set up a GraphQL server with Elixir/Phoenix stack, we will need a library called Absinthe. This article requires you already to know how to create Phoenix project, now, let's start.
After you have a Phoenix project, we need to add dependencies in mix.exs
first:
defp deps do
[
# ...
# GraphQL
{:absinthe, "~> 1.4"},
{:absinthe_plug, "~> 1.4"}
]
end
Then run mix deps.get
. Before we create GraphQL schema, we need some data to interact with, you can run mix phx.gen.context Log Events events event_type:string message:string payload:text
to get a model then run mix ecto.migrate
to set up Database.
Query
Imagine the schema:
{
events {
id
event_type
message
payload
inserted_at
updated_at
}
}
We will create lib/xxx_web/schema/event.ex
with the following content:
defmodule XxxWeb.Schema.DataTypes do
use Absinthe.Schema.Notation
object :event do
field :id, :id
field :event_type, :string
field :message, :string
field :payload, :string
end
end
then create lib/xxx_web/schema.ex
with the following content:
defmodule XxxWeb.Schema do
use Absinthe.Schema
import_types XxxWeb.Schema.DataTypes
query do
@desc "Get a list of events"
field :events, list_of(:event) do
resolve fn _parent, _args, _resolution ->
{:ok, Xxx.Log.list_events()}
end
end
end
end
In the project xxx
, there will have a file lib/xxx_web/router.ex
, now we have to modify it, delete the following code:
scope "/" do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
then add the following:
scope "/api" do
pipe_through :api
forward "/graphiql", Absinthe.Plug.GraphiQL, schema: XxxWeb.Schema
end
Finally, we can test it out, run iex mix -S phx.server
to start the server. Now, connect to http://localhost:4000/api/graphiql
with the browser, it will show a GraphQL playground you can play with it.
Mutation
We would like to create some data, how to do this? We still modify lib/xxx_web/schema.ex
defmodule XxxWeb.Schema do
# ...
mutation do
@desc "Create a new event"
field :create_event, :string do
arg(:event_type, non_null(:string))
arg(:message, :string)
arg(:payload, :string)
resolve fn _parent, params, _resolution ->
case Xxx.Log.create_event(params) do
{:ok, _} -> {:ok, "event created"}
{:error, changeset} -> {:error, inspect(changeset.errors)}
end
end
end
end
end
Now you can use the following mutation to create an event:
mutation {
createEvent(event_type: "Test", message: "Hello")
}
Summary
Now we know how to create queries and mutation with Absinthe, we don't cover how to add middleware, how to write unit test for our code. You will be able to get more information about those uncovered in document of Absinthe, have a nice day.
Recommend
-
134
Absinthe GraphQL implementation for Elixir. Goals: Complete implementation of the GraphQL Work...
-
53
There are many team messaging apps available in the market, but Slack is not only preferred for its simplicity but also for a wide variety of Slack apps. Slack apps make our lives a lot easier if designed carefully....
-
43
If you’re anything like me, at least 3 or 4 of the keywords in the title of this article fall under the category of “things I’ve been wanting to play with but haven’t gotten around to it yet.” React is the exception; I u...
-
21
Here are some notes on WebSockets-based Absinthe GraphQL subscriptions. A basic setup and familiarity with regular queries and mutations is assumed. Before we write our first subscription we need to ensure that th...
-
7
“I absolutely love AppSignal.” Discover AppSignal With a special thanks to Aleksandar and
-
7
“I absolutely love AppSignal.” Discover AppSignal Great news for all the Elixir alchemists, we’ve just released
-
14
How Absinthe Uses Compilation Callbacks for Schema Validation in Elixir Devon Estes on Jan 19, 2021 “I absolutely love AppSignal.” Disc...
-
8
Using a Different System?
-
16
And Now For Starting Again with Elixir and Phoenix Not Elm Apr 1, 2022 In my last blog post, I dug into Elm and while I like a number of things conceptually about Elm, t...
-
1
In our introduction to Absinthe, we covered the basics of Absinthe and GraphQL for an Elixir application. Now we'll dig deeper and see how we can customize Absinthe for larger-scale Elixir applications. Let's get going!
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK