Created
April 30, 2017 16:58
-
-
Save PawkaHub/d8f0ab37b4f8459c0983156c1d34c607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HelloGraphql.Mixfile do | |
use Mix.Project | |
def project do | |
[app: :hello_graphql, | |
version: "0.1.0", | |
elixir: "~> 1.4", | |
build_embedded: Mix.env == :prod, | |
start_permanent: Mix.env == :prod, | |
deps: deps()] | |
end | |
# Configuration for the OTP application | |
# | |
# Type "mix help compile.app" for more information | |
def application do | |
# Specify extra applications you'll use from Erlang/Elixir | |
[ | |
applications: applications(Mix.env), | |
mod: {HelloGraphql.Application, []}, | |
extra_applications: [:logger], | |
] | |
end | |
defp applications(:dev), do: applications(:all) ++ [:remix] | |
defp applications(_), do: [:cowboy] | |
# Dependencies can be Hex packages: | |
# | |
# {:my_dep, "~> 0.3.0"} | |
# | |
# Or git/path repositories: | |
# | |
# {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | |
# | |
# Type "mix help deps" for more examples and options | |
defp deps do | |
[ | |
{ | |
:cowboy, | |
"~> 1.0.0", | |
}, | |
{ | |
:plug, | |
"~> 1.3.1", | |
}, | |
{ | |
:absinthe, | |
"~> 1.3.0", | |
}, | |
{ | |
:absinthe_plug, | |
"~> 1.3.1", | |
}, | |
{ | |
:poison, | |
"~> 3.1.0", | |
}, | |
{ | |
:remix, | |
"~> 0.0.1", | |
only: :dev, | |
}, | |
] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HelloGraphql.Router do | |
use Plug.Router | |
require Logger | |
plug Plug.RequestId | |
plug Plug.Logger | |
plug :match | |
plug :dispatch | |
plug Plug.Parsers, | |
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser], | |
pass: ["*/*"], | |
json_decoder: Poison | |
forward "/test", to: Absinthe.Plug.GraphiQL, schema: HelloGraphql.Schema | |
forward "/api", to: Absinthe.Plug, schema: HelloGraphql.Schema | |
get "/hello" do | |
Logger.info "Hmm" | |
hello(conn) | |
end | |
get "/hello/:name" do | |
hello(conn, name) | |
end | |
match _ do | |
goodbye(conn) | |
end | |
defp hello(conn, name \\ "World") do | |
body = "Hello, #{String.capitalize(name)}!" | |
conn | |
|> put_resp_content_type("text/plain") | |
|> send_resp(200, body) | |
end | |
defp goodbye(conn) do | |
body = "Goodbye!" | |
conn | |
|> put_resp_content_type("text/plain") | |
|> send_resp(404, body) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule HelloGraphql.Schema do | |
use Absinthe.Schema | |
use Absinthe.Schema.Notation | |
require Logger | |
# Example data | |
@items %{ | |
"foo" => %{id: "foo", name: "Foo!"}, | |
"bar" => %{id: "bar", name: "Bar"} | |
} | |
@desc "An item" | |
object :item do | |
field :id, :id | |
field :name, :string | |
end | |
query do | |
field :item, :item do | |
arg :id, non_null(:id) | |
resolve fn %{id: item_id}, _ -> | |
Logger.info("Item resolve called") | |
{:ok, @items[item_id]} | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment