Skip to content

Commit

Permalink
Add ability to handle args in GraphQL resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
denvaar committed Mar 6, 2020
1 parent 57a581b commit dc96e01
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
40 changes: 26 additions & 14 deletions lib/absinthe_demo/database.ex
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
defmodule AbsintheDemo.Database do
import Ecto.Query, only: [from: 2]
import Ecto.Query, only: [from: 2, from: 1]
alias AbsintheDemo.{Author, Post, Category, Repo}

# Posts

def all_posts() do
Repo.all(Post)
def gen_all(query) do
query |> Repo.all()
end

# Posts

def get_post(id) do
Repo.get(Post, id)
end

def get_posts_of_author(author_id) do
from(c in Post, preload: :author, where: c.author_id == ^author_id)
|> Repo.all()
def query_all_posts(args) do
from(p in Post)
|> apply_post_args(args)
end

def get_posts_of_category(category_id) do
from(p in Post,
join: pc in "posts_categories",
on: pc.category_id == ^category_id and pc.post_id == p.id
)
|> Repo.all()
defp apply_post_args(query, %{author_id: author_id} = args) do
query = from(p in query, where: p.author_id == ^author_id)

apply_post_args(query, Map.delete(args, :author_id))
end

defp apply_post_args(query, %{categories: categories} = args) do
query = from(p in query, join: c in assoc(p, :categories), where: c.name in ^categories)

apply_post_args(query, Map.delete(args, :categories))
end

defp apply_post_args(query, %{title: title} = args) do
query = from(p in query, where: ilike(p.title, ^"%#{title}%"))

apply_post_args(query, Map.delete(args, :title))
end

defp apply_post_args(query, %{}), do: query

# Authors

def all_authors() do
Expand Down
7 changes: 7 additions & 0 deletions lib/absinthe_demo_web/data_source.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
defmodule AbsintheDemo.DataSource do
import Ecto.Query, only: [from: 2]
alias AbsintheDemo.{Post, Database}

def data() do
Dataloader.Ecto.new(AbsintheDemo.Repo, query: &query/2)
end

def query(Post, params) do
Database.query_all_posts(params)
end

def query(queryable, _params) do
queryable
end
Expand Down
4 changes: 2 additions & 2 deletions lib/absinthe_demo_web/graphql/resolvers.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule AbsintheDemoWeb.GraphQL.Resolvers do
alias AbsintheDemo.Database

def list_posts(_parent, _args, resolution) do
{:ok, Database.all_posts()}
def list_posts(_parent, args, resolution) do
{:ok, Database.query_all_posts(args) |> Database.gen_all()}
end

def list_authors(_parent, _args, _resolution) do
Expand Down
2 changes: 2 additions & 0 deletions lib/absinthe_demo_web/graphql/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule AbsintheDemoWeb.GraphQL.Schema do
query do
@desc "Get all posts"
field :posts, list_of(:post) do
arg(:categories, list_of(:string))
arg(:title, :string)
resolve(&Resolvers.list_posts/3)
end

Expand Down
19 changes: 15 additions & 4 deletions lib/absinthe_demo_web/graphql/typedefs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@ defmodule AbsintheDemoWeb.GraphQL.Typedefs do

field :author, :author, resolve: dataloader(:blog_context)

field :categories, list_of(:category), resolve: dataloader(:blog_context)
field :categories, list_of(:category) do
# post_id arg is implied
resolve(dataloader(:blog_context))
end
end

object :author do
field :id, :id
field :name, :string
field :profile_picture_link, :string
field :profile_pic_link, :string

field :posts, list_of(:post), resolve: dataloader(:blog_context)
field :posts, list_of(:post) do
# author_id arg is implied
arg(:categories, list_of(:string))
resolve(dataloader(:blog_context))
end
end

object :category do
field :id, :id
field :name, :string

field :posts, list_of(:post), resolve: dataloader(:blog_context)
field :posts, list_of(:post) do
arg(:categories, list_of(:string))
arg(:author_id, :id)
resolve(dataloader(:blog_context))
end
end
end

0 comments on commit dc96e01

Please sign in to comment.