26
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ElixirAdvent Calendar 2022

Day 8

Elixir × GraphQLシリーズ① ~ サクッとGraphQLサーバーのセットアップ ~

Last updated at Posted at 2022-12-07

Elixir × GraphQLシリーズ

~ Abshinthe Setup ~

この記事は「Elixir Advent Calendar 2022」8日目の記事です
東京にいるけどFukuokaexのYOSUKEです。

Graphqlのセットアップは以前も書いた事があるので、今回は 簡単に手順のみをシンプルにサクッと書いていこう。というコンセプトで作っていきます。

このシリーズは3部作で考えています。

mix phx.new open_data_ql
mix phx.gen.context Community Place places name:string location:string lat:float lon:float scale:integer

実行

mix ecto.create
mix ecto.migrate

依存関係の追加

mix.exs
defp deps do
    [
    # 省略 #
      {:absinthe, "~> 1.7"},
      {:absinthe_plug, "~> 1.5"},
      {:absinthe_phoenix, "~> 2.0"}
    ]
end

実行

mix deps.get

サンプルデータを準備する

seeds.exs
alias OpenDataQl.Repo
alias OpenDataQl.Community.Place

%Place{
  name: "test1",
  location: "test1",
  lat: 123.45,
  lon: 67.89,
  scale: 1
} |> Repo.insert!

%Place{
  name: "test2",
  location: "test2",
  lat: 223.45,
  lon: 67.89,
  scale: 2
} |> Repo.insert!

%Place{
  name: "test3",
  location: "test3",
  lat: 323.45,
  lon: 67.89,
  scale: 3
} |> Repo.insert!

スキーマの作成

schema/schema.ex

defmodule OpenDataQlWeb.Schema.Schema do
  use Absinthe.Schema

  object :place do
    field :id, non_null(:id)
    field :name, non_null(:string)
    field :location, non_null(:string)
    field :lat, non_null(:float)
    field :lon, non_null(:float)
    field :scale, non_null(:integer)
  end

  query do
    @desc "Get a list of places"
    field :places, list_of(:place) do
      resolve &OpenDataQlWeb.Resolvers.Community.places/3
    end

    @desc "Get a place by its id"
    field :place, :place do
      arg :id, non_null(:id)
      resolve &OpenDataQlWeb.Resolvers.Community.place/3
    end
  end

end

リゾルバの作成

resolvers/community.ex
defmodule OpenDataQlWeb.Resolvers.Community do
  alias OpenDataQl.Community

  def places(_, _, _) do
    {:ok, Community.list_places()}
  end

  def place(_, %{id: id}, _) do
    {:ok, Community.get_place!(id)}
  end
end

routerに追記

router.ex
defmodule OpenDataQlWeb.Router do
  use OpenDataQlWeb, :router

# 省略 #

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/api" do
    pipe_through :api

    forward "/", Absinthe.Plug,
      schema: OpenDataQlWeb.Schema.Schema
  end

# 省略 #

  if Mix.env() == :dev do
    forward "/graphiql", Absinthe.Plug.GraphiQL,
      schema: OpenDataQlWeb.Schema.Schema,
      interface: :simple
  end
end

起動

iex -S mix phx.server

アクセス

http://localhost:4000/graphiql

スクリーンショット 2022-12-04 14.30.17.png

さて、ここまで完成したので、次はクライアントもElixirで作っていきましょう。

26
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?