Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,52 +443,79 @@ Note: This tutorial was updated on macOS 11.6.3.

import_types(ZeroPhoenixWeb.GraphQL.Schemas.Queries.Person)

alias ZeroPhoenix.Accounts
alias ZeroPhoenix.Repo

query do
import_fields(:person_queries)
end

def context(ctx) do
loader =
Dataloader.new
|> Dataloader.add_source(Accounts, Accounts.datasource())

Map.put(ctx, :loader, loader)
end

def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end
end
```

22. update the `Accounts` context by adding the following after the `change_person` function:

`lib/zero_phoenix/accounts.ex`:

```elixir
# Dataloader

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

def query(queryable, _) do
queryable
end
```

22. add our Person type which will be performing queries against:
23. add our Person type which will be performing queries against:

`lib/zero_phoenix_web/graphql/types/person.ex`:

```elixir
defmodule ZeroPhoenixWeb.GraphQL.Types.Person do
use Absinthe.Schema.Notation

import Ecto
import Absinthe.Resolution.Helpers, only: [dataloader: 1]

alias ZeroPhoenix.Repo
alias ZeroPhoenix.Accounts

@desc "a person"
object :person do
@desc "unique identifier for the person"
field :id, non_null(:string)
field(:id, non_null(:string))

@desc "first name of a person"
field :first_name, non_null(:string)
field(:first_name, non_null(:string))

@desc "last name of a person"
field :last_name, non_null(:string)
field(:last_name, non_null(:string))

@desc "username of a person"
field :username, non_null(:string)
field(:username, non_null(:string))

@desc "email of a person"
field :email, non_null(:string)
field(:email, non_null(:string))

@desc "a list of friends for our person"
field :friends, list_of(:person) do
resolve fn _, %{source: person} ->
{:ok, Repo.all(assoc(person, :friends))}
end
end
field :friends, list_of(:person), resolve: dataloader(Accounts)
end
end
```

23. add the `person_queries` object to contain all the queries for a person:
24. add the `person_queries` object to contain all the queries for a person:

`lib/zero_phoenix_web/graphql/schemas/queries/person.ex`:

Expand All @@ -506,7 +533,7 @@ Note: This tutorial was updated on macOS 11.6.3.
end
```

24. add the `PersonResolver` to fetch the individual fields of our person object:
25. add the `PersonResolver` to fetch the individual fields of our person object:

`lib/zero_phoenix_web/graphql/resolvers/person_resolver.ex`:

Expand All @@ -515,7 +542,7 @@ Note: This tutorial was updated on macOS 11.6.3.
alias ZeroPhoenix.Accounts
alias ZeroPhoenix.Accounts.Person

def find(_parent, %{id: id}, _info) do
def find(_parent, %{id: id}, _resolution) do
case Accounts.get_person(id) do
%Person{} = person ->
{:ok, person}
Expand All @@ -527,7 +554,7 @@ Note: This tutorial was updated on macOS 11.6.3.
end
```

25. add routes for our GraphQL API and GraphiQL browser endpoints:
26. add routes for our GraphQL API and GraphiQL browser endpoints:

`lib/zero_phoenix_web/router.ex`:

Expand Down Expand Up @@ -559,19 +586,19 @@ Note: This tutorial was updated on macOS 11.6.3.
end
```

26. start the server
27. start the server

```zsh
mix phx.server
```

27. navigate to our application within the browser
28. navigate to our application within the browser

```zsh
open http://localhost:4000/graphiql
```

28. enter the below GraphQL query on the left side of the browser window
29. enter the below GraphQL query on the left side of the browser window

```graphql
{
Expand All @@ -590,7 +617,7 @@ Note: This tutorial was updated on macOS 11.6.3.
}
```

29. run the GraphQL query
30. run the GraphQL query

