<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saloni Kathuria on Medium]]></title>
        <description><![CDATA[Stories by Saloni Kathuria on Medium]]></description>
        <link>https://medium.com/@kathuria.saloni29?source=rss-997c6f5269------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*voprUbgNdkoE2UWmoI5K_A.jpeg</url>
            <title>Stories by Saloni Kathuria on Medium</title>
            <link>https://medium.com/@kathuria.saloni29?source=rss-997c6f5269------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:18:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kathuria.saloni29/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unleashing GraphQL Type-Based Mocking]]></title>
            <link>https://medium.com/@kathuria.saloni29/unleashing-graphql-type-based-mocking-d7968ae494d3?source=rss-997c6f5269------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7968ae494d3</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[mocking]]></category>
            <category><![CDATA[graphql-schema]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unittest]]></category>
            <dc:creator><![CDATA[Saloni Kathuria]]></dc:creator>
            <pubDate>Tue, 10 Oct 2023 06:56:35 GMT</pubDate>
            <atom:updated>2023-10-11T06:50:11.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VqAsnjoDcwFvSzCN" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Developing GraphQL APIs often involves creating and testing the API’s functionality, and one essential aspect of this process is generating mock data. Mock data helps developers build and test GraphQL queries and mutations without relying on a real database or production data. In this article, we’ll explore how to use <strong>graphql-codegen-typescript-mock-data </strong>to easily generate mock data based on your GraphQL schema.</p><p><strong>Understanding GraphQL Schemas</strong></p><p>A GraphQL schema defines the types and operations available in your API. It includes information about queries, mutations, and the data types that can be retrieved or manipulated. The schema acts as a contract between the client and server, ensuring that clients request only valid data.</p><p><strong>Generating Mock Data with graphql-codegen-typescript-mock-data</strong></p><p>graphql-codegen-typescript-mock-data is a powerful tool that works in conjunction with graphql-codegen. It generates TypeScript types and mock data for your GraphQL schema, making it easier to develop and test your API.</p><p>Steps to get started with graphql-codegen-typescript-mock-data:</p><p>1. <strong><em>Install Dependencies</em></strong></p><pre>npm install - save-dev @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-mock-data graphql</pre><p><em>2. </em><strong><em>Creating a GraphQL Schema</em></strong></p><p>To get started, you need a GraphQL schema for your API. You can define your schema using the GraphQL Schema Definition Language (SDL) or a programmatic approach, depending on your preferences. Here’s a simple example of an SDL-based schema:</p><pre>type Post {<br>  id: ID!<br>  title: String!<br>  content: String!<br>}<br><br>type Query {<br>  getPosts: [Post!]!<br>}</pre><p>With this schema, you can query for a list of posts. Now, let’s see how to use graphql-codegen-typescript-validation-schema to generate TypeScript types and mock data based on this schema.</p><p>3.<strong><em> Configure Code Generation</em></strong></p><pre>schema: ./src/schema.ts<br>require:<br>  - ts-node/register/transpile-only<br>generates:<br>  src/generated-types.ts:<br>    plugins:<br>      - typescript<br>      - typescript-resolvers<br>  src/generated-mocks.ts:<br>    plugins:<br>      - typescript-mock-data:<br>          typesFile: &#39;./generated-types.ts&#39;   </pre><p>This configuration specifies the schema path and the output file for the generated TypeScript code. Also generates mock data using typescript-mock-data plugin.</p><p>The configuration has the following fields:</p><blockquote><strong><em>schema : </em></strong>specifies the path to the local/ graphql schema endpoint</blockquote><blockquote><strong><em>generates</em></strong>: A map where the key represents an output path for the generated code, and the value represents a set of relevant options for that specific file. Below are the possible options that can be specified.</blockquote><ul><li>Generating types in output file src/generated-types.ts , using plugins typescript and typescript-resolver .</li><li>Generating mocks from the types in the putput file src/generated-mocks.ts , using the plugin typescript-mock-data</li></ul><p>You can understand more about configuration options here in the documentation. <a href="https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config#configuration-options">https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config#configuration-options</a></p><p>4. <strong><em>Run the command to generate types and mock data</em></strong></p><pre>npm run gql-gen</pre><p>Once you run this command it will create 2 files one with generated types and other with generated mocks using the types.</p><p>The generated mock data file looks like this</p><pre>export const aPost = (overrides?: Partial&lt;Post&gt;): Post =&gt; {<br>  return {<br>    id: overrides &amp;&amp; overrides.hasOwnProperty(&#39;id&#39;) ? overrides.id! : &#39;0550ff93-dd31-49b4-8c38-ff1cb68bdc38&#39;,<br>    title: overrides &amp;&amp; overrides.hasOwnProperty(&#39;title&#39;) ? overrides.title! : &#39;aliquid&#39;,<br>    content: overrides &amp;&amp; overrides.hasOwnProperty(&#39;content&#39;) ? overrides.title! : &#39;rerum&#39;, <br>  };<br>};</pre><p>5. <strong><em>Use Mock Data</em></strong></p><p>You can use the generated mock data in your tests, development environment, or anywhere else you need mock data.</p><p>Here we will see the usage of it in unit testing:</p><pre>const { aPost } from &#39;src/generated-mocks&#39;;<br><br>const post = aPost({ title: &#39;Hello World&#39; });<br><br>// will create a post object with `title` property overridden to `Hello World`</pre><p>The mockPost() function will provide you with mock data for a Post type:</p><pre>{<br>  &quot;id&quot;: &quot;0550ff93-dd31-49b4-8c38-ff1cb68bdc38&quot;,<br>  &quot;title&quot;: &quot;Hello World&quot;,<br>  &quot;content&quot;: &quot;rerum&quot;<br>}</pre><p>Generating mock data for your GraphQL API is crucial for effective development and testing. With tools like graphql-codegen-typescript-mock-data, you can streamline the process by automatically generating TypeScript types and mock data based on your GraphQL schema. This simplifies development, reduces reliance on real data sources, and ensures consistent and reliable testing for your GraphQL API.</p><p>Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7968ae494d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reset your ErrorBoundary]]></title>
            <link>https://medium.com/@kathuria.saloni29/reset-your-error-boundary-a4bef36b72d0?source=rss-997c6f5269------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4bef36b72d0</guid>
            <category><![CDATA[error-boundaries]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-error-boundary]]></category>
            <dc:creator><![CDATA[Saloni Kathuria]]></dc:creator>
            <pubDate>Sat, 10 Jun 2023 18:09:15 GMT</pubDate>
            <atom:updated>2023-06-21T10:28:21.728Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sTAnDTMQdOq7KvIk" /><figcaption>Photo by <a href="https://unsplash.com/@brett_jordan?utm_source=medium&amp;utm_medium=referral">Brett Jordan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Error boundaries</strong> are a powerful feature in React that allow developers to catch and <strong>handle JavaScript errors gracefully</strong>, preventing the entire application from crashing. However, sometimes you may encounter situations where you need to reset an error boundary and recover from an error state. In this blog post, we will explore how to reset an error boundary in React and regain control over error handling.</p><p>Before we dive into resetting error boundaries let us quickly recap what error boundaries are.</p><p><strong>What is an Error Boundary?</strong></p><blockquote>Error boundaries work like a JavaScript catch {} block, but for components. They are React components that implement the componentDidCatch lifecycle method. By defining this method, you can catch errors that occur within the component&#39;s tree and display a fallback UI instead of the error propagating to the entire application.</blockquote><p>By default, error boundaries will catch errors in the rendering phase. However, they do not catch errors in event handlers, async code (e.g., setTimeout), or during server-side rendering. When an error is caught by an error boundary, React will call the componentDidCatch method, allowing you to handle the error appropriately.</p><p><strong>Resetting an Error Boundary</strong></p><p>Resetting an Error Boundary becomes necessary in certain scenarios. For example, imagine a form where user is filling up the data and the form has some validation rules. After the user has entered information if application runs into an error because of an invalid input, it would be beneficial to reset the error boundary after displaying an error message. This allows the user to correct the input and submit the form again without having to refresh the page or start from scratch.</p><p>To reset an Error Boundary, you need to manipulate its internal state.</p><pre>class MyErrorBoundary extends React.Component {<br>     constructor(props) {<br>       super(props);<br>       this.state = { hasError: false };<br>       this.resetErrorBoundary = this.resetErrorBoundary.bind(this);<br>     }<br>   <br>     componentDidCatch(error, errorInfo) {<br>       this.setState({ hasError: true });<br>     }<br>   <br>     resetErrorBoundary() {<br>       this.setState({ hasError: false });<br>     }<br>   <br>     render() {<br>       if (this.state.hasError) {<br>         return (<br>           &lt;div&gt;<br>             &lt;h1&gt;Something went wrong!&lt;/h1&gt;<br>             &lt;button onClick={this.resetErrorBoundary}&gt;Reset&lt;/button&gt;<br>           &lt;/div&gt;<br>         );<br>       }<br>   <br>       return this.props.children;<br>     }<br>   }</pre><p>Steps to follow:</p><ol><li>Create a state variable within your error boundary component to track the error state. For example, you can define a hasError state variable and initialise it to false.</li><li>In the componentDidCatch method of the error boundary component, set the hasError state variable to true. This indicates that an error has occurred.</li><li>In render function return an appropriate fallback UI to notify the user of the error only when the state hasError is true.</li><li>To reset the state, add a Reset button in the fallback UI.</li><li>Implement the reset function like resetErrorBoundary defined in the above mentioned code. This function should set the hasError state variable back to false and perform any necessary cleanup or reset actions.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*wLBbQaJKdKSkfGspR2L9PQ.gif" /><figcaption>Reset Error Boundary</figcaption></figure><p><strong>Update Error boundary from outside</strong></p><p>To update the error boundary state from outside, you can make use of a React Context. There can be other ways also.</p><p>Here’s an example using React context:</p><ol><li>Create an error context to manage the error state and provide a method to update it.</li></ol><pre>// Error Context Provider<br>import React, { createContext, useState } from &quot;react&quot;;<br><br>const ErrorContext = createContext({<br>  hasError: false,<br>  updateError: () =&gt; {}<br>});<br><br>const ErrorContextProvider = ({ children }) =&gt; {<br>  const [hasError, setHasError] = useState(false);<br><br>  const updateError = (value) =&gt; {<br>    setHasError(value);<br>  };<br><br>  return (<br>    &lt;ErrorContext.Provider value={{ hasError, updateError }}&gt;<br>      {children}<br>    &lt;/ErrorContext.Provider&gt;<br>  );<br>};<br><br>export { ErrorContext, ErrorContextProvider };</pre><p>2. In the error boundary component, access the error context and update the error state when an error occurs.</p><pre>// Error Context Provider<br>import React from &quot;react&quot;;<br>import { ErrorContext } from &quot;./ErrorContextProvider&quot;;<br><br>class ErrorBoundary extends React.Component {<br>  static contextType = ErrorContext;<br><br>  componentDidCatch(error, errorInfo) {<br>    this.context.updateError(true);<br>  }<br><br>  render() {<br>    if (this.context.hasError) {<br>      return (<br>        &lt;div&gt;<br>          &lt;h1&gt;Something went wrong!&lt;/h1&gt;<br>        &lt;/div&gt;<br>      );<br>    }<br><br>    return this.props.children;<br>  }<br>}<br><br>export default ErrorBoundary;</pre><p>3. Wrap your application or the relevant portion of the component tree with the ErrorContextProvider.</p><pre>import { createRoot } from &quot;react-dom/client&quot;;<br>import { ErrorContextProvider } from &quot;./ErrorContextProvider&quot;;<br><br>import App from &quot;./App&quot;;<br><br>const rootElement = document.getElementById(&quot;root&quot;);<br>const root = createRoot(rootElement);<br><br>root.render(<br>  &lt;ErrorContextProvider&gt;<br>    &lt;App /&gt;<br>  &lt;/ErrorContextProvider&gt;<br>);</pre><p>4. From a higher-level component or an event handler outside the error boundary component hierarchy, update the error state by accessing the updateError method from the error context. TextField internally throws an error.</p><pre>import &quot;./styles.css&quot;;<br>import { TextField } from &quot;./TextField&quot;;<br>import ErrorBoundary from &quot;./ErrorBoundary&quot;;<br>import { useContext } from &quot;react&quot;;<br>import { ErrorContext } from &quot;./ErrorContextProvider&quot;;<br><br>export default function App() {<br>  const { updateError } = useContext(ErrorContext);<br>  const handleReset = () =&gt; {<br>    updateError(false);<br>  };<br><br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;div&gt;<br>        &lt;button onClick={handleReset}&gt;Reset boundary&lt;/button&gt;<br>      &lt;/div&gt;<br>      &lt;ErrorBoundary&gt;<br>        &lt;TextField /&gt; <br>      &lt;/ErrorBoundary&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*j_mLL_6kHvfInASLtNRXWA.gif" /><figcaption>Reset error boundary from outside</figcaption></figure><p><em>Codesandbox : </em><a href="https://codesandbox.io/s/reset-error-boundary-wk86wj">https://codesandbox.io/s/reset-error-boundary-wk86wj</a></p><p>Hope this helps. Happy coding! :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4bef36b72d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Lazy Loading React Component is important for Large Applications ?]]></title>
            <link>https://medium.com/@kathuria.saloni29/why-lazy-loading-react-component-is-important-for-large-applications-f42cf23065e4?source=rss-997c6f5269------2</link>
            <guid isPermaLink="false">https://medium.com/p/f42cf23065e4</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[lazy-load]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Saloni Kathuria]]></dc:creator>
            <pubDate>Wed, 29 Jul 2020 10:49:34 GMT</pubDate>
            <atom:updated>2020-08-12T09:23:24.732Z</atom:updated>
            <content:encoded><![CDATA[<h4>React / Lazy and Suspense</h4><h4>And how it impacts application performance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8cOkzNVV6mPDSf_G2sIAfQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@oshomah?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Oshomah Abubakar</a> on <a href="https://medium.com/s/photos/box-load-truck?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><strong>What is Lazy loading ?</strong></p><p>You all must be already familiar with the term <strong>Lazy Loading,</strong> if not, it’s a common asynchronous loading pattern where you defer loading a resource until it is actually needed.<br>In React v16.6, some interesting <a href="https://reactjs.org/blog/2018/10/23/react-v-16-6.html">features</a> were introduced, couple of them being React.lazy() and React.Suspense , to power your application with component or route based code-splitting.</p><blockquote><strong>Note: </strong>React.lazy and Suspense are not yet available for server-side rendering (v16.13.0). If you want to do code-splitting in a server rendered app, we recommend <a href="https://github.com/gregberge/loadable-components">Loadable Components</a>. It has a nice <a href="https://loadable-components.com/docs/server-side-rendering/">guide for bundle splitting with server-side rendering</a>.</blockquote><p><strong>Why is it needed?</strong></p><p>JavaScript has evolved tremendously over the last few years. With W<a href="https://webpack.js.org/">ebpack</a>, B<a href="http://browserify.org/">rowserify</a>, g<a href="https://gruntjs.com/">runt</a> and many more bundlers, you can achieve <strong>code-splitting</strong> by breaking your one large bundle into different bundles (or chunks) and loading them individually, instead of all at once, to reduce the impact on page load time.</p><p>With time, your application would grow and it could lead to increase in <strong>bundle size </strong>and could in turn impact <strong>page load time</strong>.</p><p><strong>Let’s jump into the code to understand this.</strong></p><blockquote>Here, at OLX, users can post ads for items (like car, phone etc) on our platform and other users can buy it. To ease this process, we came up with a one-click solution where users can show their interest towards an item by &quot;Making an Offer&quot;.</blockquote><p>Suppose, you have a MakeOffer component which renders when user decides to make an offer for a posted item. In the scenario below, during the initial load, the MakeOffer component will be part of the bundle.</p><p>During the user journey user might not make an offer right away or there could be a case when user may never makes an offer. Even then this component will be a part of your bundle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z9LkoOilS2bTapa-He2WHw.png" /></figure><p>But you must be thinking, what issues this might create? This seems be good so far! No?</p><p>It does seem good for smaller applications. But when the application scales and feature set increases; with its implementation <strong>the bundle size increases</strong> as well, eventually <strong>increasing the page load time</strong>. Furthermore, users with low end devices and slower network adds on to the problem.</p><p>This is where React.Lazy and Suspense come to the rescue!</p><p><strong>Let’s fix the current scenario by using this amazing feature.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mv7uEMzyqnMjJW9a6lAy4w.png" /></figure><p>We have created an Async MakeOffer component. And while the component is getting fetched, <strong>suspense</strong> helps you add a <strong>loading</strong> state to your view. Now, let’s use AsyncMakeOffer component instead of the other one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJj9jYMEp930H8sXKwXTbQ.png" /></figure><p>Voila!! This is it. This way the MakeOffer component will only be fetched when the canMakeOffer state is set i.e., only when the component is required.</p><p><strong>To check how does it work ?</strong></p><p>Gets downloaded as a separated bundle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ja_P2rjwaAR7Qrh-vwaY6Q.png" /></figure><p><strong>Benefits of using these components:</strong></p><ul><li>Trimmed initial bundle size.</li><li>Better initial page load time.</li><li>Better performance on low end devices or devices with weaker network strength.</li></ul><p>Multiple such components were suspended and lazily loaded in OLX chat module with the following <strong>performance benefits</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hlloLm4IHxJVXLRisFv6ng.png" /></figure><p><strong>Conclusion</strong></p><p>Lazy and suspense feature has great potential benefits with large feature-rich applications. Whenever you add a new feature to your project, try to evaluate its performance impact on the bundle. If you think the feature could be used on demand or may never be used in the user journey then Lazy and Suspense is the way to go!</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f42cf23065e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>