<?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: Nik Shevchenko</title>
    <description>The latest articles on Forem by Nik Shevchenko (@shevchenkonik).</description>
    <link>https://forem.com/shevchenkonik</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%2F286870%2F7078154e-079f-49a3-bdb1-fe3ef8d7f075.jpeg</url>
      <title>Forem: Nik Shevchenko</title>
      <link>https://forem.com/shevchenkonik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shevchenkonik"/>
    <language>en</language>
    <item>
      <title>The Road to GraphQL At Enterprise Scale</title>
      <dc:creator>Nik Shevchenko</dc:creator>
      <pubDate>Tue, 07 Nov 2023 15:11:16 +0000</pubDate>
      <link>https://forem.com/shevchenkonik/the-road-to-graphql-at-enterprise-scale-j01</link>
      <guid>https://forem.com/shevchenkonik/the-road-to-graphql-at-enterprise-scale-j01</guid>
      <description>&lt;p&gt;The original one is &lt;a href="https://shevchenkonik.com/blog/graphql-at-scale" rel="noopener noreferrer"&gt;https://shevchenkonik.com/blog/graphql-at-scale&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Intro&lt;/li&gt;
&lt;li&gt;Backstory&lt;/li&gt;
&lt;li&gt;I. Introduce GraphQL Federation as API Gateway&lt;/li&gt;
&lt;li&gt;II. Decompose monolith service to GraphQL Subgraphs (Microservices)&lt;/li&gt;
&lt;li&gt;
III. Mesh and/or add support to existing services

&lt;ul&gt;
&lt;li&gt;Transport Service that consumes many API/Sources types&lt;/li&gt;
&lt;li&gt;GraphQL as a part of existing services written on Java, Kotlin, .NET, etc&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;IV. Schema Registry&lt;/li&gt;

&lt;li&gt;V. Observability&lt;/li&gt;

&lt;li&gt;Closing Thoughts&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a id="intro"&gt;Intro&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;GraphQL was designed to allow your API to evolve continuously in response to new product requirements without the burden of versioning, simplify the process of getting data from different sources and support typings for API out of the box.&lt;/p&gt;

&lt;p&gt;Our team is responsible for developing an API — a single endpoint that provides all the necessary data to render the UI for clients and core web infrastructure for the customer portal. Several years ago, we initially used REST endpoints to gather data from multiple services. However, over time, the number of these API calls has grown, and it has become increasingly difficult to manage this system swiftly and maintain up-to-date synchronization between teams.&lt;/p&gt;

&lt;p&gt;We started looking for a solution that would help us easily support the development of the portal and the ability to migrate the current monolith to microservices.&lt;br&gt;
The experience of big tech companies (e.g. &lt;a href="https://shopify.engineering/using-graphql-for-high-performing-mobile-applications" rel="noopener noreferrer"&gt;Shopify&lt;/a&gt;, &lt;a href="https://medium.com/paypal-tech/graphql-at-paypal-an-adoption-story-b7e01175f2b7" rel="noopener noreferrer"&gt;PayPal&lt;/a&gt;, &lt;a href="https://developer.atlassian.com/platform/atlassian-graphql-api/graphql/" rel="noopener noreferrer"&gt;Atlassian&lt;/a&gt;, &lt;a href="https://medium.com/airbnb-engineering/how-airbnb-is-moving-10x-faster-at-scale-with-graphql-and-apollo-aa4ec92d69e2" rel="noopener noreferrer"&gt;Airbnb&lt;/a&gt;, &lt;a href="https://docs.github.com/en/graphql" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, &lt;a href="https://blog.twitter.com/engineering/en_us/topics/infrastructure/2020/rebuild_twitter_public_api_2020" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, and &lt;a href="https://landscape.graphql.org/card-mode?category=graph-ql-adopter&amp;amp;grouping=category&amp;amp;zoom=200" rel="noopener noreferrer"&gt;so on&lt;/a&gt;) with successful usage of GraphQL for their core products that have High Load &amp;amp; High Availability at the same time was a vital sign for introducing GraphQL in our scope.&lt;/p&gt;

&lt;p&gt;In this article, I focus on integrating GraphQL within an enterprise as an infrastructure without considering load balancing, external infrastructure, cache configurations and other important things not directly related to our migration from perspective of GraphQL.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="backstory"&gt;Backstory&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The situation when you can introduce GraphQL as a core service for an enterprise from the scratch is a dream &lt;br&gt;
because you usually have existing infrastructure, and you need to integrate it into the existing codebase as smoothly as possible.&lt;/p&gt;

&lt;p&gt;Our case is not an exception. We had an infrastructure for the customer portal based on multiple REST calls from dozens of UI applications and BFFs to thousands of microservices. In that case, you have the possibility to simplify the work through libraries that contains some needed typings, the logic around HTTP clients, shared configuration, etc.&lt;br&gt;
Services could be written with different protocols, e.g., SOAP, RPC, REST... Most of the services exposes through REST for NodeJS and it expects different HTTP headers.&lt;/p&gt;

&lt;p&gt;We already used libraries and shared configurations for our NodeJS services and UI-related applications (React, Vue, Angular, or pure HTML) for these purposes. &lt;br&gt;
But whenever you need to update something in that library or within shared configuration, it takes a lot of work because you should patch every service &amp;amp; application and spend many hours with product teams and infrastructure teams.&lt;/p&gt;

&lt;p&gt;The goal was to implement such a system that can be maintained by one team and has everything from the data perspective for portal purposes. &lt;br&gt;
In the meantime, product teams don't need to go deeply into details to realize how to call new services, which headers they should provide, and how to see API (Swagger, Postman, etc.).&lt;/p&gt;

&lt;p&gt;GraphQL had the perfect fit for that purpose and contained many additional benefits for us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single typed endpoint for different clients (Web, Mobile, BFF, internal services written on Kotlin, Java, .NET, etc.).&lt;/li&gt;
&lt;li&gt;Playground with the described schema that is a super helpful feature fo quick testing new features (e.g. &lt;a href="https://github.com/graphql/graphiql#graphiql" rel="noopener noreferrer"&gt;GraphiQL&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;The unified way of exposing data through a single endpoint.&lt;/li&gt;
&lt;li&gt;Language agnostic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a team, we are responsible for maintaining the entire portal. &lt;br&gt;
The excellent direction is a simplification of the process of how clients can get all the needed data. &lt;br&gt;
We had a monolith service for the whole portal that combines all needed logic: auth, data about users, features, meta information, analytics, and so on. &lt;br&gt;
A simplified diagram without extra infrastructure (load balancing, caching, etc.) might look like this:&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Finitial-state.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Finitial-state.png" alt="Initial state of the infrastructure" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem with this approach is the amount of redeploys and the possibility of breaking some core functionality when upgrading product-related modules.&lt;br&gt;
As a team, we looked very carefully at each PR, as product teams could change functions, interfaces, and general code used in different modules in the monolith, and it was essential to keep track of every change in each PR. The number of such PRs could be in the dozens per day, which meant that our team had spent much time manually reviewing and revising. And... As you know, this is a place for mistakes.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;a id="i-introduce-graphql-federation-as-api-gateway"&gt;I. Introduce GraphQL Federation as API Gateway&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;First of all, you need add layer for future microservices architecture as a gateway which processes incoming requests to GraphQL.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-first-step.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-first-step.png" alt="Introduced GraphQL Gateway" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GraphQL Gateway&lt;/strong&gt; is primarily responsible for serving GraphQL queries to consumers. &lt;br&gt;
It takes a query from a client, breaks it into smaller sub-queries, and executes that plan by proxying calls to the appropriate downstream subgraphs. &lt;br&gt;
When we started our journey, there was only &lt;a href="https://www.apollographql.com/docs/federation/" rel="noopener noreferrer"&gt;Apollo Federation&lt;/a&gt; in the arena, and we used it. &lt;br&gt;
Still, now you can look at other options (e.g. &lt;a href="https://github.com/mercurius-js/mercurius" rel="noopener noreferrer"&gt;Mercurius&lt;/a&gt;, &lt;a href="https://the-guild.dev/graphql/gateway" rel="noopener noreferrer"&gt;Conductor&lt;/a&gt;, &lt;a href="https://chillicream.com/docs/hotchocolate" rel="noopener noreferrer"&gt;Hot Chocolate&lt;/a&gt;, &lt;a href="https://wundergraph.com" rel="noopener noreferrer"&gt;Wundergraph&lt;/a&gt;, &lt;a href="https://hasura.io/docs/latest/remote-schemas/overview/" rel="noopener noreferrer"&gt;Hasura Remote Schemas&lt;/a&gt;), compare benchmarks and decide what's important and preferable for your needs.&lt;br&gt;
The Gateway provides a unified API for consumers while giving backend engineers flexibility and service isolation.&lt;/p&gt;

&lt;p&gt;Our Monolith is &lt;strong&gt;GraphQL Subgraph&lt;/strong&gt; for now. &lt;br&gt;
As usual, it has the same characteristics as microservice, but from the perspective of our migration is still a monolith but with integrated GraphQL. &lt;br&gt;
This step is important to define GraphQL schema based on GraphQL principles and reduce the approach when we move the REST approach to the GraphQL world.&lt;br&gt;
If you're a newbie in GraphQL, I highly recommend these articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://graphql.org/learn/thinking-in-graphs" rel="noopener noreferrer"&gt;Official GraphQL Specification. Thinking in Graphs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.apollographql.com/blog/graphql/basics/designing-your-first-graphql-schema" rel="noopener noreferrer"&gt;Designing your first GraphQL Schema by Apollo team&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;a id="ii-decompose-monolith-service-to-graphql-subgraphs-microservices"&gt;II. Decompose monolith service to GraphQL Subgraphs&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The decomposition of the monolith is a long process, it doesn't matter at which language service was written. &lt;br&gt;
From that perspective, think of this step as a long process which not directly connected with GraphQL, but we use the benefits of GraphQL to decompose it into microservices.&lt;br&gt;
We decomposed the first module of the monolith as a GraphQL subgraph and added it as a subgraph to the Federation schema:&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-second-step.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-second-step.png" alt="Decomposition monolith" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, &lt;em&gt;Analytics&lt;/em&gt; is not a module of monolith service anymore. &lt;br&gt;
It works as a GraphQL subgraph and isolates all needed business logic within itself.&lt;/p&gt;

&lt;p&gt;The GraphQL Federation by Apollo &lt;a href="https://www.apollographql.com/docs/technotes/TN0013-monolith-to-federation/" rel="noopener noreferrer"&gt;offers a way&lt;/a&gt; how to decompose GraphQL monolith to several subgraphs, but if you use Mercurius as I mentioned above or something else, you can use the same approach with opensource solutions, like &lt;a href="https://the-guild.dev/graphql/tools/docs/schema-directives" rel="noopener noreferrer"&gt;GraphQL Tools&lt;/a&gt; by The Guild.&lt;br&gt;
In the current state, two existing subgraphs have identical resolvers for analytics, and we should mark one resolver as deprecated &amp;amp; second one as used.&lt;br&gt;
Technically, you just need to mark migrated GraphQL field with GraphQL directives in two places, as &lt;code&gt;@shareable&lt;/code&gt; one in monolith to indicate that field will be received from another subgraph and &lt;code&gt;@override&lt;/code&gt; in the new subgraph to indicate a new field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="c"&gt;# GraphQL Monolith&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;analytics&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Analytics&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;shareable&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="c"&gt;# GraphQL Analytics Subgraph&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;extend&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;analytics&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Analytics&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;override&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="n"&gt;monolith&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GraphQL Gateway processes it during schema composition and creates a single schema with correctness data sources, all analytics requests go to Analytics Subgraph and you can easily deprecate the legacy part of the monolith in favor of the new service.&lt;/p&gt;

&lt;p&gt;If you need to migrate fields between subgraphs with slowly shift traffic to test new service before going to production for all clients, please read &lt;a href="https://www.reddit.com/r/RedditEng/comments/z76ous/migrating_traffic_to_new_graphql_federated/" rel="noopener noreferrer"&gt;article by Reddit team&lt;/a&gt;, how they've migrated their schema between subgraphs with &lt;a href="https://www.redhat.com/en/topics/devops/what-is-blue-green-deployment" rel="noopener noreferrer"&gt;Blue/Green&lt;/a&gt; Subgraph deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a id="iii-mesh-andor-add-support-to-existing-services"&gt;III. Mesh and introduce GraphQL to existing services&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We have hundreds of services that were written in the latest 15 years, which means services were developed in different languages by different teams and have different states of support for now. &lt;br&gt;
A bunch of services requires one type of HTTP Headers, which should be provided, another bunch has different HTTP Headers and different API sources (e.g., REST, SOAP, RPC), &lt;br&gt;
and we need to get some data from them to calculate some business data within our particular infrastructure.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-second-step-zoomed.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-second-step-zoomed.png" alt="Mesh and introduce GraphQL to existing services" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have several ways to manage it and unify the process for GraphQL infrastructure.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a id="transport-service-that-consumes-many-apisources-types"&gt;Transport Service that consumes many API/Sources types&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The transport service is a standalone service, a layer (API gateway) between existing services and our infrastructure.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-third-step.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposition-third-step.png" alt="Decomposition monolith. Step 3" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This approach is adding another layer within the infrastructure to process everything in one place. It has benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Expose GraphQL schema in one place for the whole of our infrastructure and don't mind about other services within GraphQL infrastructure. &lt;/li&gt;
&lt;li&gt;Progressively migrate existing non-GraphQL services under a unified GraphQL API. &lt;/li&gt;
&lt;li&gt;Use one caching strategy for the whole GraphQL infrastructure and easily maintain it. &lt;/li&gt;
&lt;li&gt;Unify the logic for other services in one place. &lt;/li&gt;
&lt;li&gt;It can be developed and maintained by a single team, which speeds up the process of migrating the monolith.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Great news, we have an open-source solution by The Guild &lt;a href="https://the-guild.dev/graphql/mesh/docs" rel="noopener noreferrer"&gt;GraphQL Mesh&lt;/a&gt; that covers many APIs and could be easily extended by writing needed plugins.&lt;/p&gt;

&lt;p&gt;As usual, every approach has some cons &amp;amp; props. Let’s take a look at the cons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A transport service is a bottleneck of the system because it processes all requests from subgraphs in one place, and you should be careful because it multiplies the number of requests for complex GraphQL queries. So, you need a good single-core processor because NodeJS runs on a single thread, and be careful with performance. Of course, you should have vertical &amp;amp; horizontal auto-scaling to process traffic spikes.&lt;/li&gt;
&lt;li&gt;An extra layer in your infrastructure increased complexity. Better to decrease the complexity of the system (&lt;a href="https://blog.codinghorror.com/the-best-code-is-no-code-at-all" rel="noopener noreferrer"&gt;the best code is no code at all&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;a id="graphql-as-a-part-of-existing-services-written-on-java-kotlin-net-etc"&gt;GraphQL as a part of existing services written on Java, Kotlin, .NET, etc.&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;More native way of integration GraphQL in existing services is to do it with adding GraphQL servers. &lt;br&gt;
GraphQL is language agnostic, so we don’t have a lock for using different languages. &lt;br&gt;
Let’s take Kotlin as an example, e.g. &lt;a href="https://opensource.expediagroup.com/graphql-kotlin/docs/server/graphql-server/" rel="noopener noreferrer"&gt;Kotlin GraphQL Server&lt;/a&gt;.&lt;br&gt;
As we integrate Messages Service as a subgraph, we don't need to go through GraphQL Transport Service (GTS) and can get all needed data directly from the source.&lt;br&gt;
The diagram below shows that Java &amp;amp; Kotlin services are integrated into the Federation schema as subgraphs and the system’s complexity is extremely low.&lt;/p&gt;

&lt;p&gt;For our flexibility, we can combine both approaches; if we introduce a new service, we can easily get all the needed data from subgraphs, &lt;br&gt;
but if we need some existing data from not integrated services, we can use GTS as transport or add existing service as a subgraph.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposed-fourth-step.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposed-fourth-step.png" alt="Decomposition monolith. Step 4" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After migrating the monolith to microservices, we will still have a core service that may contain some data, or we can get rid of it completely.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposed-last-step.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdecomposed-last-step.png" alt="Decomposition monolith. Last step" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a id="iv-schema-registry"&gt;IV. Schema Registry&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;During development and especially in situations when several teams contribute to the GraphQL infrastructure, our schema is growing and changing. To support this healthy schema evolution, we are able to use the GraphQL Schema Registry.&lt;/p&gt;

&lt;p&gt;The GraphQL Schema Registry is a persistent system element that retains all schemas and schema modifications for each subgraph.&lt;br&gt;
Its role encompasses validating individual DGS schemas and merged schemas, ensuring compliance. &lt;br&gt;
Lastly, the registry assembles the unified schema and supplies it to the gateway.&lt;/p&gt;

&lt;p&gt;GraphQL Schema Registry serves the main purpose of preventing breaking changes. This empowers you to strategize in advance and implement any required schema modifications promptly.&lt;/p&gt;

&lt;p&gt;In the first implementation, we used an approach without a Schema Registry, and we had a dynamic composition of schema during runtime. We had no way to validate changes and prevent breaking schema on production.&lt;/p&gt;

&lt;p&gt;Integrated Schema Registry to our scope we can describe by next diagram:&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fschema-registry.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fschema-registry.png" alt="Schema Registry inserted to Infra" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every subgraph pushes schema in runtime to Schema Registry Service that processes it and validates it for breaking changes. Also, it exposes a unified schema, which is used by Gateway as a main schema of GraphQL infra.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fschema-registry-implementation.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fschema-registry-implementation.png" alt="Schema Registry implementation" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When Schema Registry Service gets a new schema of the updated subgraph, it starts processing the schema from the perspective of composition to the whole schema and checks the main directions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The updated schema is a valid GraphQL schema &amp;amp; could be executed.&lt;/li&gt;
&lt;li&gt;The updated schema composes seamlessly with the rest of the subgraph's schemas to create a valid composed schema for Gateway.&lt;/li&gt;
&lt;li&gt;The updated schema is backward compatible and clients shouldn't upgrade their codebase.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If all of the above conditions are met, then the schema is checked into the Schema Registry. The service store schema in Database and cache it in Redis. &lt;br&gt;
The next step is to publish changes through Kafka to Gateway, and we have updated the schema within the environment.&lt;/p&gt;

&lt;p&gt;The good news is that we have several options not to create our custom solution from scratch and spend a lot of time on it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/tot-ra/graphql-schema-registry" rel="noopener noreferrer"&gt;Completely open-sourced solution&lt;/a&gt; written with similar tools&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://the-guild.dev/graphql/hive/docs" rel="noopener noreferrer"&gt;The open-sourced solution by The Guild&lt;/a&gt; that could be &lt;a href="https://the-guild.dev/graphql/hive/docs/self-hosting/get-started" rel="noopener noreferrer"&gt;self-hosted&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.apollographql.com/docs/graphos/delivery" rel="noopener noreferrer"&gt;The solution by Apollo&lt;/a&gt;, they deliver an utterly outsourced solution, which I don't really like because it has many limitations and questions from the perspective of GDPR and more.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a id="v-observability"&gt;V. Observability&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Observability can be achieved with a set of tools that we combine to get the best possible way of detecting ongoing issues and preventing a bunch of them. &lt;br&gt;
We split the observability into three directions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Alerting - immediately notify when something goes wrong or a metric begins to form that leads to a problem&lt;/li&gt;
&lt;li&gt;Discovery - determine what exactly isn't working properly&lt;/li&gt;
&lt;li&gt;Finding - debug why something isn't working as expected&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As I said at the begging of the article, I will not go into the implementation details. Alerting &amp;amp; discovery can be done similarly as for other services in your company, like with OpsGenie, Grafana, Graylog, and so on.&lt;/p&gt;

&lt;p&gt;From the perspective of the realization of GraphQL infrastructure, the interesting direction is "Finding". &lt;br&gt;
How to find the problem? How to find the bottleneck of the system? Distributed Tracing System (DTS) will help answer this question. &lt;br&gt;
Distributed tracing is a method of observing requests as they propagate through distributed environments. &lt;br&gt;
In our scenario, we have dozens of subgraphs, gateway, and transport layer through which the request goes.&lt;br&gt;
We have several tools that can be used to detect the whole lifecycle of the request through the system, e.g. &lt;a href="https://www.jaegertracing.io" rel="noopener noreferrer"&gt;Jaeger&lt;/a&gt;, &lt;a href="https://zipkin.io" rel="noopener noreferrer"&gt;Zipkin&lt;/a&gt; or solutions that provided DTS as a part of the solution &lt;a href="https://newrelic.com" rel="noopener noreferrer"&gt;NewRelic&lt;/a&gt;.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdts.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fdts.png" alt="Distributed Tracing System" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, let’s take a look at &lt;a href="https://github.com/prasek/supergraph-demo-opentelemetry" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; with integrated Open Telemetry to Apollo Federation.&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fopentelemetry.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fopentelemetry.png" alt="OpenTelemetry" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Based on the UI provided by DTS, we can easily find bottlenecks of the system and tune the system’s performance, that important one from the perspective of scaling infrastructure to the whole company.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a id="closing-thoughts"&gt;Closing Thoughts&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;GraphQL Federation has lowered the connectivity of the monolith and works as a perfect solution for the different client (Web, Mobile) needs.&lt;br&gt;
The addition of GraphQL had fantastic feedback from product teams because they had a typed contract inside their TypeScript/Kotlin codebase and a beautiful playground where&lt;br&gt;
they can make an example of new functionality in a matter of minutes.&lt;/p&gt;

&lt;p&gt;Despite our positive experience as a team, GraphQL Infrastructure migration requires high commitment from other teams and seamless cross-functional collaboration,&lt;br&gt;
as you remembered we had several approaches with unification HTTP headers and API protocols and integration GraphQL servers should be on the side of partner teams,&lt;br&gt;
not to overload the core team. &lt;br&gt;
It also requires some work as a developer advocate because explaining how to work/develop properly with GraphQL and what the best practices are is necessary. &lt;br&gt;
The trend of usage is increasing and this improves the situation:&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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fusage.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%2Fshevchenkonik.com%2Fimages%2Fgraphql-at-scale%2Fusage.png" alt="OpenTelemetry" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Download statistics for npm &lt;b&gt;graphql&lt;/b&gt; package&lt;/p&gt;

&lt;p&gt;Technologies are constantly changing and our responsibility to choose aligned technologies for particular purpose.&lt;br&gt;
From my perspective and experience, GraphQL is good candidate for core infrastructure for customer-centric products.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>graphql</category>
      <category>api</category>
    </item>
    <item>
      <title>Resilience and fault tolerance of Web Oriented Services</title>
      <dc:creator>Nik Shevchenko</dc:creator>
      <pubDate>Tue, 31 May 2022 20:29:27 +0000</pubDate>
      <link>https://forem.com/shevchenkonik/resilience-and-fault-tolerance-of-web-oriented-services-go5</link>
      <guid>https://forem.com/shevchenkonik/resilience-and-fault-tolerance-of-web-oriented-services-go5</guid>
      <description>&lt;p&gt;The original one is &lt;a href="https://shevchenkonik.com/blog/resilience-web-services" rel="noopener noreferrer"&gt;https://shevchenkonik.com/blog/resilience-web-services&lt;/a&gt;&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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fcover-light.svg" 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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fcover-light.svg" alt="Resilience in Web Services" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;
Resilience methodologies

&lt;ul&gt;
&lt;li&gt;Identifying failure points in architecture&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Load Balancing

&lt;ul&gt;
&lt;li&gt;Load Balancing Algorithms&lt;/li&gt;
&lt;li&gt;Internal-Communication Load Balancing&lt;/li&gt;
&lt;li&gt;External Load Balancing with HAPRoxy&lt;/li&gt;
&lt;li&gt;Health Check&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Cache

&lt;ul&gt;
&lt;li&gt;In-Memory Cache&lt;/li&gt;
&lt;li&gt;External Cache&lt;/li&gt;
&lt;li&gt;No Cache&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Microservice architecture have become the new model for building modern cloud-native applications as a part of distributed systems. &lt;/p&gt;

&lt;p&gt;One of the key aspects of building modern web-oriented distributed systems is resilience and fault tolerance. Fault tolerance is the ability for a system to remain in operation even if some components used to build the system fail. With synchronous communication between microservices, it is important that the failure of one service doesn't cause other services to fail as well. &lt;/p&gt;

&lt;p&gt;Otherwise, the unavailability of a single service can cause further microservice to fail until the entire system is no longer available.&lt;/p&gt;

&lt;p&gt;I would like to repeat definitions given by Adrian Hornsby from AWS &lt;a href="https://www.youtube.com/watch?v=gET51_C3k5s" rel="noopener noreferrer"&gt;Patterns for Resilient Architecture, AWS, 2019&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Resilience is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;art of being able to run system with failures.&lt;/li&gt;
&lt;li&gt;ability for a system to handle and eventually recover from unexpected conditions.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, one more thing in terms of architecture is prevented cascading failures. &lt;/p&gt;

&lt;p&gt;This article will cover only the questions of resilience and fault tolerance with high availability. The issues of monitoring, alerting, logging, CI/CD, health checks, etc. will not be considered in this article.&lt;br&gt;
Some examples will use TypeScript &amp;amp; NestJS. &lt;/p&gt;
&lt;h1&gt;
  
  
  Resilience methodologies
&lt;/h1&gt;

&lt;p&gt;Before we start looking at ways how to solve the problems relevant with resilience and fault tolerance, let's understand how to index these issues. One of ways is &lt;strong&gt;Chaos Testing&lt;/strong&gt;, you can find more details &lt;a href="https://www.ibm.com/garage/method/practices/manage/practice_chaotic_testing/" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and &lt;a href="https://medium.com/the-cloud-architect/what-is-aws-fault-injection-simulator-and-why-you-should-care-3fbe457ca227" rel="noopener noreferrer"&gt;this&lt;/a&gt; is brilliant article about Chaos Testing. Think about scenarios like below and find out how the system behaves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Service X isn't able to communicate with Service Y.&lt;/li&gt;
&lt;li&gt;DB isn't accessible.&lt;/li&gt;
&lt;li&gt;Service X isn't able to connect to the Service Y with HTTP, e.g. Service Y supports only HTTPS.&lt;/li&gt;
&lt;li&gt;Server is down or not responding.&lt;/li&gt;
&lt;li&gt;Inject timeouts into the tested services.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Identifying failure points in architecture
&lt;/h2&gt;

&lt;p&gt;If you have been troubleshooting your services and individual service performance, you have already likely identified a few services that either receive or send a lot of requests.&lt;/p&gt;

&lt;p&gt;Optimizing those requests is important and potentially could help you to prolong availability of whole system. But, given a high enough load, the services sending or receiving those requests are likely failure points for your application.&lt;/p&gt;
&lt;h1&gt;
  
  
  Load Balancing
&lt;/h1&gt;

&lt;p&gt;Microservices have the benefit that each microservice can be scaled independently of the other microservices. For that it is needed that the call to a separated microservice can be distributed to several instance by a &lt;strong&gt;load balancer&lt;/strong&gt;. Load balancing solutions allow an application to run on multiple network nodes, removing the concern about a single point of failure&lt;/p&gt;

&lt;p&gt;The good approach for Load Balancing is HAPRoxy, which stands for High Availability Proxy, is a popular open source solution TCP/HTTP Load Balancer and proxying solution.&lt;/p&gt;
&lt;h2&gt;
  
  
  Load Balancing Algorithms
&lt;/h2&gt;

&lt;p&gt;The load balancing algorithms that are used determines which server will be selected when load balancing. HAPRoxy offers several options for algorithms. In addition, to the load balancing algorithm, servers can be assigned a weight parameter to manipulate how frequently the server is selected, compared to other servers. &lt;a href="http://cbonte.github.io/haproxy-dconv/2.5/configuration.html#4.2-balance" rel="noopener noreferrer"&gt;In HAPRoxy Configuration Manual&lt;/a&gt; you can find all existing algorithms, I would like to use &lt;code&gt;roundrobin&lt;/code&gt; (as a default one) for our purposes but if you have specific needs, please use your own algorithm.&lt;/p&gt;
&lt;h2&gt;
  
  
  Internal-Communication Load Balancing
&lt;/h2&gt;

&lt;p&gt;The idea of internal load balancing can be implemented with a load balancer for each microservice. The load balancer must obtain the information about the currently available microservices from the service discovery:&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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Finternal_load_balancing.jpg" 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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Finternal_load_balancing.jpg" alt="Schema of internal load balancing" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Decentralized load balancing is great idea, we could deploy our microservices canary and load balancer is single point of failure only for one microservice. The entire microservices' system should work without one microservice, important that the failure of one microservice doesn't cause other microservices to fail as well. &lt;/p&gt;
&lt;h2&gt;
  
  
  External Load Balancing with HAPRoxy
&lt;/h2&gt;

&lt;p&gt;HAProxy is a very performant open source reverse proxy that works with both Layer 4 and Layer 7 protocols. &lt;br&gt;
It’s written in C and is designed to be stable and use minimal resources, offloading as much processing as possible to the kernel. Like JavaScript, HAProxy is event driven and single threaded.&lt;/p&gt;

&lt;p&gt;We should provide multiple HAPRoxy instances per each our Data Center to be able that we don't have a failure point of this layer for high availability. &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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fexternal_load_balancing.jpg" 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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fexternal_load_balancing.jpg" alt="Schema of external load balancing" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Floating IPs let you redirect all incoming network traffic between any of existing HAPRoxy within the same datacenter (EU1 in the scheme below). It can be resolved with &lt;a href="https://github.com/acassen/keepalived" rel="noopener noreferrer"&gt;Keepalived&lt;/a&gt; and &lt;a href="https://networklessons.com/cisco/ccie-routing-switching/vrrp-virtual-router-redundancy-protocol" rel="noopener noreferrer"&gt;Virtual Router Redundancy Protocol&lt;/a&gt;. As a result you can use floating IPs to create server infrastructures without single points of failure.&lt;/p&gt;
&lt;h2&gt;
  
  
  Health Check
&lt;/h2&gt;

&lt;p&gt;HAProxy uses health checks to determine if a backend server is available to process requests. This avoids having to manually remove a server from the backend if it becomes unavailable. The default health check is to try to establish a TCP connection to the server i.e. it checks if the backend server is listening on the configured IP address and port.&lt;/p&gt;

&lt;p&gt;If a server fails a health check, and therefore is unable to serve requests, it is automatically disabled in the backend i.e. traffic will not be forwarded to it until it becomes healthy again. If all servers in a backend fail, the service will become unavailable until at least one of those backend servers becomes healthy again.&lt;/p&gt;

&lt;p&gt;For certain types of backends, like database servers in certain situations, the default health check is insufficient to determine whether a server is still healthy.&lt;/p&gt;
&lt;h1&gt;
  
  
  Cache
&lt;/h1&gt;

&lt;p&gt;Caching is a great and simple technique that helps improve your app's performance and resilience. It acts as a temporary data store providing high performance data access. I would concentrate on three different strategies which could be combined are &lt;strong&gt;In-Memory Cache&lt;/strong&gt;, &lt;strong&gt;External Cache&lt;/strong&gt; (Redis, Memcached, etc.), &lt;strong&gt;No Cache&lt;/strong&gt; strategy.&lt;br&gt;
But how cache help us increase fault tolerance and resilience? If the service X had a successful request to service Y, we cached the response in Redis. But then some problems happened in service Y, we get the same request for the data again, we can go to the cache and return the user the correct information from the cache. But it is important to remember the right caching strategy. &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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fresilience-web-services%2Fredis_scheme.jpg" 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%2Fshevchenkonik.com%2Fimages%2Fresilience-web-services%2Fresilience-web-services%2Fredis_scheme.jpg" alt="Redis Cache" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  In-Memory Cache
&lt;/h2&gt;

&lt;p&gt;The simplest cache is based on the In-Memory Cache. In-Memory Cache represents a cache stored in the memory of the web server. &lt;/p&gt;

&lt;p&gt;This is the fastest one, but it has a strong disadvantage in terms of data compatibility. The cache is destroyed between crashed, re-deployments, deployments of services. One of potential cases for storing data in memory cache of the web server is store sensitive data. Because Redis was designed not for maximum security but for maximum performance and simplicity. That means, every engineer within trusted environment can read non-encrypted data in key-value store.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis is designed to be accessed by trusted clients inside trusted environments. This means that usually it is not a good idea to expose the Redis instance directly to the internet or, in general, to an environment where untrusted clients can directly access the Redis TCP port or UNIX socket.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The simplest implementation of In-Memory Cache based on the interface:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;CacheClientInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&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;delete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;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;I would like to use &lt;a href="https://www.npmjs.com/package/memory-cache" rel="noopener noreferrer"&gt;memory-cache&lt;/a&gt; package as a quick solution. An example of using combined with default &lt;a href="https://docs.nestjs.com/fundamentals/lifecycle-events#lifecycle-events-1" rel="noopener noreferrer"&gt;lifecycle events&lt;/a&gt; from NestJS, &lt;code&gt;onModuleInit&lt;/code&gt; and &lt;code&gt;onApplicationShutdown&lt;/code&gt; help us initialize in-memory cache when application is started and clear in-memory cache when application is off.&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="nx"&gt;inMemoryCache&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;memory-cache&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;CacheClientInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&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;delete&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InMemoryCacheClient&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;CacheClientInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;onModuleInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inMemoryCache&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;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;onApplicationShutdown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inMemoryCache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;del&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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;h2&gt;
  
  
  External Cache
&lt;/h2&gt;

&lt;p&gt;It's slower than an in-memory cache but should be faster than hitting the source of truth. &lt;br&gt;
It also prevents the cache from being wiped out between crashes and deployments, &lt;br&gt;
or when 3rd party service has issues for some reasons, and we can prevent starting cascading failures.&lt;br&gt;
We have several opensource options of noSQL key-value in-memory data storage systems, e.g. Redis, Memcached.&lt;br&gt;
I would like to use Redis (but if you need more details of comparing these two opensource solutions, please read &lt;a href="https://www.imaginarycloud.com/blog/redis-vs-memcached/" rel="noopener noreferrer"&gt;Redis or MemCached&lt;/a&gt;).&lt;br&gt;
Also, we should implement &lt;code&gt;CacheClientInterface&lt;/code&gt; from the example below. I will consider a third-party cache using Redis as an example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis can be accessed by all the processes of your applications, possibly running on several nodes (something local memory cannot achieve).&lt;/li&gt;
&lt;li&gt;Redis memory storage is quite efficient, and done in a separate process.&lt;/li&gt;
&lt;li&gt;Redis can persist the data on disk if needed.&lt;/li&gt;
&lt;li&gt;Redis can replicate its activity with a master/slave mechanism in order to implement high-availability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  No cache
&lt;/h2&gt;

&lt;p&gt;In this approach, an application talks directly with external services.&lt;/p&gt;

&lt;p&gt;Let's start with external cache, we have several options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis&lt;/li&gt;
&lt;li&gt;ElasticSearch Cache&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Redis is a powerful service exposing several useful data structures while providing many commands to interact with them. But Redis has a limitation: the data stored in a Redis instance must fit completely in memory. For this reason, Redis is best used only as a cache, not as a data source.&lt;/p&gt;

&lt;p&gt;Where the rest of the problem is and how to solve it we are going to explore in Part 2 of our series.&lt;/p&gt;

</description>
      <category>node</category>
      <category>microservices</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React, TypeScript &amp; Mobx</title>
      <dc:creator>Nik Shevchenko</dc:creator>
      <pubDate>Sun, 23 Aug 2020 17:09:55 +0000</pubDate>
      <link>https://forem.com/shevchenkonik/react-typescript-mobx-4mei</link>
      <guid>https://forem.com/shevchenkonik/react-typescript-mobx-4mei</guid>
      <description>&lt;p&gt;The original post: &lt;a href="https://shevchenkonik.com/blog/react-typescript-mobx" rel="noopener noreferrer"&gt;https://shevchenkonik.com/blog/react-typescript-mobx&lt;/a&gt;&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%2F1b073eznw1i86l2ey6w5.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%2F1b073eznw1i86l2ey6w5.png" alt="Mobx, TypeScript and React" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I want to share my R&amp;amp;D process of using React, TS, and Mobx. It’s about conceptual approaches to building large scalable applications. The second part of this article series will talk about building real application with 3rd services and ML preprocessing 🔥&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;I built a web application that allows us to work with Mobx, TypeScript, React, and 3rd API Services. This article focuses on practical examples as the best way to understand the concepts of technologies and part of patterns theory.&lt;/p&gt;

&lt;p&gt;I'll use two ways of organizing React Components for showing different ways of using stores, Class-based Components, and Functional Components with React Hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Application
&lt;/h2&gt;

&lt;p&gt;I'll provide a short introduction to the setup process, you can skip this section if you already know it. If you need a more specific application, please use custom webpack/rollup/parcel or something else, but we'll use Create React App with TypeScript support for simple process of setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install create-react-app with TypeScript
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app typescript-mobx-react &lt;span class="nt"&gt;--template&lt;/span&gt; typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install types needed for TypeScript as development dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;--save-dev&lt;/span&gt; typescript @types/node @types/react @types/react-dom @types/jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install Mobx and its connection to React
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i mobx-react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;App's source code is nested beneath the &lt;code&gt;src&lt;/code&gt; directory. And structure of application will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;├── src
│   ├── components
│   ├── containers
│   ├── contexts
│   ├── hocs
│   ├── hooks
│   ├── pages
│   ├── services
│   ├── stores
│   └── index.tsx
├── dist
├── node_modules
├── README.md
├── package.json
└── .gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup Services &amp;amp; Stores
&lt;/h2&gt;

&lt;p&gt;I started developing my application by designing stores in the domain area. A few main concepts of stores composition that I need for my application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy communication between stores.&lt;/li&gt;
&lt;li&gt;Root store composition with children stores.&lt;/li&gt;
&lt;li&gt;Separate communications and stores.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I designed my application approach with MVC like Design Pattern and layered architecture as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All backend communications (in our case we use only Spotify API as 3rd Service) are done by Service Layer.&lt;/li&gt;
&lt;li&gt;The store has a state of the application so it consumes service &lt;a href="https://mobx.js.org/best/store.html" rel="noopener noreferrer"&gt;Defining data stores&lt;/a&gt;. All service functions will be called in the only store, components execute Store actions when the state is needed.&lt;/li&gt;
&lt;li&gt;Presentational Component can use the store directly by injecting the store or Props from Container Component can be passed in it.&lt;/li&gt;
&lt;li&gt;Container or Presentational Component can invoke store actions and automatic rendering of components will be done by Mobx.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Services are a place for communication between application and Backend Services. We use this separation for a more flexible and elegant way to organize our codebase, cause if we'll use service calls inside the store then we'll find complicated stores with harder test writing process when an application will scale. Inside a store, we call the service method and update the store only inside the &lt;code&gt;@action&lt;/code&gt; decorator of Mobx. Service methods are needed only for communication and they don’t modify Stores, we can modify observable variables only inside &lt;code&gt;@action&lt;/code&gt; calls in Stores.&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%2F24zb0otjspzy4j763hf9.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%2F24zb0otjspzy4j763hf9.png" alt="Conceptual configuration of Application" width="800" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main responsibilities of Stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separate logic and state with components.&lt;/li&gt;
&lt;li&gt;A standalone testable place that can be used in both Frontend and Backend JavaScript. And you can write really simple unit tests for your Stores &amp;amp; Services with any codebase size.&lt;/li&gt;
&lt;li&gt;A single source of truth of Application.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;An alternative, more opinionated way of organizing stores is using &lt;a href="https://github.com/mobxjs/mobx-state-tree" rel="noopener noreferrer"&gt;mobx-state-tree&lt;/a&gt;, which ships with cool features as structurally shared snapshots, action middlewares, JSON patch support, etc out of the box.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But Mobx-State-Tree (MST) is a like framework based on Mobx and when you start using MST you need to implement practices and API from MST. But I want to use &lt;em&gt;more native way&lt;/em&gt; of my codebase and &lt;em&gt;less overkill for my needs&lt;/em&gt;. If you want to see the big codebase of MST and Mobx, you can check my previous big opensource project of data labeling and annotation tools for ML on React, Mobx, and MST - &lt;a href="https://github.com/heartexlabs/label-studio" rel="noopener noreferrer"&gt;Label Studio&lt;/a&gt; and &lt;a href="https://github.com/heartexlabs/label-studio-frontend" rel="noopener noreferrer"&gt;Frontend Part of Label Studio&lt;/a&gt;. In MST we have many awesome things like a Tree, Snapshots, Time Travelling, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Organizing Stores
&lt;/h2&gt;

&lt;p&gt;The primary purpose of Mobx is to simplify the management of Stores. As application scales, the amount of state you manage will also increase. This requires some techniques to break down your application state and to divvy it up across a set of stores. Of course, putting everything in one Store is not prudent, so we apply &lt;em&gt;divide-and-conquer&lt;/em&gt; instead.&lt;/p&gt;

&lt;p&gt;And don't write your business logic in your components, because when you writing it, you have &lt;em&gt;no way&lt;/em&gt; to reuse it. Better way is writing the business logic with methods in the Stores and call these methods from your containers and components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Communication between stores
&lt;/h3&gt;

&lt;p&gt;The main concept of stores communication is using Root Store as a global store where we create all different stores and pass global &lt;code&gt;this&lt;/code&gt; inside a constructor of Root Store. Stores are the place of the truth for application.&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%2Fndobiw85wj8xzfsdqdlh.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%2Fndobiw85wj8xzfsdqdlh.png" alt="Mobx Stores" width="800" height="607"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Root Store collects all other stores in one place. If your children store needs methods or data from another store, you can pass &lt;code&gt;this&lt;/code&gt; into a store like as User Store for &lt;em&gt;easy communication between stores&lt;/em&gt;. The main advantages of this pattern are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple to set up your application.&lt;/li&gt;
&lt;li&gt;Supports strong typings well.&lt;/li&gt;
&lt;li&gt;Makes complex unit tests easy as you just have to instantiate a root store.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Import all your stores
 */&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;AuthStore&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;./AuthStore&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;UserStore&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;./UserStore&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Root Store Class with
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RootStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;authStore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthStore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;userStore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserStore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authStore&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;AuthStore&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userStore&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;UserStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Pass `this` into stores for easy communication&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;And then you can use methods from Auth Store in User Store for example:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;observable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&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;mobx&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;v4&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;uuidv4&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="s2"&gt;uuid&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;RootStoreModel&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;./rootStore&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IUserStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;pic&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserStore&lt;/span&gt; &lt;span class="kr"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IUserStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;rootStore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RootStoreModel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;observable&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;uuidv4&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;observable&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;observable&lt;/span&gt; &lt;span class="nx"&gt;pic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rootStore&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;RootStoreModel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rootStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rootStore&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="nd"&gt;action&lt;/span&gt; &lt;span class="nx"&gt;getName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rootStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;h2&gt;
  
  
  Context Provider to pass Store
&lt;/h2&gt;

&lt;p&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level. Nothing spectacular about it, better to read &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;React Context&lt;/a&gt; if you are unsure though. Let's create Provider for our Application:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ReactNode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ReactElement&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;react&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;RootStoreModel&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;../stores&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;RootStoreModel&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;as&lt;/span&gt; &lt;span class="nx"&gt;RootStoreModel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;StoreComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RootStoreModel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReactNode&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;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;StoreProvider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;StoreComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;store&lt;/span&gt;
&lt;span class="p"&gt;}):&lt;/span&gt; &lt;span class="nx"&gt;ReactElement&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StoreContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&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="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StoreContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;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;And you can use in the entry point of Application:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;react&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;ReactDOM&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;react-dom&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;StoreProvider&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;./store/useStore&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;App&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;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StrictMode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;StoreProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/StoreProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.StrictMode&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&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;h2&gt;
  
  
  Class and Functional Components
