<?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: Samuel Kauffmann</title>
    <description>The latest articles on DEV Community by Samuel Kauffmann (@skauffmann).</description>
    <link>https://dev.to/skauffmann</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%2F321676%2Fcb82ee12-c3b3-4c4c-9f58-0b2198edae32.jpg</url>
      <title>DEV Community: Samuel Kauffmann</title>
      <link>https://dev.to/skauffmann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skauffmann"/>
    <language>en</language>
    <item>
      <title>Apollo mongoose plugin</title>
      <dc:creator>Samuel Kauffmann</dc:creator>
      <pubDate>Fri, 30 Apr 2021 12:47:05 +0000</pubDate>
      <link>https://dev.to/skauffmann/apollo-mongoose-plugin-2j7i</link>
      <guid>https://dev.to/skauffmann/apollo-mongoose-plugin-2j7i</guid>
      <description>&lt;p&gt;Let me introduce my new library &lt;a href="https://github.com/skauffmann/apollo-mongoose-plugin" rel="noopener noreferrer"&gt;&lt;code&gt;apollo-mongoose-plugin&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's a plugin to add mongoose query information on the Apollo GraphQL response extensions node.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrkcq8w549xx1qqurxpc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnrkcq8w549xx1qqurxpc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you need this plugin?
&lt;/h2&gt;

&lt;p&gt;The purpose of the &lt;code&gt;apollo-mongoose-plugin&lt;/code&gt; is to help you to identify your redundant, long, or un-batched MongoDB queries.&lt;br&gt;
Thanks to GraphQL it's really easy to create a field resolver to load linked data on-demand. &lt;br&gt;
But working on small pieces of data can make us lose the big picture and lead to performance issues.&lt;br&gt;
With this plugin, you will be able to keep a look on global performance and the execution plan of your Mongo queries.&lt;/p&gt;
&lt;h2&gt;
  
  
  How it works?
&lt;/h2&gt;

&lt;p&gt;The library is composed of 2 plugins. Once to collect query information from Mongoose and the second one to add collected queries on the Apollo GraphQL response.&lt;/p&gt;
&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Install the plugin with &lt;code&gt;npm install apollo-mongoose-plugin&lt;/code&gt;, then register the &lt;code&gt;mongoCollectorPlugin&lt;/code&gt; to Mongoose and &lt;code&gt;ApolloMongoosePlugin&lt;/code&gt; on the &lt;code&gt;ApolloServer&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ApolloMongoosePlugin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;mongoCollectorPlugin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apollo-mongoose-plugin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apollo-server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// first: register mongoose query collector plugin&lt;/span&gt;
&lt;span class="c1"&gt;// 🔔  Make sure you are registering the mongoose plugin&lt;/span&gt;
&lt;span class="c1"&gt;//     before you are creating mongoose models&lt;/span&gt;
&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;plugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mongoCollectorPlugin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// then: register apollo plugin&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="c1"&gt;// ...schema, dataSources&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApolloMongoosePlugin&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;h2&gt;
  
  
  How can I handle performance issues?
&lt;/h2&gt;

&lt;p&gt;I must admit that I am not an expert on this subject but through my experience, I have used at least these few tips:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add indexes to optimize search&lt;/li&gt;
&lt;li&gt;Use a &lt;code&gt;Dataloader&lt;/code&gt; to group called in one.&lt;/li&gt;
&lt;li&gt;Cache query result (for example in Redis) to serve it later&lt;/li&gt;
&lt;li&gt;Use persisted GraphQL queries&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>graphql</category>
      <category>mongodb</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
