<?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>Forem: Danko Šimunović</title>
    <description>The latest articles on Forem by Danko Šimunović (@danko_simunovic).</description>
    <link>https://forem.com/danko_simunovic</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%2F736232%2F151d7b39-3157-4d86-a933-5619424b79d8.jpeg</url>
      <title>Forem: Danko Šimunović</title>
      <link>https://forem.com/danko_simunovic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/danko_simunovic"/>
    <language>en</language>
    <item>
      <title>Setup React and Webpack manually</title>
      <dc:creator>Danko Šimunović</dc:creator>
      <pubDate>Sun, 02 Jan 2022 20:38:38 +0000</pubDate>
      <link>https://forem.com/danko_simunovic/setup-react-and-webpack-manually-hpc</link>
      <guid>https://forem.com/danko_simunovic/setup-react-and-webpack-manually-hpc</guid>
      <description>&lt;p&gt;If you are using React, you are probably also using &lt;strong&gt;create-react-app&lt;/strong&gt;, which is a great way for setting up a new React project in a fast and easy manner. It hides away all the build process config, so you can focus on writing code immediately. This way you don't have to worry about configuring Webpack, Babel, and the other build tools. But doing this manually can be very beneficial for learning purposes, so let's see a simple way to set up your project manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project initialization
&lt;/h2&gt;

&lt;p&gt;Let's create a directory and initialize npm and git.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir react-app
cd react-app
npm init
git init .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Our folder structure will look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT-APP
└───src
│   └───App.js
│   └───index.js
|   └───index.html
└───package-lock.json
└───package.json
└───webpack.config.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So we will create all the needed files and directories.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
cd src
touch App.js
touch index.html
touch index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then, we need to install React runtime dependencies.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  React application setup
&lt;/h2&gt;

&lt;p&gt;We will add content to the files in the &lt;code&gt;src&lt;/code&gt; folder, so we have a working React application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you open&lt;code&gt;index.html&lt;/code&gt; in the browser, it will be blank. Reason for this is that in &lt;code&gt;App.js&lt;/code&gt; file we are using JSX when we write: &lt;code&gt;return &amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;;&lt;/code&gt;. The browser does not understand this syntax, so it needs to be transformed from JSX code into regular JavaScript. For this purpose, we use the Babel compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Babel setup
&lt;/h2&gt;

&lt;p&gt;First, we will install Babel core and CLI packages locally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev @babel/core @babel/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Use React preset
&lt;/h3&gt;

&lt;p&gt;We also need to install and configure Babel to use presets, which will enable transforms for React. Let's install the required preset.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @babel/preset-react --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To configure Babel we will create a &lt;code&gt;babel.config.json&lt;/code&gt; configuration file in the project root.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch babel.config.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Inside the config file, we will define which presets we want to use.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": ["@babel/preset-react"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Testing the compiled code
&lt;/h3&gt;

&lt;p&gt;After we run &lt;code&gt;babel src -d dist&lt;/code&gt; compiled code will be located inside the &lt;code&gt;dist&lt;/code&gt; folder. To use the compiled code, we need to reference compiled &lt;code&gt;index.js&lt;/code&gt; file in &lt;code&gt;index.html&lt;/code&gt; file.  To do this we will add &lt;code&gt;&amp;lt;script src="../dist/index.js" /&amp;gt;&lt;/code&gt;.  If we examine the code compiled by Babel, we will see that JSX syntax is compiled to valid JavaScript code. &lt;/p&gt;

&lt;p&gt;One thing worth noting is that we are using ES modules. Since modern browsers have started to support module functionality natively, our application should work out of the box. But if we open &lt;code&gt;index.html&lt;/code&gt; in the browser, the first problem that we will encounter is that the browser does not recognize &lt;code&gt;index.js&lt;/code&gt; as a module, therefore we get an error saying &lt;code&gt;Uncaught SyntaxError: Cannot use import statement outside a module&lt;/code&gt;. To fix this we need to include &lt;code&gt;type="module"&lt;/code&gt; in the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; element, to declare this script as a module. Our script element will look like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="module" src="../dist/index.js" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This should help, right? Not really. If we try to run the page again, we will encounter the second problem: the browser is complaining that the React module relative reference is not valid. This is because the browser accepts only one kind of module specifier in an import statement: a URL, which must be either fully-qualified or a path starting with &lt;code&gt;/&lt;/code&gt;, &lt;code&gt;./&lt;/code&gt; or &lt;code&gt;../&lt;/code&gt;.  One possible solution would be to use the relative path to React module located in &lt;code&gt;node_modules&lt;/code&gt; folder. But again, we face another problem. If you check the &lt;code&gt;react&lt;/code&gt; folder you can see that React currently only supports &lt;strong&gt;UMD&lt;/strong&gt; and &lt;strong&gt;CommonJS&lt;/strong&gt; modules. At this point, we would like to find some solution that would allow us not to worry about module formats of the dependencies and how to import them. Let's see what Webpack brings to the table and what problems it is trying to solve.&lt;/p&gt;
&lt;h2&gt;
  
  
  Webpack setup
&lt;/h2&gt;

&lt;p&gt;Webapck bundles all the required imports into one JavaScript file to be used on the client side. This is why we call it a bundler.  Since all modules are contained in one namespace, it resolves all dependency and module format problems for us. Other important features, that are worth mentioning are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tree shaking mechanism&lt;/strong&gt; - it can eliminate code that’s not used and imported by any other module.
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Code-Splitting&lt;/strong&gt; - it can create multiple bundles that can be dynamically loaded at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To start using Webpack, we first need to install the required packages:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install webpack webpack-cli --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We are installing 2 packages:  main Webpack package and &lt;strong&gt;webpack-cli&lt;/strong&gt; for running Webpack commands.&lt;/p&gt;

&lt;p&gt;Next up, let's add Webpack configuration file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch webpack.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will start with the basic configuration:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;What's happening here? First, we are defining an entry point of an application. This is the point from which Webpack starts the bundling process and builds the dependency tree. In our case, the entry point will be &lt;code&gt;index.js&lt;/code&gt; file. Also, we are defining the output path for the bundled file. We will use &lt;code&gt;dist&lt;/code&gt; folder as the output path. &lt;/p&gt;

&lt;p&gt;Since we have the basic configuration set up, we can build the application with Webpack CLI. We can use &lt;code&gt;webpack build&lt;/code&gt; command, but since this is the default command, we can use only &lt;code&gt;webpack&lt;/code&gt;. But if we try to run this command Webpack will output something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Module parse failed: Unexpected token (5:16)
You may need an appropriate loader to handle this file type, currently, no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import App from "./App";
| 
&amp;gt; ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById("app"));
| 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Webpack is telling us that it does not recognize JSX syntax and that it needs something called loader to handle it properly. So, let's see how to do this.&lt;/p&gt;
&lt;h3&gt;
  
  
  Babel loader setup
&lt;/h3&gt;

&lt;p&gt;Out of the box, Webpack only understands JavaScript and JSON files. Loaders allow Webpack to understand other file types. For JSX files we will use Babel loader. We have already installed and used Babel core package and presets. Now we need to install the loader.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install babel-loader --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then we can modify Webpack configuration to start using Babel loader. The configuration file will look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Since &lt;strong&gt;production&lt;/strong&gt; mode minifies the code by default, we will use &lt;strong&gt;development&lt;/strong&gt; mode because of output readability. These are the explanations for some of the other properties that are used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt; identifies which file or files should be transformed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exclude&lt;/code&gt; identifies which modules should be excluded&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;use&lt;/code&gt; indicates which loader should be used to do the transforming&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;presets&lt;/code&gt; is a list of presets that should be used&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Webpack should be satisfied now and will run the build command successfully. If we take look at the output bundle, we can see that Webpack packaged our app modules and React modules in one file. Now we can use this bundle in &lt;code&gt;index.html&lt;/code&gt; by adding the script tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="../dist/main.js" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you open the &lt;code&gt;index.html&lt;/code&gt; file in the browser now, you will see that *&lt;em&gt;Hello World *&lt;/em&gt; message is displayed. This means our application is up and running. That's sweet 😌 . Let's see some ways that we can optimize the build process.&lt;/p&gt;
&lt;h3&gt;
  
  
  HtmlWebpackPlugin setup
&lt;/h3&gt;

&lt;p&gt;Right now, we are including the bundle in the &lt;code&gt;index.html&lt;/code&gt; file manually. This is enough for our app to run. But in real world applications, we could use code splitting that would produce multiple bundles, or we could even hash bundle file names for caching purposes. It would be a tedious process to include them manually in our &lt;code&gt;index.html&lt;/code&gt; every time the bundles are produced. So we will automate this process by using &lt;strong&gt;HtmlWebpackPlugin&lt;/strong&gt;. Plugins are third party packages that can be used with Webpack to extend its functionality. In this case, we are using HtmlWebpackPlugin. First, let's install it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install html-webpack-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And then modify the configuration file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;After running the build command, you will notice that now there is also &lt;code&gt;index.html&lt;/code&gt; file included in &lt;code&gt;dist&lt;/code&gt; folder. And the most important thing, &lt;code&gt;main.js&lt;/code&gt; script tag is automatically injected. This means that we can remove the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag from &lt;code&gt;src/index.html&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Development server setup
&lt;/h3&gt;

&lt;p&gt;Currently, we are manually building the bundle after every change and opening the &lt;code&gt;index.html&lt;/code&gt; to see the effects in the browser. This of course is not the ideal solution for the development environment and it would be best if we could automatize these steps. Webpack offers a special package called &lt;strong&gt;webpack-dev-server&lt;/strong&gt; that acts as a development server and supports live reloading. This way, we will be able to host our bundle and any change in the code will cause reload of our application in the browser.&lt;/p&gt;

&lt;p&gt;The important thing to mention here is that the devserver is creating a separate JavaScript bundle and output in the memory. It will monitor the dependencies of the entry point defined in the Webpack configuration, and re-create the output when changes are detected. We will be using this output when serving the application in the development environment, and not the output that was created by Webpack CLI. First, let's install the required package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install webpack-dev-server --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, we need to configure dev-server in the Webpack configuration file: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;This is the basic configuration that will allow us to host the application locally. First, we define the &lt;code&gt;port&lt;/code&gt; on which the server will run. After that, we set &lt;code&gt;open&lt;/code&gt; property to &lt;code&gt;true&lt;/code&gt;, which means that dev server will open the application in the default browser after the server had been started. We start the browser with &lt;code&gt;webpack serve&lt;/code&gt; command. &lt;br&gt;
The application will be opened in the browser and any changes in the code will appear automagically and instantly in the browser. Remember, the dev server is serving in-memory output, so even if you clear the contents of the output folder, the page will still run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we have covered the basics of the Webpack ecosystem. We have seen how to initialize a React project from scratch and how to use Babel transpiler. Also, we have learned about Webpack loaders, plugins, and how to use Webpack dev server. Of course, these are jut the basics, and there are a lot more concepts to learn about Webpack ecosystem. I will cover some of them in the next posts.&lt;/p&gt;

&lt;p&gt;You can check out the example repo  &lt;a href="https://github.com/sdanko/react-app"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Let’s Talk About Executing and Caching Queries with React Apollo</title>
      <dc:creator>Danko Šimunović</dc:creator>
      <pubDate>Wed, 27 Oct 2021 16:45:31 +0000</pubDate>
      <link>https://forem.com/infobipdev/lets-talk-about-executing-and-caching-queries-with-react-apollo-15f1</link>
      <guid>https://forem.com/infobipdev/lets-talk-about-executing-and-caching-queries-with-react-apollo-15f1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HpTjgPLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ynlmycoph96dp9tem4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HpTjgPLb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13ynlmycoph96dp9tem4.jpg" alt="Cover photo" width="880" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yep, you’ve guessed it. We’re going to talk about queries. &lt;/p&gt;

&lt;p&gt;Let’s start with basics. The &lt;code&gt;useQuery&lt;/code&gt; React hook is the primary API for executing queries when using Apollo Client in React. To run a query within a React component, we call the &lt;code&gt;useQuery&lt;/code&gt;  and pass it a GraphQL query string. When the component renders, &lt;code&gt;useQuery&lt;/code&gt;  returns an object from Apollo Client that contains &lt;code&gt;loading&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt;, and &lt;code&gt;data&lt;/code&gt; properties. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Apollo Query component is a deprecated way of executing queries. It’s a part of &lt;code&gt;@apollo/react-components&lt;/code&gt; package and it is implementing Render props pattern. Render props are used for sharing code between React components using a prop whose value is a function. A component with a render prop takes a function that returns a React element. The component then calls this function instead of implementing its own render logic. In the case of Query component, we are using the &lt;strong&gt;children&lt;/strong&gt; prop as a render prop. But because the children prop doesn’t need to be named in the list of “attributes”,  you can put it directly inside the element. In the following example, we are passing a query prop, which is required. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Internally, Query component actually uses the &lt;code&gt;useQuery&lt;/code&gt; hook and then calls the render props function with results returned from the hook. This means that we’re using the same options and are getting the same result object as when using the &lt;code&gt;useQuery&lt;/code&gt; hook. &lt;/p&gt;

&lt;h3&gt;
  
  
  Cache-first, Ask Questions Later
&lt;/h3&gt;

&lt;p&gt;Apollo is locally caching results for the queries by default. This makes subsequent executions of the same query extremely fast. This is called &lt;strong&gt;cache-first&lt;/strong&gt; policy. We can define fetch policy on query level.  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;These are 6 supported fetch policies: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;cache-first&lt;/strong&gt;: This is the default fetch policy. If data is present in the cache, that data is returned. Otherwise, a query is executed against the GraphQL server and the data is returned after it is cached. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;cache-only&lt;/strong&gt;: Query is only executed against the cache. GraphQL server is never called. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;cache-and-network&lt;/strong&gt;: If data is present in the cache, that data is returned. But even then, a query is executed against the GraphQL server. If the response differs from what is stored in the cache, it will update the cache. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;network-only&lt;/strong&gt;:  Query is executed against the GraphQL server, without first checking the cache. The query's result is stored in the cache in case the same query with a different fetch policy is being made elsewhere in the application. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;no-cache&lt;/strong&gt;: Behaves like network-only, except the query's result is not stored in the cache. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;standby&lt;/strong&gt;: Behaves like cache-first, except this query does not automatically update when underlying field values change. You have to update it manually. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also a possibility to define &lt;strong&gt;nextFetchPolicy&lt;/strong&gt; option. This way, you can define fetch policy for the first query execution using &lt;strong&gt;fetchPolicy&lt;/strong&gt; and then you can define fetch policy for the subsequent executions with &lt;strong&gt;nextFetchPolicy&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Apollo Client Behind the Scenes
&lt;/h3&gt;

&lt;p&gt;Apollo Client acts as a facade to the underlying cached data. Behind the scenes, Apollo normalizes the data by splitting the results into individual objects and assigns a unique identifier to each object. These objects are stored in a flattened structure. Apollo then exposes an API to interact with the cached data. By minimizing direct access to the actual data using the facade/API, Apollo can normalize data under the hood. &lt;/p&gt;

&lt;p&gt;Apollo can &lt;strong&gt;automatically update cache&lt;/strong&gt; for: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries &lt;/li&gt;
&lt;li&gt;Mutations that update a single existing entity &lt;/li&gt;
&lt;li&gt;Bulk update mutations that return the entire set of changed items &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, there are use cases in which we have to &lt;strong&gt;manually update the cache&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application-specific side-effects (something that happens after the mutation, but does not use data returned from the mutation) &lt;/li&gt;
&lt;li&gt;Update operations that add, remove, or reorder items in a cached collection &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Manually Updating Cached Data
&lt;/h3&gt;

&lt;p&gt;Apollo supports multiple ways of reading and writing cached data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;readQuery / writeQuery&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;readFragment / writeFragment&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cache.modify&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With &lt;code&gt;readQuery&lt;/code&gt; method, it is possible to run GraphQL queries directly on the local cache. If the cache contains all of the data necessary to execute a specified query, &lt;code&gt;readQuery&lt;/code&gt; returns a data object in the shape of the query, just like a GraphQL server does.  If some fields are missing from the cache, &lt;code&gt;null&lt;/code&gt; is returned. Using &lt;code&gt;writeQuery&lt;/code&gt; we can write arbitrary data to the cache for the specific query. It looks similar to &lt;code&gt;readQuery&lt;/code&gt;, but it accepts &lt;code&gt;data&lt;/code&gt; option. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Using fragments it is possible to read or update only parts of the cached data, unlike &lt;code&gt;readQuery / writeQuery&lt;/code&gt; methods, which require a complete query. When using fragments to interact with cache, we can use &lt;code&gt;readFragment / writeFragment&lt;/code&gt; methods. They require &lt;code&gt;id&lt;/code&gt; option, which represents the unique identifier that was assigned to the object in the cache.  By default, this identifier has the format &lt;code&gt;&amp;lt;_typename&amp;gt;:&amp;lt;id&amp;gt;&lt;/code&gt;, but this can be customized. If there is no object with the specified ID, &lt;code&gt;readFragment&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;.  &lt;code&gt;writeFramgent&lt;/code&gt; accepts &lt;code&gt;data&lt;/code&gt; option, which represents data that will be written for the specified object and that conforms to the specified fragment. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;With &lt;code&gt;cache.modify&lt;/code&gt; it is possible to directly modify cached fields. This method requires the ID of the cached object to modify and a modifier function for each field to modify. &lt;/p&gt;

&lt;p&gt;It is important to emphasize that any changes made with these methods are not pushed to the GraphQL server. If the current page is refreshed, these changes will disappear. Also, all methods trigger a refresh of all active queries that depend on modified fields. &lt;/p&gt;

&lt;h3&gt;
  
  
  Two Strategies for Updating the Cached Results
&lt;/h3&gt;

&lt;p&gt;Besides manually rewriting cached data, there are two strategies for updating the cached results: &lt;strong&gt;polling&lt;/strong&gt; and &lt;strong&gt;refetching&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;With polling, we execute the query periodically at a specified interval. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Refetching is done by using &lt;code&gt;refetch&lt;/code&gt; function that enables us to re-execute the query.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Apollo Client is a Powerful Caching Machine
&lt;/h3&gt;

&lt;p&gt;In conclusion, one would have to agree that Apollo Client is a mighty caching machine. It’s equipped with a powerful caching mechanism that makes it  easy to execute data queries quickly and efficiently. However, to make better use of its caching possibilities, one should get familiar with various methods of interacting with cache, cache setup and configuration. &lt;/p&gt;

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