<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohammed Rabeeh</title>
    <description>The latest articles on DEV Community by Mohammed Rabeeh (@rabeehrz).</description>
    <link>https://dev.to/rabeehrz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F448424%2Fe3a00b50-f228-4d1d-a138-483d49f110c1.jpeg</url>
      <title>DEV Community: Mohammed Rabeeh</title>
      <link>https://dev.to/rabeehrz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rabeehrz"/>
    <language>en</language>
    <item>
      <title>Persisting DataLoader between requests and using it as a cache</title>
      <dc:creator>Mohammed Rabeeh</dc:creator>
      <pubDate>Sat, 14 Nov 2020 13:46:27 +0000</pubDate>
      <link>https://dev.to/rabeehrz/persisting-dataloader-between-requests-and-using-it-as-a-cache-26fo</link>
      <guid>https://dev.to/rabeehrz/persisting-dataloader-between-requests-and-using-it-as-a-cache-26fo</guid>
      <description>&lt;p&gt;I'm working on a project where I use the DataLoader to batch requests (N + 1 Problem). I accidentally created DataLoader beforehand and started to pass it in the context, instead of initializing the DataLoader in the context. I'll explain.&lt;/p&gt;

&lt;p&gt;DataLoader is meant to be used for "per request batching and caching". Therefore, you would do something like this to implement DataLoader normally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(...,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;userLoader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DataLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batchFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would make it so that the DataLoader exist only in the context of a particular request. However, what I accidentally did was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;

&lt;span class="nx"&gt;userLoader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DataLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batchFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(...,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;userLoader&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This started to cache the data between requests. It was not hitting my database for the data it already had. So I started wondering if there was anything wrong in caching the data in this way. I get batching. I get caching without having to manually implement it. I can clear the data-&amp;gt;id from the cache when the database is updated. So far so good. &lt;/p&gt;

&lt;p&gt;But there has to be issues with this implementation. One limitation I found is that as the cache grows, eventually the entire database will be cached. I don't know if that's an issue or not. This can be avoided by using &lt;code&gt;{ cache:false }&lt;/code&gt; option in the loader to cache only the required data.&lt;/p&gt;

&lt;p&gt;It would be great if someone more experienced could give me some feedback and shed some more light on this. Thank you!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>graphql</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
