2022-03-04 10:32:21 -06:00
<!DOCTYPE HTML>
2022-04-12 11:26:39 -05:00
< html lang = "en" class = "sidebar-visible no-js light" >
2022-03-04 10:32:21 -06:00
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
2022-04-12 11:26:39 -05:00
< title > Dataloaders - Juniper Book (GraphQL server for Rust)< / title >
2023-02-27 13:27:31 -06:00
2022-04-12 11:26:39 -05:00
<!-- Custom HTML head -->
2023-02-27 13:27:31 -06:00
2022-04-12 11:26:39 -05:00
< meta name = "description" content = "User guide for Juniper (GraphQL server library for Rust)." >
2022-03-04 10:32:21 -06:00
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< meta name = "theme-color" content = "#ffffff" / >
2022-04-12 11:26:39 -05:00
< link rel = "icon" href = "../favicon.svg" >
2022-03-04 10:32:21 -06:00
< link rel = "shortcut icon" href = "../favicon.png" >
< link rel = "stylesheet" href = "../css/variables.css" >
< link rel = "stylesheet" href = "../css/general.css" >
< link rel = "stylesheet" href = "../css/chrome.css" >
< link rel = "stylesheet" href = "../css/print.css" media = "print" >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
<!-- Fonts -->
< link rel = "stylesheet" href = "../FontAwesome/css/font-awesome.css" >
2022-04-12 11:26:39 -05:00
< link rel = "stylesheet" href = "../fonts/fonts.css" >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
<!-- Highlight.js Stylesheets -->
< link rel = "stylesheet" href = "../highlight.css" >
< link rel = "stylesheet" href = "../tomorrow-night.css" >
< link rel = "stylesheet" href = "../ayu-highlight.css" >
<!-- Custom theme stylesheets -->
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
< / head >
2022-04-12 11:26:39 -05:00
< body >
2023-02-27 13:27:31 -06:00
< div id = "body-container" >
2022-03-04 10:32:21 -06:00
<!-- Provide site root to javascript -->
2023-02-27 13:27:31 -06:00
< script >
2022-04-12 11:26:39 -05:00
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
< / script >
2022-03-04 10:32:21 -06:00
<!-- Work around some values being stored in localStorage wrapped in quotes -->
2023-02-27 13:27:31 -06:00
< script >
2022-03-04 10:32:21 -06:00
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') & & theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') & & sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
< / script >
<!-- Set the theme before any content is loaded, prevents flash -->
2023-02-27 13:27:31 -06:00
< script >
2022-03-04 10:32:21 -06:00
var theme;
2022-04-12 11:26:39 -05:00
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('light')
html.classList.add(theme);
html.classList.add('js');
2022-03-04 10:32:21 -06:00
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
2023-02-27 13:27:31 -06:00
< script >
2022-03-04 10:32:21 -06:00
var html = document.querySelector('html');
2023-04-04 07:07:31 -05:00
var sidebar = null;
2022-03-04 10:32:21 -06:00
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
2023-04-04 07:07:31 -05:00
} else {
sidebar = 'hidden';
2022-03-04 10:32:21 -06:00
}
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
< / script >
< nav id = "sidebar" class = "sidebar" aria-label = "Table of contents" >
2022-04-12 11:26:39 -05:00
< div class = "sidebar-scrollbox" >
< ol class = "chapter" > < li class = "chapter-item expanded " > < a href = "../index.html" > < strong aria-hidden = "true" > 1.< / strong > Introduction< / a > < / li > < li class = "chapter-item expanded " > < a href = "../quickstart.html" > < strong aria-hidden = "true" > 2.< / strong > Quickstart< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/index.html" > < strong aria-hidden = "true" > 3.< / strong > Type System< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../types/objects/defining_objects.html" > < strong aria-hidden = "true" > 3.1.< / strong > Defining objects< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../types/objects/complex_fields.html" > < strong aria-hidden = "true" > 3.1.1.< / strong > Complex fields< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/objects/using_contexts.html" > < strong aria-hidden = "true" > 3.1.2.< / strong > Using contexts< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/objects/error_handling.html" > < strong aria-hidden = "true" > 3.1.3.< / strong > Error handling< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../types/other-index.html" > < strong aria-hidden = "true" > 3.2.< / strong > Other types< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../types/enums.html" > < strong aria-hidden = "true" > 3.2.1.< / strong > Enums< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/interfaces.html" > < strong aria-hidden = "true" > 3.2.2.< / strong > Interfaces< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/input_objects.html" > < strong aria-hidden = "true" > 3.2.3.< / strong > Input objects< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/scalars.html" > < strong aria-hidden = "true" > 3.2.4.< / strong > Scalars< / a > < / li > < li class = "chapter-item expanded " > < a href = "../types/unions.html" > < strong aria-hidden = "true" > 3.2.5.< / strong > Unions< / a > < / li > < / ol > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../schema/schemas_and_mutations.html" > < strong aria-hidden = "true" > 4.< / strong > Schemas and mutations< / a > < / li > < li class = "chapter-item expanded " > < a href = "../servers/index.html" > < strong aria-hidden = "true" > 5.< / strong > Adding A Server< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../servers/official.html" > < strong aria-hidden = "true" > 5.1.< / strong > Official Server Integrations< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../servers/warp.html" > < strong aria-hidden = "true" > 5.1.1.< / strong > Warp< / a > < / li > < li class = "chapter-item expanded " > < a href = "../servers/rocket.html" > < strong aria-hidden = "true" > 5.1.2.< / strong > Rocket< / a > < / li > < li class = "chapter-item expanded " > < a href = "../servers/iron.html" > < strong aria-hidden = "true" > 5.1.3.< / strong > Iron< / a > < / li > < li class = "chapter-item expanded " > < a href = "../servers/hyper.html" > < strong aria-hidden = "true" > 5.1.4.< / strong > Hyper< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../servers/third-party.html" > < strong aria-hidden = "true" > 5.2.< / strong > Third Party Integrations< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/index.html" > < strong aria-hidden = "true" > 6.< / strong > Advanced Topics< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../advanced/introspection.html" > < strong aria-hidden = "true" > 6.1.< / strong > Introspection< / a > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/non_struct_objects.html" > < strong aria-hidden = "true" > 6.2.< / strong > Non-struct objects< / a > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/implicit_and_explicit_null.html" > < strong aria-hidden = "true" > 6.3.< / strong > Implicit and explicit null< / a > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/objects_and_generics.html" > < strong aria-hidden = "true" > 6.4.< / strong > Objects and generics< / a > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/multiple_ops_per_request.html" > < strong aria-hidden = "true" > 6.5.< / strong > Multiple operations per request< / a > < / li > < li class = "chapter-item expanded " > < a href = "../advanced/dataloaders.html" class = "active" > < strong aria-hidden = "true" > 6.6.< / strong > Dataloaders< / a > < / li > < li cla
< / div >
< div id = "sidebar-resize-handle" class = "sidebar-resize-handle" > < / div >
2022-03-04 10:32:21 -06:00
< / nav >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
2023-02-27 13:27:31 -06:00
< div id = "menu-bar-hover-placeholder" > < / div >
2022-04-12 11:26:39 -05:00
< div id = "menu-bar" class = "menu-bar sticky bordered" >
< div class = "left-buttons" >
< button id = "sidebar-toggle" class = "icon-button" type = "button" title = "Toggle Table of Contents" aria-label = "Toggle Table of Contents" aria-controls = "sidebar" >
< i class = "fa fa-bars" > < / i >
< / button >
< button id = "theme-toggle" class = "icon-button" type = "button" title = "Change theme" aria-label = "Change theme" aria-haspopup = "true" aria-expanded = "false" aria-controls = "theme-list" >
< i class = "fa fa-paint-brush" > < / i >
< / button >
< ul id = "theme-list" class = "theme-popup" aria-label = "Themes" role = "menu" >
2023-02-27 13:27:31 -06:00
< li role = "none" > < button role = "menuitem" class = "theme" id = "light" > Light< / button > < / li >
2022-04-12 11:26:39 -05:00
< li role = "none" > < button role = "menuitem" class = "theme" id = "rust" > Rust< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "coal" > Coal< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "navy" > Navy< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "ayu" > Ayu< / button > < / li >
< / ul >
< button id = "search-toggle" class = "icon-button" type = "button" title = "Search. (Shortkey: s)" aria-label = "Toggle Searchbar" aria-expanded = "false" aria-keyshortcuts = "S" aria-controls = "searchbar" >
< i class = "fa fa-search" > < / i >
< / button >
< / div >
< h1 class = "menu-title" > Juniper Book (GraphQL server for Rust)< / h1 >
< div class = "right-buttons" >
< a href = "../print.html" title = "Print this book" aria-label = "Print this book" >
< i id = "print-button" class = "fa fa-print" > < / i >
< / a >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
< / div >
< / div >
< div id = "search-wrapper" class = "hidden" >
< form id = "searchbar-outer" class = "searchbar-outer" >
2022-04-12 11:26:39 -05:00
< input type = "search" id = "searchbar" name = "searchbar" placeholder = "Search this book ..." aria-controls = "searchresults-outer" aria-describedby = "searchresults-header" >
2022-03-04 10:32:21 -06:00
< / form >
< div id = "searchresults-outer" class = "searchresults-outer hidden" >
< div id = "searchresults-header" class = "searchresults-header" > < / div >
< ul id = "searchresults" >
< / ul >
< / div >
< / div >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
2023-02-27 13:27:31 -06:00
< script >
2022-03-04 10:32:21 -06:00
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
< / script >
< div id = "content" class = "content" >
< main >
2022-04-12 11:26:39 -05:00
< h1 id = "avoiding-the-n1-problem-with-dataloaders" > < a class = "header" href = "#avoiding-the-n1-problem-with-dataloaders" > Avoiding the N+1 Problem With Dataloaders< / a > < / h1 >
2022-03-04 10:32:21 -06:00
< p > A common issue with graphql servers is how the resolvers query their datasource.
This issue results in a large number of unneccessary database queries or http requests.
Say you were wanting to list a bunch of cults people were in< / p >
< pre > < code class = "language-graphql" > query {
persons {
id
name
cult {
id
name
}
}
}
< / code > < / pre >
< p > What would be executed by a SQL database would be:< / p >
< pre > < code class = "language-sql" > SELECT id, name, cult_id FROM persons;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 1;
SELECT id, name FROM cults WHERE id = 2;
SELECT id, name FROM cults WHERE id = 2;
SELECT id, name FROM cults WHERE id = 2;
# ...
< / code > < / pre >
< p > Once the list of users has been returned, a separate query is run to find the cult of each user.
You can see how this could quickly become a problem.< / p >
< p > A common solution to this is to introduce a < strong > dataloader< / strong > .
This can be done with Juniper using the crate < a href = "https://github.com/cksac/dataloader-rs" > cksac/dataloader-rs< / a > , which has two types of dataloaders; cached and non-cached.< / p >
2022-04-12 11:26:39 -05:00
< h4 id = "cached-loader" > < a class = "header" href = "#cached-loader" > Cached Loader< / a > < / h4 >
2022-03-04 10:32:21 -06:00
< p > DataLoader provides a memoization cache, after .load() is called once with a given key, the resulting value is cached to eliminate redundant loads.< / p >
< p > DataLoader caching does not replace Redis, Memcache, or any other shared application-level cache. DataLoader is first and foremost a data loading mechanism, and its cache only serves the purpose of not repeatedly loading the same data in the context of a single request to your Application. < a href = "https://github.com/graphql/dataloader#caching" > (read more)< / a > < / p >
2022-04-12 11:26:39 -05:00
< h3 id = "what-does-it-look-like" > < a class = "header" href = "#what-does-it-look-like" > What does it look like?< / a > < / h3 >
2022-03-04 10:32:21 -06:00
< p > !FILENAME Cargo.toml< / p >
< pre > < code class = "language-toml" > [dependencies]
actix-identity = " 0.4.0-beta.4"
actix-rt = " 1.0"
2022-04-12 11:26:39 -05:00
actix-web = " 2.0"
async-trait = " 0.1.30"
dataloader = " 0.12.0"
2022-03-04 10:32:21 -06:00
futures = " 0.3"
2022-04-12 11:26:39 -05:00
juniper = " 0.16.0"
2022-03-04 10:32:21 -06:00
postgres = " 0.15.2"
< / code > < / pre >
2022-04-12 11:26:39 -05:00
< pre > < code class = "language-rust ignore" > // use dataloader::cached::Loader;
2022-03-04 10:32:21 -06:00
use dataloader::non_cached::Loader;
use dataloader::BatchFn;
use std::collections::HashMap;
use postgres::{Connection, TlsMode};
use std::env;
pub fn get_db_conn() -> Connection {
let pg_connection_string = env::var(" DATABASE_URI" ).expect(" need a db uri" );
2022-07-13 09:04:13 -05:00
println!(" Connecting to {pg_connection_string}" );
2022-03-04 10:32:21 -06:00
let conn = Connection::connect(& pg_connection_string[..], TlsMode::None).unwrap();
println!(" Connection is fine" );
conn
}
#[derive(Debug, Clone)]
pub struct Cult {
pub id: i32,
pub name: String,
}
pub fn get_cult_by_ids(hashmap: & mut HashMap< i32, Cult> , ids: Vec< i32> ) {
let conn = get_db_conn();
for row in & conn
.query(" SELECT id, name FROM cults WHERE id = ANY($1)" , & [& ids])
.unwrap()
{
let cult = Cult {
id: row.get(0),
name: row.get(1),
};
hashmap.insert(cult.id, cult);
}
}
pub struct CultBatcher;
#[async_trait]
impl BatchFn< i32, Cult> for CultBatcher {
// A hashmap is used, as we need to return an array which maps each original key to a Cult.
async fn load(& self, keys: & [i32]) -> HashMap< i32, Cult> {
2022-07-13 09:04:13 -05:00
println!(" load cult batch {keys:?}" );
2022-03-04 10:32:21 -06:00
let mut cult_hashmap = HashMap::new();
get_cult_by_ids(& mut cult_hashmap, keys.to_vec());
cult_hashmap
}
}
pub type CultLoader = Loader< i32, Cult, CultBatcher> ;
// To create a new loader
pub fn get_loader() -> CultLoader {
Loader::new(CultBatcher)
// Usually a DataLoader will coalesce all individual loads which occur
// within a single frame of execution before calling your batch function with all requested keys.
// However sometimes this behavior is not desirable or optimal.
// Perhaps you expect requests to be spread out over a few subsequent ticks
// See: https://github.com/cksac/dataloader-rs/issues/12
// More info: https://github.com/graphql/dataloader#batch-scheduling
// A larger yield count will allow more requests to append to batch but will wait longer before actual load.
.with_yield_count(100)
}
#[juniper::graphql_object(Context = Context)]
impl Cult {
// your resolvers
// To call the dataloader
pub async fn cult_by_id(ctx: & Context, id: i32) -> Cult {
ctx.cult_loader.load(id).await
}
}
< / code > < / pre >
2022-04-12 11:26:39 -05:00
< h3 id = "how-do-i-call-them" > < a class = "header" href = "#how-do-i-call-them" > How do I call them?< / a > < / h3 >
2022-03-04 10:32:21 -06:00
< p > Once created, a dataloader has the async functions < code > .load()< / code > and < code > .load_many()< / code > .
In the above example < code > cult_loader.load(id: i32).await< / code > returns < code > Cult< / code > . If we had used < code > cult_loader.load_many(Vec< i32> ).await< / code > it would have returned < code > Vec< Cult> < / code > .< / p >
2022-04-12 11:26:39 -05:00
< h3 id = "where-do-i-create-my-dataloaders" > < a class = "header" href = "#where-do-i-create-my-dataloaders" > Where do I create my dataloaders?< / a > < / h3 >
2022-03-04 10:32:21 -06:00
< p > < strong > Dataloaders< / strong > should be created per-request to avoid risk of bugs where one user is able to load cached/batched data from another user/ outside of its authenticated scope.
Creating dataloaders within individual resolvers will prevent batching from occurring and will nullify the benefits of the dataloader.< / p >
< p > For example:< / p >
< p > < em > When you declare your context< / em > < / p >
2022-04-12 11:26:39 -05:00
< pre > < code class = "language-rust ignore" > use juniper;
2022-03-04 10:32:21 -06:00
#[derive(Clone)]
pub struct Context {
pub cult_loader: CultLoader,
}
impl juniper::Context for Context {}
impl Context {
pub fn new(cult_loader: CultLoader) -> Self {
Self {
cult_loader
}
}
2023-02-27 13:27:31 -06:00
}< / code > < / pre >
2022-03-04 10:32:21 -06:00
< p > < em > Your handler for GraphQL (Note: instantiating context here keeps it per-request)< / em > < / p >
2022-04-12 11:26:39 -05:00
< pre > < code class = "language-rust ignore" > pub async fn graphql(
2022-03-04 10:32:21 -06:00
st: web::Data< Arc< Schema> > ,
data: web::Json< GraphQLRequest> ,
) -> Result< HttpResponse, Error> {
// Context setup
let cult_loader = get_loader();
let ctx = Context::new(cult_loader);
// Execute
let res = data.execute(& st, & ctx).await;
let json = serde_json::to_string(& res).map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok()
.content_type(" application/json" )
.body(json))
2023-02-27 13:27:31 -06:00
}< / code > < / pre >
2022-04-12 11:26:39 -05:00
< h3 id = "further-example" > < a class = "header" href = "#further-example" > Further Example:< / a > < / h3 >
2022-03-04 10:32:21 -06:00
< p > For a full example using Dataloaders and Context check out < a href = "https://github.com/jayy-lmao/rust-graphql-docker" > jayy-lmao/rust-graphql-docker< / a > .< / p >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../advanced/multiple_ops_per_request.html" class = "mobile-nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
< i class = "fa fa-angle-left" > < / i >
< / a >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
< a rel = "next" href = "../advanced/subscriptions.html" class = "mobile-nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
< i class = "fa fa-angle-right" > < / i >
< / a >
2023-02-27 13:27:31 -06:00
2022-03-04 10:32:21 -06:00
< div style = "clear: both" > < / div >
< / nav >
< / div >
< / div >
< nav class = "nav-wide-wrapper" aria-label = "Page navigation" >
2022-04-12 11:26:39 -05:00
< a rel = "prev" href = "../advanced/multiple_ops_per_request.html" class = "nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
2022-03-04 10:32:21 -06:00
< i class = "fa fa-angle-left" > < / i >
< / a >
2023-02-27 13:27:31 -06:00
2022-04-12 11:26:39 -05:00
< a rel = "next" href = "../advanced/subscriptions.html" class = "nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
2022-03-04 10:32:21 -06:00
< i class = "fa fa-angle-right" > < / i >
< / a >
< / nav >
< / div >
2023-02-27 13:27:31 -06:00
< script >
2022-04-12 11:26:39 -05:00
window.playground_copyable = true;
< / script >
2023-02-27 13:27:31 -06:00
< script src = "../elasticlunr.min.js" > < / script >
< script src = "../mark.min.js" > < / script >
< script src = "../searcher.js" > < / script >
< script src = "../clipboard.min.js" > < / script >
< script src = "../highlight.js" > < / script >
< script src = "../book.js" > < / script >
2022-03-04 10:32:21 -06:00
<!-- Custom JS scripts -->
2023-02-27 13:27:31 -06:00
< / div >
2022-03-04 10:32:21 -06:00
< / body >
< / html >