Skip to content

Commit

Permalink
Add a GraphQL schema for the Project/Tasks sample relational modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gcauchon committed Jul 31, 2023
1 parent 05a5cbc commit 9790823
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lib/elixir_boilerplate/planning/planning.ex
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
8 changes: 6 additions & 2 deletions lib/elixir_boilerplate/planning/project.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
defmodule ElixirBoilerplate.Planning.Project do
use ElixirBoilerplate.Schema

alias ElixirBoilerplate.Planning.Task

schema "projects" do
field :description, :string
field :launch_at, :naive_datetime
field :next_milestone_at, :naive_datetime
field :launch_at, :utc_datetime
field :next_milestone_at, :utc_datetime
field :title, :string

has_many :tasks, Task

timestamps()
end

Expand Down
33 changes: 33 additions & 0 deletions lib/elixir_boilerplate_graphql/planning/types.ex
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
3 changes: 3 additions & 0 deletions lib/elixir_boilerplate_graphql/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ defmodule ElixirBoilerplateGraphQL.Schema do

import_types(Absinthe.Type.Custom)
import_types(ElixirBoilerplateGraphQL.Application.Types)
import_types(ElixirBoilerplateGraphQL.Planning.Types)

query do
import_fields(:application_queries)
import_fields(:planning_queries)
end

# Having an empty mutation block is invalid and raises an error in Absinthe.
# Uncomment it when you add the first mutation.
#
# mutation do
# import_fields(:application_mutations)
# import_fields(:planning_mutations)
# end

def context(context) do
Expand Down

0 comments on commit 9790823

Please sign in to comment.