-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up Dataloader to improve database queries via batching
- Loading branch information
Showing
6 changed files
with
61 additions
and
40 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 |
---|---|---|
@@ -1,20 +1,36 @@ | ||
# AbsintheDemo | ||
# GraphQL server with Dataloader Example | ||
|
||
To start your Phoenix server: | ||
I've left a trail of [commit messages](https://github.com/denvaar/absinthe_graphql_dataloader/commits/master) that is hopefully easy to follow. I might try to make a video soon. | ||
|
||
* Install dependencies with `mix deps.get` | ||
* Create and migrate your database with `mix ecto.setup` | ||
* Install Node.js dependencies with `cd assets && npm install` | ||
* Start Phoenix endpoint with `mix phx.server` | ||
- [Create a new Phoenix app](https://github.com/denvaar/absinthe_graphql_dataloader/commit/5fa2681f8797aa9317e223437eaf6fbd51e39bfb). | ||
- [Install Absinthe and configure it to work with Phoenix](https://github.com/denvaar/absinthe_graphql_dataloader/commit/51bd434b832091ca394ee2f977bdfe63b5e38ab6). | ||
- [Create a database schema with some basic relations; Hook it up to GraphQL schema](https://github.com/denvaar/absinthe_graphql_dataloader/commit/1b523986f5698eeb7c2dfa8dc12dd357e771d245). At this point we have a naive GraphQL schema. The resolvers cause some horrible database queries to happen. | ||
- [Set up Dataloader with Absinthe to improve performance of database queries](https://github.com/denvaar/absinthe_graphql_dataloader/commit/dbbaae5828ffc1a403ce0cea3bc901e048288e0f). | ||
|
||
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. | ||
### Setup Instructions | ||
|
||
Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). | ||
1. Install dependencies `mix deps.get` | ||
1. Create database `mix ecto.setup` | ||
1. Create database schema and populate with data `mix ecto.drop; mix ecto.create; mix ecto.migrate; mix run priv/repo/seeds.exs` | ||
1. Start the Phoenix server `mix phx.server` | ||
1. Run GraphQL queries (and browse schema documentation) from your web browser at `http://localhost:4000/api/graphiql` | ||
|
||
## Learn more | ||
### Learn more about Dataloader (Efficiently load data in batches) | ||
|
||
* Official website: http://www.phoenixframework.org/ | ||
* Guides: https://hexdocs.pm/phoenix/overview.html | ||
* Docs: https://hexdocs.pm/phoenix | ||
* Mailing list: http://groups.google.com/group/phoenix-talk | ||
* Source: https://github.com/phoenixframework/phoenix | ||
- [Elixir Dataloader GitHub repo](https://github.com/absinthe-graphql/dataloader) | ||
- [Elixir Dataloader Hex docs](https://hexdocs.pm/dataloader/Dataloader.html) | ||
- [A helpful blog post](https://schneider.dev/blog/elixir-phoenix-absinthe-graphql-react-apollo-followup/) | ||
- [Original Dataloader JavaScript implementation](https://github.com/graphql/dataloader) | ||
|
||
### Learn more about Absinthe (Elixir implementation of GraphQL spec) | ||
|
||
- [Absinthe GitHub Repo](https://github.com/absinthe-graphql/absinthe) | ||
- [Absinthe Hex docs](https://hexdocs.pm/absinthe/overview.html) | ||
|
||
### Learn more about Phoenix (Elixir Web Framework) | ||
|
||
- [Official website](http://www.phoenixframework.org/) | ||
- [Guides](https://hexdocs.pm/phoenix/overview.html) | ||
- [Docs](https://hexdocs.pm/phoenix) | ||
- [Mailing list](http://groups.google.com/group/phoenix-talk) | ||
- [Source](https://github.com/phoenixframework/phoenix) |
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,9 @@ | ||
defmodule AbsintheDemo.DataSource do | ||
def data() do | ||
Dataloader.Ecto.new(AbsintheDemo.Repo, query: &query/2) | ||
end | ||
|
||
def query(queryable, _params) do | ||
queryable | ||
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 |
---|---|---|
@@ -1,50 +1,32 @@ | ||
defmodule AbsintheDemoWeb.GraphQL.Typedefs do | ||
use Absinthe.Schema.Notation | ||
|
||
import Absinthe.Resolution.Helpers, only: [dataloader: 1] | ||
|
||
alias AbsintheDemo.Database | ||
|
||
object :post do | ||
field :id, :id | ||
field :title, :string | ||
field :body, :string | ||
|
||
field :author, :author do | ||
resolve(fn post, _args, _info -> | ||
# Example of N+1 problem | ||
{:ok, Database.get_author(post.author_id)} | ||
end) | ||
end | ||
|
||
field :categories, list_of(:category) do | ||
resolve(fn post, _args, _info -> | ||
# Example of N+1 problem | ||
{:ok, Database.get_categories_of_post(post.id)} | ||
end) | ||
end | ||
field :author, :author, resolve: dataloader(:blog_context) | ||
|
||
field :categories, list_of(:category), resolve: dataloader(:blog_context) | ||
end | ||
|
||
object :author do | ||
field :id, :id | ||
field :name, :string | ||
field :profile_picture_link, :string | ||
|
||
field :posts, list_of(:post) do | ||
resolve(fn author, _args, _info -> | ||
# Example of N+1 problem | ||
{:ok, Database.get_posts_of_author(author.id)} | ||
end) | ||
end | ||
field :posts, list_of(:post), resolve: dataloader(:blog_context) | ||
end | ||
|
||
object :category do | ||
field :id, :id | ||
field :name, :string | ||
|
||
field :posts, list_of(:post) do | ||
resolve(fn category, _args, _info -> | ||
# Example of N+1 problem | ||
{:ok, Database.get_posts_of_category(category.id)} | ||
end) | ||
end | ||
field :posts, list_of(:post), resolve: dataloader(:blog_context) | ||
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