```text
Control + Enter
Expand Down
32 changes: 32 additions & 0 deletions lib/zero_phoenix/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ defmodule ZeroPhoenix.Accounts do
Repo.all(Person)
end

@doc """
Returns a list of people matching the given `criteria`.

Example Criteria:

[{:limit, 15}, {:order, :asc}]
"""

def list_people(criteria) do
query = from p in Person

Enum.reduce(criteria, query, fn
{:limit, limit}, query ->
from p in query, limit: ^limit

{:order, order}, query ->
from p in query, order_by: [{^order, :id}]
end)
|> Repo.all
end


@doc """
Gets a single person.

Expand Down Expand Up @@ -124,4 +146,14 @@ defmodule ZeroPhoenix.Accounts do
def change_person(%Person{} = person) do
Person.changeset(person, %{})
end

# Dataloader

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

def query(queryable, _) do
queryable
end
end
7 changes: 0 additions & 7 deletions lib/zero_phoenix_web/graphql/resolvers/person_resolver.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
defmodule ZeroPhoenixWeb.GraphQL.Resolvers.PersonResolver do
import Ecto

alias ZeroPhoenix.Accounts
alias ZeroPhoenix.Accounts.Person
alias ZeroPhoenix.Repo

def find(_parent, %{id: id}, _resolution) do
case Accounts.get_person(id) do
Expand All @@ -28,8 +25,4 @@ defmodule ZeroPhoenixWeb.GraphQL.Resolvers.PersonResolver do
{:error, "Could not create person"}
end
end

def friends(parent, _args, _resolution) do
{:ok, Repo.all(assoc(parent, :friends))}
end
end
16 changes: 15 additions & 1 deletion lib/zero_phoenix_web/graphql/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ defmodule ZeroPhoenixWeb.GraphQL.Schema do
use Absinthe.Schema

import_types(ZeroPhoenixWeb.GraphQL.Types.Person)

import_types(ZeroPhoenixWeb.GraphQL.Schemas.Queries.Person)

alias ZeroPhoenix.Accounts

query do
import_fields(:person_queries)
end
Expand All @@ -14,4 +16,16 @@ defmodule ZeroPhoenixWeb.GraphQL.Schema do
mutation do
import_fields(:person_mutations)
end

def context(ctx) do
loader =
Dataloader.new
|> Dataloader.add_source(Accounts, Accounts.datasource())

Map.put(ctx, :loader, loader)
end

def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end
end
10 changes: 5 additions & 5 deletions lib/zero_phoenix_web/graphql/types/person.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule ZeroPhoenixWeb.Graphql.Types.Person do
defmodule ZeroPhoenixWeb.GraphQL.Types.Person do
use Absinthe.Schema.Notation

alias ZeroPhoenixWeb.Graphql.Resolvers
import Absinthe.Resolution.Helpers, only: [dataloader: 1]

alias ZeroPhoenix.Accounts

@desc "a person"
object :person do
Expand All @@ -21,9 +23,7 @@ defmodule ZeroPhoenixWeb.Graphql.Types.Person do
field(:email, non_null(:string))

@desc "a list of friends for our person"
field :friends, list_of(:person) do
resolve &Resolvers.PersonResolver.friends/3
end
field :friends, list_of(:person), resolve: dataloader(Accounts)
end

@desc "a person input"
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ defmodule ZeroPhoenix.Mixfile do
{:plug_cowboy, "~> 2.5.2"},
{:absinthe, "~> 1.7.0"},
{:absinthe_plug, "~> 1.5.8"},
{:cors_plug, "~> 2.0.3"}
{:cors_plug, "~> 2.0.3"},
{:dataloader, "~> 1.0.10"}
]
end

Expand Down
8 changes: 4 additions & 4 deletions priv/repo/dataloader_example.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
alias ZeroPhoenix.Accounts

# Fetch 3 places we want to get the bookings for:
# Fetch 3 people we want to get the friends for:

[person1, person2, person3] = Accounts.list_people([limit: 3])

Expand All @@ -18,7 +18,7 @@ source = Dataloader.Ecto.new(ZeroPhoenix.Repo)

loader = loader |> Dataloader.add_source(Accounts, source)

# Create a batch of bookings to be loaded (does not query the database):
# Create a batch of friends to be loaded (does not query the database):

loader =
loader
Expand All @@ -30,9 +30,9 @@ loader =

loader = loader |> Dataloader.run

# 🔥 This runs one Ecto query to fetch all the bookings for all the places!
# 🔥 This runs one Ecto query to fetch all the friends for all the people!

# Now you can get the bookings for a particular place:
# Now you can get the friends for a particular person:

loader |> Dataloader.load(Accounts, :friends, person1) |> IO.inspect
loader |> Dataloader.load(Accounts, :friends, person2) |> IO.inspect
Expand Down