&lt;/h2&gt;

&lt;p&gt;We can use both ways of our components — Class-based components and Functional components with React Hooks as a modern way to organize React Application.&lt;/p&gt;

&lt;p&gt;If you are using use &lt;strong&gt;only&lt;/strong&gt; Functional Components with React Hooks, you can use &lt;code&gt;mobx-react-lite&lt;/code&gt; instead of &lt;code&gt;mobx-react&lt;/code&gt; to reduce size bundle. If you are using Class-based components and Functional components, please use only &lt;code&gt;mobx-react@6&lt;/code&gt; which includes &lt;code&gt;mobx-react-lite&lt;/code&gt; and uses it automatically for function components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom HOC to provide Store into a Class-based Components
&lt;/h3&gt;

&lt;p&gt;React Context replaces the Legacy Context which was fairly awkward to use. In simple words, React Context is used to store some data in one place and use it all over the app. Previously, Mobx had &lt;code&gt;Provider/inject&lt;/code&gt; pattern, but currently this pattern is deprecated and we must use only one way - Context. And again, it's not mandatory to use React Context with Mobx but it's recommended now officially on the &lt;code&gt;mobx-react&lt;/code&gt; website. You can read more info about it here - &lt;a href="https://mobx-react.js.org/recipes-inject" rel="noopener noreferrer"&gt;Why is Store Injecting obsolete?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I wrote &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;HOC (High Order Component)&lt;/a&gt; for support Class based Components:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ComponentType&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;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cm"&gt;/**
 * https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
