Last active
July 17, 2017 09:06
-
-
Save pmk1c/c4d9c4104ffdb6a5ea796592a5d361df to your computer and use it in GitHub Desktop.
RestQL
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 Restql.Web.PostController do | |
use Restql.Web, :controller | |
def index(conn, _params) do | |
posts = Restql.Blog.list_posts | |
json conn, posts | |
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 Restql.Web.PostController do | |
use Restql.Web, :controller | |
alias Restql.Blog | |
alias Restql.Blog.Post | |
def index(conn, _params) do | |
graphql conn, """ | |
{ | |
posts { | |
title | |
body | |
author { | |
name | |
} | |
} | |
} | |
""" | |
end | |
defp graphql(conn, document, variables \\ %{}, schema \\ Restql.Blog.Schema) do | |
with {:ok, data} <- Absinthe.run(document, schema, variables: variables) do | |
json conn, data | |
end | |
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 Restql.Web.PostController do | |
# ... | |
def create(conn, %{"post" => post_params}) do | |
graphql conn, """ | |
mutation CreatePost($title: String!, $body: String!) { | |
post(title: $title, body: $body) { | |
id | |
} | |
} | |
""", post_params | |
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 Restql.Blog.PostResolver do | |
def all(_args, _info) do | |
{:ok, Restql.Blog.list_posts |> Restql.Repo.preload(:author)} | |
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 Restql.Blog.PostResolver do | |
# ... | |
def create(args, _info) do | |
Restql.Blog.create_post(args) | |
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 Restql.Blog.Schema do | |
use Absinthe.Schema | |
import_types Restql.Blog.Schema.Types | |
query do | |
field :posts, list_of(:post) do | |
resolve &Restql.Blog.PostResolver.all/2 | |
end | |
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 Restql.Blog.Schema do | |
# ... | |
mutation name: "CreatePost" do | |
field :post, type: :post do | |
arg :title, non_null(:string) | |
arg :body, non_null(:string) | |
resolve &Restql.Blog.PostResolver.create/2 | |
end | |
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 Restql.Blog.Schema.Types do | |
use Absinthe.Schema.Notation | |
object :post do | |
field :id, :id | |
field :title, :string | |
field :body, :string | |
field :author, :user | |
end | |
object :user do | |
field :id, :id | |
field :name, :string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment