{"path":"docs/architecture","templateID":1,"sharedPropsHashes":{"sidebarHeaders":"Z2g2wgG"},"localProps":{"title":"architecture","path":"/architecture/","renderedMd":"<p><a name=\"architecture\"></a></p>\n\n        <h1>\n          Architecture\n        </h1>\n      \n<p>Much of <code class=\"language-text\">urql</code> is about being flexible and customizable.\nTo this extent a large chunk of this document is dedicated to\nhow <code class=\"language-text\">urql</code> works and how to adapt it to different use cases.</p>\n<p>If you wish to use <code class=\"language-text\">urql</code> without any customizations, this\ndocument is entirely optional for you. But it's still worth\nthe read. Promised.</p>\n<p><code class=\"language-text\">urql</code>'s core parts are separated into three concepts:\n<strong>operations &#x26; results</strong>, <strong>the client</strong>, and <strong>exchanges</strong>.</p>\n\n            <h2>\n              <a class=\"Anchor\"\n                aria-hidden=\"true\"\n                id=\"requests\"\n                href=\"#requests\"\n              >\n                <svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\"aria-hidden=\"true\">\n                  <path fill-rule=\"evenodd\"\n                    d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n                  />\n                </svg>\n              </a>\n              Requests\n            </h2>\n          \n<p>Every GraphQL Request starts as a collection of just a\nquery and variables, which are supposed to be sent to\na GraphQL API (presumably). Those are objects that\ntie the two together.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">type GraphQLRequest <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  key<span class=\"token punctuation\">:</span> number<span class=\"token punctuation\">,</span>\n  query<span class=\"token punctuation\">:</span> string <span class=\"token operator\">|</span> DocumentNode<span class=\"token punctuation\">,</span>\n  variables<span class=\"token operator\">?</span><span class=\"token punctuation\">:</span> object<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <code class=\"language-text\">key</code> property will become a unique identifier of the GraphQL Request.\nIt's a hash of the exact <code class=\"language-text\">query</code> and <code class=\"language-text\">variables</code> combination, i.e. a\nunique string for this request. It's used to keep track of\nwhat request the client is dealing with at any given time.</p>\n<p>To begin sending a GraphQL request the <strong>client</strong> has\n<a href=\"https://github.com/FormidableLabs/urql/blob/master/src/client.ts\">three main methods</a>\nthat are responsible to provide this <strong>input</strong>.</p>\n<ul>\n<li><code class=\"language-text\">executeQuery</code></li>\n<li><code class=\"language-text\">executeSubscription</code></li>\n<li><code class=\"language-text\">executeMutation</code></li>\n</ul>\n<p>All of these can be called with a <code class=\"language-text\">GraphQLRequest</code> as\nthe first argument and optionally accept some additional\n\"context\" information. The method that was called determined\nthe operation that is then sent.</p>\n<blockquote>\n<p><em>Note:</em> In GraphQL and in <code class=\"language-text\">urql</code> the term \"query\" can be ambiguous.\nIt is used to refer to a string composed in the\n<a href=\"https://facebook.github.io/graphql/June2018/#sec-Overview\">query language</a>\nbut also one of the\n<a href=\"https://facebook.github.io/graphql/June2018/#sec-Language.Operations\">three basic operations</a>.</p>\n</blockquote>\n\n            <h2>\n              <a class=\"Anchor\"\n                aria-hidden=\"true\"\n                id=\"operations\"\n                href=\"#operations\"\n              >\n                <svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\"aria-hidden=\"true\">\n                  <path fill-rule=\"evenodd\"\n                    d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n                  />\n                </svg>\n              </a>\n              Operations\n            </h2>\n          \n<p>The client will enrich every request with meta information,\nthe result of which is called an <strong>operation</strong>. You can think\nof them as the actual input of the eventual GraphQL request.</p>\n<p>All bits that are added to a request to form an <strong>operation</strong>\nare there to inform what should happen to it. It determines\nhow the network request should be sent and how the cache\nshould behave.</p>\n<p>The shape of an operation extends the shape of a GraphQL Request:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">type Operation <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// GraphQLRequest:</span>\n  key<span class=\"token punctuation\">:</span> number<span class=\"token punctuation\">,</span>\n  query<span class=\"token punctuation\">:</span> DocumentNode<span class=\"token punctuation\">,</span>\n  variables<span class=\"token operator\">?</span><span class=\"token punctuation\">:</span> object\n\n  <span class=\"token comment\">// The rest:</span>\n  operationName<span class=\"token punctuation\">:</span> OperationType<span class=\"token punctuation\">,</span>\n  context<span class=\"token punctuation\">:</span> OperationContext\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>The <code class=\"language-text\">OperationType</code> here is simply one of the three basic\nGraphQL operations: <code class=\"language-text\">&#39;subscription&#39;</code>, <code class=\"language-text\">&#39;query&#39;</code>, or <code class=\"language-text\">&#39;mutation&#39;</code>.\nAdditionally there's an internal operation type called <code class=\"language-text\">&#39;teardown&#39;</code>\nwhich is used to <strong>cancel</strong> all ongoing work for a previous operation\nand free its resources.</p>\n<p>If we call <code class=\"language-text\">executeQuery({ query: &#39;{ content }&#39; })</code>, the client\nwill internally dispatch the following operation:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> exampleOperation <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  query<span class=\"token punctuation\">:</span> <span class=\"token string\">'{ content }'</span><span class=\"token punctuation\">,</span>\n  variables<span class=\"token punctuation\">:</span> <span class=\"token keyword\">undefined</span><span class=\"token punctuation\">,</span>\n\n  key<span class=\"token punctuation\">:</span> <span class=\"token comment\">/* KEY */</span><span class=\"token punctuation\">,</span>\n  operationName<span class=\"token punctuation\">:</span> <span class=\"token string\">'query'</span><span class=\"token punctuation\">,</span>\n  context<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The <code class=\"language-text\">context</code> contains some more information and can be extended with\nthe second argument to <code class=\"language-text\">executeQuery</code>. By default it contains:</p>\n<ul>\n<li><code class=\"language-text\">fetchOptions</code> for the <code class=\"language-text\">fetch</code> call's options</li>\n<li><code class=\"language-text\">url</code> for the <code class=\"language-text\">fetch</code> call's API endpoint</li>\n<li><code class=\"language-text\">requestPolicy</code> to determine the cache's behavior</li>\n</ul>\n<p>The <code class=\"language-text\">executeQuery</code> call will return a <a href=\"https://github.com/kitten/wonka\">Wonka</a>\nstream. This is just an observable (not following the Observable spec)\nthat sends back the GraphQL request's result (an <strong>\"Operation Result\"</strong>).\nWhen all consumers unsubscribe from this stream however, it'll terminate\nany ongoing requests for this operation and free resources.</p>\n<p>This is done by sending the exact same operation as above (<code class=\"language-text\">exampleOperation</code>)\nbut with the <code class=\"language-text\">&#39;teardown&#39;</code> operation name:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> teardownOperation <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token operator\">...</span>exampleOperation<span class=\"token punctuation\">,</span>\n  operationName<span class=\"token punctuation\">:</span> <span class=\"token string\">'teardown'</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></code></pre></div>\n\n            <h2>\n              <a class=\"Anchor\"\n                aria-hidden=\"true\"\n                id=\"exchanges\"\n                href=\"#exchanges\"\n              >\n                <svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\"aria-hidden=\"true\">\n                  <path fill-rule=\"evenodd\"\n                    d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n                  />\n                </svg>\n              </a>\n              Exchanges\n            </h2>\n          \n<p>The next bit of <code class=\"language-text\">urql</code>'s inner workings is how these operations are handled.\nWhen a <code class=\"language-text\">new Client()</code> is created you may pass it <code class=\"language-text\">url</code> and <code class=\"language-text\">fetchOptions</code>.\nBut you can also pass an <code class=\"language-text\">exchanges</code> array.</p>\n<p><strong>Exchanges</strong> are operation handlers. It'll receive the <code class=\"language-text\">client</code> and a <code class=\"language-text\">forward</code>\nfunction as an object. It then returns a function accepting a stream of\noperations and returning a stream of operation results (i.e. GraphQL results).</p>\n<p>In other words, exchanges are handlers that fulfill our GraphQL requests.\nThey're Input/Output streams, inputs being operations, outputs being results.\nThey're also composable. The <code class=\"language-text\">forward</code> function that an <strong>exchange</strong> receives\nis just another Input/Output handler.</p>\n<p>In practice the signature is:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">type ExchangeInput <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> forward<span class=\"token punctuation\">:</span> ExchangeIO<span class=\"token punctuation\">,</span> client<span class=\"token punctuation\">:</span> Client <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\ntype <span class=\"token function-variable function\">Exchange</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">input<span class=\"token punctuation\">:</span> ExchangeInput</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> ExchangeIO<span class=\"token punctuation\">;</span>\ntype <span class=\"token function-variable function\">ExchangeIO</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">Source<span class=\"token operator\">&lt;</span>Operation<span class=\"token operator\">></span></span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> Source<span class=\"token operator\">&lt;</span>OperationResult<span class=\"token operator\">></span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>The simplest yet useful exchange would be one that accepts all operations and\nimmediately sends them to a GraphQL API with a fetch call. And in fact,\nthat is what the default <code class=\"language-text\">fetchExchange</code> does.</p>\n<p>The default exchanges that a client will create when custom no <code class=\"language-text\">exchanges</code>\nare passed to it are:</p>\n<ul>\n<li><code class=\"language-text\">dedupExchange</code>: Deduplicates pending operations (pending = waiting for a response)</li>\n<li><code class=\"language-text\">cacheExchange</code>: All caching logic for operations and results</li>\n<li><code class=\"language-text\">fetchExchange</code>: Sends an operation to the API and returns results</li>\n</ul>\n<p>The <code class=\"language-text\">client</code> accepts exchanges and composes them using the <code class=\"language-text\">composeExchanges</code> function\nthat is also exported by <code class=\"language-text\">urql</code>.</p>\n<p>In essence these exchanges build a pipeline that runs in the order in which the\nexchanges are in the list above.</p>\n<p><strong>First,</strong> ongoing operations are deduplicated. It wouldn't make sense to send the\nsame operation / request twice at the same time.</p>\n<p><strong>Second,</strong> operations are checked against the cache. Depending on the <code class=\"language-text\">requestPolicy</code>\ncached results can be resolved instead and results from network requests are cached.</p>\n<p><strong>Third,</strong> operations are sent to the API and the result is normalized.</p>\n\n            <h2>\n              <a class=\"Anchor\"\n                aria-hidden=\"true\"\n                id=\"operation-results\"\n                href=\"#operation-results\"\n              >\n                <svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\"aria-hidden=\"true\">\n                  <path fill-rule=\"evenodd\"\n                    d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n                  />\n                </svg>\n              </a>\n              Operation Results\n            </h2>\n          \n<p>Every operation that enters the exchange pipeline will receive a result, either\nimmediately (read: synchronously) or eventually as the result of a network\nrequest comes in.</p>\n<p>The raw GraphQL result from an API is: <code class=\"language-text\">{ data?: T, errors?: GraphQLError[], extensions?: Record&lt;string, any&gt; }</code>.\nAnd <code class=\"language-text\">urql</code>'s operation results are very similar: <code class=\"language-text\">{ data?: T, error?: CombinedError, extensions?: Record&lt;string, any&gt;}</code>.</p>\n<p>The <a href=\"https://github.com/FormidableLabs/urql/blob/master/src/utils/error.ts\"><code class=\"language-text\">CombinedError</code> is a very simple wrapper</a>\nthat has either a <code class=\"language-text\">networkError</code> property with any unexpected errors that might occur,\nor a <code class=\"language-text\">graphQLErrors</code> array with the list of errors that have been returned by the API.</p>\n<p>This is a convenience wrapper that helps when the specific <em>kind of error</em> that has occured\ndoes not matter.</p>\n<p>Additionally <code class=\"language-text\">urql</code>'s operation results will also have the <code class=\"language-text\">operation</code> property, which\njust contains the original operation itself, which is how the client can tell which result\nit has just received.</p>\n","tocArray":[{"content":"Architecture","level":1},{"content":"Requests","level":2},{"content":"Operations","level":2},{"content":"Exchanges","level":2},{"content":"Operation Results","level":2}]}}