*/&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;hoistNonReactStatics&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;hoist-non-react-statics&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;useStores&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;../hooks/useStores&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TWithStoreHOC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;JSX&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withStore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TWithStoreHOC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&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="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComponentWithStore&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useStores&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nx"&gt;ComponentWithStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultProps&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;ComponentWithStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;displayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`WithStores(&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;
        &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;displayName&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;)`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;hoistNonReactStatics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ComponentWithStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ComponentWithStore&lt;/span&gt; &lt;span class="o"&gt;/&amp;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;And Class based Component will be:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&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;react&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;observer&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;mobx-react&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;withStore&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;../hocs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserNameComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;UserNameComponent&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one is an elegant way to use Stores inside Components. If you want to use &lt;code&gt;decorators&lt;/code&gt;, the code will be:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&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;react&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;observer&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;mobx-react&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;withStore&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;../hocs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;withStore&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;observer&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserNameComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;UserNameComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  React Hook with Stores for Functional Components
&lt;/h3&gt;

&lt;p&gt;We add a function to help us to get the stores inside the React Functional Components. Using &lt;code&gt;useContext&lt;/code&gt; that React provides us, we pass the previously created context to it and get the value we spicified.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&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;react&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;RootStore&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;../stores&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;StoreContext&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;../contexts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useStores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;RootStore&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;StoreContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Functional Components
&lt;/h3&gt;

&lt;p&gt;If you want to use functional components, you need to use only &lt;code&gt;observer&lt;/code&gt; function from &lt;code&gt;mobx-react&lt;/code&gt; bindings and &lt;code&gt;useStores&lt;/code&gt; hook of our Application:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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;react&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;observer&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;mobx-react&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;useStores&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;../hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FunctionalContainer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;userStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useStores&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Functional&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;FunctionalContainer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Services Layer
&lt;/h2&gt;

&lt;p&gt;Services layer is the place of communications with Backend, 3rd API. Don't call your REST API Interfaces from within your stores. It really makes them hard to test your code. Instead of, please put these API Calls into extra classes (Services) and pass these instances to each store using the store's constructor. When you write tests, you can easily mock these API calls and pass your mock API Instance to each store.&lt;/p&gt;

&lt;p&gt;For example, we need a class &lt;code&gt;SpotifyService&lt;/code&gt; where we can use API and this class is &lt;code&gt;Singleton&lt;/code&gt;. I use Singleton pattern because I want just a single instance available to all Stores.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;SpotifyWebApi&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;spotify-web-api-js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;APISpotifyService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getAlbums&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SpotifyService&lt;/span&gt; &lt;span class="kr"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;APISpotifyService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SpotifyWebApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SpotifyWebApiJs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&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;SpotifyWebApi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getAlbums&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;albums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMySavedAlbums&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;albums&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="cm"&gt;/**
 * Export only one Instance of SpotifyService Class
*/&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SpotifyServiceInstance&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;SpotifyService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you can use in your Stores in this way:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;action&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;mobx&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;SpotifyServiceInstance&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;../services&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserStore&lt;/span&gt; &lt;span class="kr"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IUserStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;action&lt;/span&gt; &lt;span class="nx"&gt;getAlbums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;SpotifyServiceInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAlbums&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To sum up, this guide shows how we can connect React with Hooks and Classes with Mobx and TypeScript. I think this combination of MVC pattern with Mobx, React and TypeScript produces highly typed, straightforward and scalable code.&lt;/p&gt;

&lt;p&gt;The source code will be available on my &lt;a href="https://github.com/shevchenkonik" rel="noopener noreferrer"&gt;github&lt;/a&gt; &amp;amp; will be under the MIT License for your using when I'll publish the second part of the article series.&lt;/p&gt;

&lt;p&gt;I hope this walkthrough was interesting and you can could find some information that helped in your projects. If you have any feedback or something else, please write to me on &lt;a href="https://twitter.com/nikpshevchenko" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; and we will discuss any moments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://mobx.js.org/best/store.html" rel="noopener noreferrer"&gt;Best Practices for building large scale maintainable projects with Mobx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://refactoring.guru/design-patterns/singleton" rel="noopener noreferrer"&gt;Design Patterns – Singleton&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>mobx</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Memory size of Javascript Boolean</title>
      <dc:creator>Nik Shevchenko</dc:creator>
      <pubDate>Thu, 20 Feb 2020 19:43:12 +0000</pubDate>
      <link>https://forem.com/shevchenkonik/memory-size-of-javascript-boolean-3mlj</link>
      <guid>https://forem.com/shevchenkonik/memory-size-of-javascript-boolean-3mlj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Javascript in the browser works a level above Javascript Engine (&lt;a href="https://en.wikipedia.org/wiki/V8_(JavaScript_engine)" rel="noopener noreferrer"&gt;V8&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Rhino_(JavaScript_engine)" rel="noopener noreferrer"&gt;Rhino&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/WebKit#JavaScriptCore" rel="noopener noreferrer"&gt;JavaScriptCore&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/SpiderMonkey" rel="noopener noreferrer"&gt;SpiderMonkey&lt;/a&gt;). These engines follow the ECMAScript Standards. ECMAScript defines the standard for the scripting language. [1]&lt;/p&gt;

&lt;p&gt;In this post, we will use V8 Engine by Google. The V8 Engine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The V8 Engine is written in C++ and used in Chrome &amp;amp; in Node.js, among others.&lt;/li&gt;
&lt;li&gt;It implements the ECMAScript Standard as specified in ECMA-262. [1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More specific documentation of the V8 Engine is available at &lt;a href="https://v8.dev/docs#about-v8" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The main scheme of compilation JavaScript code to Machine Code:&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%2Fi%2F6cb8bmkmlixrtdq507fe.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%2Fi%2F6cb8bmkmlixrtdq507fe.png" alt="Javascript to Machine Code scheme" width="800" height="577"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each object takes a specific size in memory and C++ or ECMAScript specify on this size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boolean
&lt;/h2&gt;

&lt;p&gt;Size: &lt;strong&gt;4&lt;/strong&gt; bytes or &lt;strong&gt;1&lt;/strong&gt; byte&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;boolean&lt;/code&gt; is actually &lt;code&gt;1 byte&lt;/code&gt;. But alignment may cause 4 bytes to be used on a 32-bit platform or 8 bytes on a 64-bit platform. This old trick comes from the observation that allocated memory takes up at least 4 or 8 bytes, and are aligned in the way that the least significant bit or three will be zero.&lt;/p&gt;

&lt;p&gt;In C++, the size of the type &lt;code&gt;boolean&lt;/code&gt; is implementation-defined (&lt;a href="http://eel.is/c++draft/expr.sizeof#1" rel="noopener noreferrer"&gt;expr.sizeof[p1]&lt;/a&gt;) and is usually equal to 1 (the size of the type char, and the smallest size a type can have), but is not required to be (&lt;a href="http://eel.is/c++draft/expr.sizeof#footnote-77" rel="noopener noreferrer"&gt;expr.sizeof[fn77]&lt;/a&gt;): in particular, in Visual Studio up to version 4.2, it was 4. More information about C++ boolean values is available at &lt;a href="http://eel.is/c++draft/expr.sizeof#footnoteref-69[]" rel="noopener noreferrer"&gt;docs[expr.sizeof(7.6.2.4)]&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;[1] –– &lt;a href="https://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="noopener noreferrer"&gt;https://www.ecma-international.org/publications/standards/Ecma-262.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] –– &lt;a href="https://www.quora.com/In-C%2B%2B-what-is-the-size-of-type-bool/answer/Sergey-Zubkov-1?ch=10&amp;amp;share=2471829a&amp;amp;srid=lXWU" rel="noopener noreferrer"&gt;https://www.quora.com/In-C%2B%2B-what-is-the-size-of-type-bool/answer/Sergey-Zubkov-1?ch=10&amp;amp;share=2471829a&amp;amp;srid=lXWU&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] –– &lt;a href="https://www.freecodecamp.org/news/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] –– &lt;a href="https://stackoverflow.com/questions/32733314/in-v8-how-are-primitive-types-such-as-null-undefined-and-boolean-stored-in-me" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/32733314/in-v8-how-are-primitive-types-such-as-null-undefined-and-boolean-stored-in-me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>v8</category>
      <category>architecture</category>
      <category>research</category>
    </item>
    <item>
      <title>GraphQL Fragments</title>
      <dc:creator>Nik Shevchenko</dc:creator>
      <pubDate>Thu, 20 Feb 2020 19:16:45 +0000</pubDate>
      <link>https://forem.com/shevchenkonik/graphql-fragments-4d2k</link>
      <guid>https://forem.com/shevchenkonik/graphql-fragments-4d2k</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;GraphQL Fragment is a reusable part of the query. Fragments let you construct sets of fields and then include them in different queries of your application. You can think of it as functions in programming languages, that are reusable units of your code. Here's an example of how you could solve the above situation using fragments and support of DRY practice in your queries and components.&lt;/p&gt;

&lt;h1&gt;
  
  
  Fragment description
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="c"&gt;# Fragment&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="k"&gt;fragment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A fragment consists of parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fragment name&lt;/strong&gt; (&lt;em&gt;name&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the custom name of the fragment and every fragment will be with own name&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Object Type&lt;/strong&gt; (&lt;em&gt;Type&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a type of object where you use a fragment&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Body of fragment&lt;/strong&gt; (&lt;em&gt;id, name&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This defines the fields of the shared query&lt;/p&gt;

&lt;h1&gt;
  
  
  How to use GraphQL Fragments
&lt;/h1&gt;

&lt;p&gt;Let's use the Artsy API, artists and artworks collections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="c"&gt;# Fragment of Artist&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="k"&gt;fragment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;artistInfo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Artist&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;bio&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Queries&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="n"&gt;trending_artists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ARTIST_INQUIRY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;artists&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="n"&gt;artistInfo&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

  &lt;/span&gt;&lt;span class="n"&gt;popular_artists&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;artists&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="n"&gt;artistInfo&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax of using a fragment is similar to the JavaScript spread operator. And the idea of a fragment is the same –– to assign the keys and values of one object to another object. &lt;code&gt;...&lt;/code&gt; give GraphQL command assigns the fields from the custom fragment to the current selection set. In our example we are able to select the name, bio, and id in two different queries using one instance of a fragment, it's cool.&lt;/p&gt;

&lt;p&gt;Playground of this example on &lt;a href="https://bit.ly/3bMw5W6" rel="noopener noreferrer"&gt;GraphQL Arty's&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do you need to use GraphQL Fragments?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Reusable part of queries&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can organize your queries into small units with reusable parts with fragments. This is the best practice for performance and components architecture of your application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Caching&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fragments are also frequently used in frontend caching clients like ApolloClient or Relay. ApolloClient provides methods for interacting with cached data (&lt;a href="https://www.apollographql.com/docs/react/caching/cache-interaction/" rel="noopener noreferrer"&gt;https://www.apollographql.com/docs/react/caching/cache-interaction/&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;In Relay, we have the so-called fragment container, which defines the component’s data requirements of the component.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Static typing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can generate static types from GraphQL for TypeScript or Flow.&lt;/p&gt;

&lt;h1&gt;
  
  
  Resources
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Official documentation from GraphQL Fragments –– &lt;a href="https://graphql.org/learn/queries/#fragments" rel="noopener noreferrer"&gt;https://graphql.org/learn/queries/#fragments&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Interacting with cached data in Apollo –– &lt;a href="https://www.apollographql.com/docs/react/caching/cache-interaction/" rel="noopener noreferrer"&gt;https://www.apollographql.com/docs/react/caching/cache-interaction/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Public GraphQL APIs list –– &lt;a href="https://github.com/APIs-guru/graphql-apis" rel="noopener noreferrer"&gt;https://github.com/APIs-guru/graphql-apis&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Artsy's GraphQL API –– &lt;a href="https://github.com/artsy/metaphysics" rel="noopener noreferrer"&gt;https://github.com/artsy/metaphysics&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>graphql</category>
      <category>javascript</category>
      <category>apollo</category>
      <category>relay</category>
    </item>
  </channel>
</rss>
