Commit 715df32d authored by Catalina Astengo's avatar Catalina Astengo 💯

absinthe setup

parent 5f9ca8b8
......@@ -2,17 +2,53 @@
To start your Phoenix server:
* Install elixir 1.10.1
* Install dependencies with `mix deps.get`
* Start Phoenix endpoint with `mix phx.server`
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html).
## Learn more
## Setup
* Official website: https://www.phoenixframework.org/
* Guides: https://hexdocs.pm/phoenix/overview.html
* Docs: https://hexdocs.pm/phoenix
* Forum: https://elixirforum.com/c/phoenix-forum
* Source: https://github.com/phoenixframework/phoenix
We are going to use Absinthe to implement graphql in our project.
Here are detailed installation instructions: https://hexdocs.pm/absinthe/plug-phoenix.html#content
We add absinthe and absinthe_plug to our deps
```
def deps do
[
{:absinthe, "~> 1.4.0"},
{:absinthe_plug, "~> 1.4"},
]
end
```
Run `mix deps.get` to fetch dependencies.
We already have `Jason` as our json library so we just configure that in the config file and our schema (that we haven't written yet).
```
config :absinthe,
json_codec: Jason,
schema: PokemonWeb.Schema
```
Add `Absinthe.Plug.Parser` to parsers in case we don't only receive `application/graphql` requests.
```
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
```
Forward graphql requests to corresponding plugs.
```
forward "/graphiql",
Absinthe.Plug.GraphiQL,
schema: PokemonWeb.Schema,
interface: :simple
forward "/", Absinthe.Plug, schema: PokemonWeb.Schema
```
\ No newline at end of file
......@@ -22,6 +22,10 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
config :absinthe,
json_codec: Jason,
schema: PokemonWeb.Schema
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
......@@ -34,7 +34,7 @@ defmodule PokemonWeb.Endpoint do
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
......
defmodule PokemonWeb.Schema do
use Absinthe.Schema
object :pokemon do
field(:name, non_null(:string))
end
query do
@desc "Returns info about given Pokémon"
field :get_pokemon, :pokemon do
@desc "Name of the Pokémon"
arg(:name, non_null(:string))
resolve(fn %{name: name}, _ ->
{:ok, %{name: name}}
end)
end
end
end
......@@ -5,7 +5,10 @@ defmodule PokemonWeb.Router do
plug :accepts, ["json"]
end
scope "/api", PokemonWeb do
pipe_through :api
end
forward "/graphiql",
Absinthe.Plug.GraphiQL,
schema: PokemonWeb.Schema,
interface: :simple
forward "/", Absinthe.Plug, schema: PokemonWeb.Schema
end
......@@ -35,7 +35,9 @@ defmodule Pokemon.MixProject do
{:phoenix, "~> 1.4.12"},
{:phoenix_pubsub, "~> 1.1"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"}
{:plug_cowboy, "~> 2.0"},
{:absinthe, "~> 1.4.0"},
{:absinthe_plug, "~> 1.4"}
]
end
end
%{
"absinthe": {:hex, :absinthe, "1.4.16", "0933e4d9f12652b12115d5709c0293a1bf78a22578032e9ad0dad4efee6b9eb1", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "076b8bd9552f4966ba1242f412f6c439b731169a36a0ddaaffcd3893828f5bf6"},
"absinthe_plug": {:hex, :absinthe_plug, "1.4.7", "939b6b9e1c7abc6b399a5b49faa690a1fbb55b195c670aa35783b14b08ccec7a", [:mix], [{:absinthe, "~> 1.4.11", [hex: :absinthe, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.2 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c6ecb0e56a963287ac252d0563e5b33b84b300ce8203d3d1410dddb5dc6d08e9"},
"cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"},
"cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment