<?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: Ayush Vishwakarma</title>
    <description>The latest articles on DEV Community by Ayush Vishwakarma (@imayush15).</description>
    <link>https://dev.to/imayush15</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%2F966842%2F3cf9fa71-84ee-4d84-9c9d-dc0aef6a5f4c.jpg</url>
      <title>DEV Community: Ayush Vishwakarma</title>
      <link>https://dev.to/imayush15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imayush15"/>
    <language>en</language>
    <item>
      <title>Crafting the Ultimate GraphQL Adventure with Express: Power Up Your API Game!</title>
      <dc:creator>Ayush Vishwakarma</dc:creator>
      <pubDate>Sun, 11 Aug 2024 15:59:39 +0000</pubDate>
      <link>https://dev.to/imayush15/crafting-the-ultimate-graphql-adventure-with-express-power-up-your-api-game-47km</link>
      <guid>https://dev.to/imayush15/crafting-the-ultimate-graphql-adventure-with-express-power-up-your-api-game-47km</guid>
      <description>&lt;p&gt;Hey everyone! As a frontend developer, I've been diving into some backend technologies lately—because let's face it, knowing your way around the backend can save you from any sneaky surprises from your backend developers! 😛&lt;/p&gt;

&lt;p&gt;After getting a good grasp of the basics in the MERN stack, I stumbled upon GraphQL, and I have to say, I'm pretty blown away by its potential. It's a game-changer, and I thought it would be fun to share my insights with you all. Let's explore the magic of GraphQL with express together!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;For people who are fairly new to GraphQL, let me give you a fair introduction to this powerful tool.&lt;/p&gt;

&lt;p&gt;GraphQL, as the &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;official website&lt;/a&gt; puts it, is &lt;em&gt;A query language for your API.&lt;/em&gt; If you've been in the industry for a while, you've likely encountered situations where an API response gives you way more data than you actually need. With a traditional REST API, whether you're using all the keys or just a couple, the entire payload is sent to the client—like getting the entire buffet when all you wanted was a snack.😶‍🌫️&lt;/p&gt;

&lt;p&gt;This can lead to inefficiencies, especially when you're only interested in specific data points for different parts of your application. GraphQL tackles this challenge head-on, allowing you to &lt;strong&gt;request only what you need, no more, no less&lt;/strong&gt;. It's like having a tailor-made suit instead of one-size-fits-all!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Get Our Hands Dirty
&lt;/h2&gt;

&lt;p&gt;We’ll tackle this in two main steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating a GraphQL Server with Apollo Client&lt;/li&gt;
&lt;li&gt;Connecting GraphQL with Express&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By the end, you'll not only have a GraphQL server up and running, but you'll also know how to connect it with Express. So, roll up your sleeves—it's time to turn code into magic!&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating a GraphQL Server with Apollo Client
&lt;/h4&gt;

&lt;p&gt;Let's get started by setting up our GraphQL server. First, we'll lay the groundwork with some essential tools.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize an Empty npm Folder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Begin by initializing an npm project. This sets up your project directory with a &lt;code&gt;package.json&lt;/code&gt; file that will track your 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 init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create a basic &lt;code&gt;package.json&lt;/code&gt; file with all the default settings—kind of like getting the keys to your new coding kingdom.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Express, Apollo Server, Dotenv and GraphQL&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, we'll bring in the core tools: express for our server, Apollo for our GraphQL needs, and GraphQL itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express @apollo/server graphql dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, you're setting up the foundation of your GraphQL server. Think of it as assembling the ingredients for a gourmet meal—each one is essential for the final dish.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install nodemon for Hassle-Free Development&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To make your development process smoother, we’ll add nodemon as dev dependencies, nodemon will watch your files for changes and automatically restart the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nodemon is like having a trusty sidekick who handles the grunt work, so you can focus on coding.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;src&lt;/code&gt; Folder and &lt;code&gt;server.js&lt;/code&gt; File&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let’s set up the structure of our project. Start by creating a &lt;code&gt;src&lt;/code&gt; folder, where all your source code will live. Then, create a &lt;code&gt;server.js&lt;/code&gt; file inside src—this will be the starting point of our application:&lt;/p&gt;

&lt;p&gt;After installing all these dependencies, your &lt;code&gt;package.json&lt;/code&gt; file should look something like this:&lt;/p&gt;

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

&lt;p&gt;With this setup, you're now ready to start building your GraphQL server. Buckle up—this is where the real adventure begins!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In our &lt;code&gt;server.js&lt;/code&gt; file we'll need to import bunch of things as shown below&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Let’s walk through the key lines in our server.js file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Apollo and Server Setup: We import &lt;code&gt;ApolloServer&lt;/code&gt; and &lt;code&gt;startStandaloneServer&lt;/code&gt; to get our GraphQL server rolling, while &lt;code&gt;dotenv&lt;/code&gt; handles our environment variables like a pro.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We initialize an ApolloServer instance, primarily requiring &lt;code&gt;typeDefs&lt;/code&gt; to define the schema. In the provided example, we specify a &lt;code&gt;Query&lt;/code&gt; type named &lt;code&gt;check&lt;/code&gt;, which is of type String and marked as non-nullable, ensuring that the query will always return a valid string.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In addition to &lt;code&gt;typeDefs&lt;/code&gt;, the &lt;code&gt;resolvers&lt;/code&gt; argument is mandatory. It specifies how to handle and resolve the requested queries. In this case, we’ve defined a resolver for the &lt;code&gt;check&lt;/code&gt; query that logs the message &lt;code&gt;checking query&lt;/code&gt; and returns a non-nullable string &lt;code&gt;Checking Query&lt;/code&gt;, consistent with the schema defined in &lt;code&gt;typeDefs&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we start the server. If all goes well, you’ll see a message in the console confirming that your server is up and running!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now when you start the server, you'll be able to see a screen similar to the one shown below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbp0ccbxxzkmjr5czeplq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbp0ccbxxzkmjr5czeplq.png" alt="Apollo Server Image" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Connecting GraphQL with Express
&lt;/h4&gt;

&lt;p&gt;Now that we’ve got our Apollo Server for GraphQL up and running, let’s integrate it with a REST API using Express. It’s a simple tweak, and you’ll be up and running in no time. Follow these steps to make the switch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, we need to bring in &lt;code&gt;express&lt;/code&gt; from express and the &lt;code&gt;expressMiddleware&lt;/code&gt; from Apollo Server. This will allow us to blend the best of both REST and GraphQL worlds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we’ll create an instance of an Express app. This will serve as the backbone for our server, handling both REST routes and our GraphQL endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We’ll call the &lt;code&gt;start&lt;/code&gt; method from the ApolloServer instance. This prepares the server to handle incoming requests when connected to Express.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we’ll create a &lt;code&gt;/graphql&lt;/code&gt; endpoint where our Apollo Server will handle requests using &lt;code&gt;expressMiddleware&lt;/code&gt;. This is where the magic happens—combining the flexibility of GraphQL with the simplicity of Express.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s what your updated &lt;code&gt;server.js&lt;/code&gt; will look like after making these changes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpk2y6s4fgf86mllfnl0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpk2y6s4fgf86mllfnl0a.png" alt="Final Server.js" width="800" height="931"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And just like that, when you navigate to &lt;code&gt;http://localhost:8002/graphql&lt;/code&gt;, you’ll be greeted by the familiar Apollo Server interface, ready to handle your queries.&lt;/p&gt;

&lt;p&gt;Congratulations! You’ve successfully set up a GraphQL server and integrated it with Express. It’s a powerful combo that’s now at your fingertips.&lt;/p&gt;

&lt;p&gt;Feel free to share your thoughts in the comments—I’d love to hear how this worked for you. I hope you found this guide helpful, and maybe even a little fun!&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>mern</category>
      <category>express</category>
    </item>
    <item>
      <title>Caching the React Query Way!</title>
      <dc:creator>Ayush Vishwakarma</dc:creator>
      <pubDate>Sat, 19 Nov 2022 18:44:57 +0000</pubDate>
      <link>https://dev.to/imayush15/caching-the-react-query-way-jkl</link>
      <guid>https://dev.to/imayush15/caching-the-react-query-way-jkl</guid>
      <description>&lt;p&gt;Recently I had a chance to learn &lt;em&gt;react-query&lt;/em&gt;, and after having used &lt;em&gt;Angular&lt;/em&gt; and &lt;em&gt;HttPClient&lt;/em&gt; for API calls for over an year I found this really amazing and easy to use as it provides bunch of different configurable parameters which solves a massive chunk of common problems for which we had to write functions manually.&lt;/p&gt;

&lt;p&gt;A major problem that we all must have faced is CACHING! Traditionally, if we had to cache any data and prevent an API from being called again and again, we needed to store that data somewhere (in &lt;code&gt;BehaviourSubject&lt;/code&gt; or &lt;code&gt;localstorage&lt;/code&gt;, if you work with Angular) and every-time before calling an API we had to check if the data is already stored anywhere, if yes we could just use that or else go ahead and fetch data using API. A small code snippet for the same use case is provided&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Flxnw64p6vg4wmkz8pp4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Flxnw64p6vg4wmkz8pp4n.png" alt="Caching in Angular" width="800" height="935"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above is an example of &lt;code&gt;AppComponent&lt;/code&gt; of angular where we are fetching the Super Heros data using &lt;code&gt;getSuperHeros&lt;/code&gt; function. This function first checks if the data is already stored in a &lt;code&gt;BehaviourSubject&lt;/code&gt;, and if it does it simply assigns the data to the &lt;code&gt;superHeros&lt;/code&gt; variable else it goes ahead and makes an API call to fetch the data and store it in the &lt;code&gt;BehaviourSubject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although there is no issue with the above implementation, one major question that arises is, "for how long the data will be cached?".&lt;/p&gt;

&lt;p&gt;There are different answers for it based on what you use for storing the data.&lt;/p&gt;

&lt;p&gt;If you've used &lt;code&gt;BehaviourSubject&lt;/code&gt; or anything similar to it that persists till the page is reloaded, the data will be cached till page is reloaded.&lt;/p&gt;

&lt;p&gt;If you've used &lt;code&gt;localstorage&lt;/code&gt; that persists even if the page is reloaded, your data will be cached till the time you clear your localStorage. If you want your App to periodically look for changes, you'll have to store an interval along with the cached data and make API calls to see if the data from server has changed. &lt;/p&gt;

&lt;p&gt;Looks a very tedious job... right?&lt;/p&gt;

&lt;p&gt;You can achieve the above functionality with just two properties in react-query.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cacheTime&lt;/li&gt;
&lt;li&gt;staleTime&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before we dive into above properties a little bit about &lt;code&gt;useQuery&lt;/code&gt; a very important hook from react-query&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fy81nurhf67y8vrmmzs38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fy81nurhf67y8vrmmzs38.png" alt="Data Fetching using useQuery" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useQuery
&lt;/h2&gt;

&lt;p&gt;The above image is an basic example of how we fetch data in React using &lt;code&gt;useQuery&lt;/code&gt; hook from react-query.&lt;/p&gt;

&lt;p&gt;useQuery accepts multiple arguments first of which is &lt;code&gt;queryKey&lt;/code&gt; an unique key (which in our case is very important), second is &lt;code&gt;queryFn&lt;/code&gt; a function that is called to actually hit the endpoint, third is an object which contains multiple configurational options such as &lt;code&gt;refetchOnWindowFocus&lt;/code&gt;, &lt;code&gt;cacheTime&lt;/code&gt;, &lt;code&gt;staleTime&lt;/code&gt;, etc...&lt;/p&gt;

&lt;p&gt;It also returns an object which when expanded contains multiple fields of which &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;isLoading&lt;/code&gt;, &lt;code&gt;isFetching&lt;/code&gt;, being the important ones in context to this article&lt;/p&gt;

&lt;h4&gt;
  
  
  - data
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;data&lt;/code&gt; is the same object that is returned from API, it is provided to us by useQuery&lt;/p&gt;

&lt;h4&gt;
  
  
  - isLoading
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;isLoading&lt;/code&gt; is a boolean. It is set to &lt;code&gt;true&lt;/code&gt; when the data fetched from the API is being loaded and set to &lt;code&gt;false&lt;/code&gt; when the data is loaded completely&lt;/p&gt;

&lt;h4&gt;
  
  
  - isFetching
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;isFetching&lt;/code&gt; is a boolean. It is set to &lt;code&gt;true&lt;/code&gt; when the API call starts and set to &lt;code&gt;false&lt;/code&gt; when the API call ends&lt;/p&gt;

&lt;p&gt;Now let's have a look on the two properties that is used to achieve caching&lt;/p&gt;

&lt;h2&gt;
  
  
  cacheTime
&lt;/h2&gt;

&lt;p&gt;cacheTime property accepts time(in ms) which determines how long the data from the API will be cached. By default it is set to &lt;code&gt;5 mins&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In the above example, when we land on the App for the &lt;em&gt;first&lt;/em&gt; time the following steps takes place in order to show the data&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useQuery hooks triggers &lt;code&gt;superHeroFetcher&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isFetching&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isLoading&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;API call takes place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt; is populated&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isFetching&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isLoading&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and then we're able to see the data fetched from API on screen.&lt;/p&gt;

&lt;p&gt;But if we visit the App for the &lt;em&gt;second&lt;/em&gt; time the following steps takes place&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useQuery hooks triggers &lt;code&gt;superHeroFetcher&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isFetching&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isLoading&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;API call takes place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt; is populated&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isFetching&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isLoading&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See the difference?&lt;/p&gt;

&lt;p&gt;Since the data is cached, using the unique key (&lt;code&gt;queryKey&lt;/code&gt;). &lt;code&gt;isLoading&lt;/code&gt; property is never set to &lt;code&gt;true&lt;/code&gt; and we won't see a Loading screen. However, an API call is still made and &lt;code&gt;isFetching&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt; and then to &lt;code&gt;false&lt;/code&gt; and if there is any change in data the updated data object is returned.&lt;/p&gt;

&lt;p&gt;To prevent this extra API call, &lt;code&gt;staleTime&lt;/code&gt; is the property that is to be used&lt;/p&gt;

&lt;h2&gt;
  
  
  staleTime
&lt;/h2&gt;

&lt;p&gt;staleTime property accepts time(in ms) too and it determines for how long the data that is cached will be stale. By default it is set to &lt;code&gt;0&lt;/code&gt; which means everytime you visit a page the API call will be triggered. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F80nn07signo9kpjjq70t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F80nn07signo9kpjjq70t.png" alt="cacheTime and staleTime in use" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image both &lt;code&gt;staleTime&lt;/code&gt; and &lt;code&gt;cacheTime&lt;/code&gt; are set to 3 mins each.&lt;/p&gt;

&lt;p&gt;Cheers!🥂&lt;/p&gt;

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