-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a GraphQL schema for the Project/Tasks sample relational modules
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule ElixirBoilerplate.Planning do | ||
alias ElixirBoilerplate.Planning.Project | ||
alias ElixirBoilerplate.Repo | ||
|
||
import Ecto.Query | ||
|
||
def list() do | ||
projects = | ||
queryable() | ||
|> Repo.all() | ||
|
||
{:ok, projects} | ||
end | ||
|
||
def get(project_id) do | ||
project = | ||
queryable() | ||
|> where(id: ^project_id) | ||
|> Repo.one() | ||
|
||
{:ok, project} | ||
end | ||
|
||
defp queryable() do | ||
from(p in Project, | ||
join: t in assoc(p, :tasks), | ||
order_by: [asc: field(t, :priority)], | ||
preload: [tasks: t] | ||
) | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule ElixirBoilerplateGraphQL.Planning.Types do | ||
use Absinthe.Schema.Notation | ||
|
||
object :project do | ||
@desc "A sample project" | ||
field(:id, :id) | ||
field(:title, :string) | ||
field(:description, :string) | ||
field(:launch_at, :datetime) | ||
field(:tasks, list_of(:task)) | ||
end | ||
|
||
object :task do | ||
@desc "A sample task" | ||
field(:id, :id) | ||
field(:description, :string) | ||
field(:priority, :integer) | ||
field(:due_at, :datetime) | ||
end | ||
|
||
object :planning_queries do | ||
@desc "A list of projects" | ||
field :projects, list_of(:project) do | ||
resolve(fn _parent, _args, _resolution -> ElixirBoilerplate.Planning.list() end) | ||
end | ||
|
||
@desc "A project" | ||
field :project, :project do | ||
arg(:id, :id) | ||
resolve(fn _parent, %{id: id}, _resolution -> ElixirBoilerplate.Planning.get(id) end) | ||
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