Skip to content

Instantly share code, notes, and snippets.

@pmk1c
Last active July 17, 2017 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmk1c/c4d9c4104ffdb6a5ea796592a5d361df to your computer and use it in GitHub Desktop.
Save pmk1c/c4d9c4104ffdb6a5ea796592a5d361df to your computer and use it in GitHub Desktop.
RestQL
defmodule Restql.Web.PostController do
use Restql.Web, :controller
def index(conn, _params) do
posts = Restql.Blog.list_posts
json conn, posts
end
end
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
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
defmodule Restql.Blog.PostResolver do
def all(_args, _info) do
{:ok, Restql.Blog.list_posts |> Restql.Repo.preload(:author)}
end
end
defmodule Restql.Blog.PostResolver do
# ...
def create(args, _info) do
Restql.Blog.create_post(args)
end
# ...
end
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
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
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