<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lorenzo Di Giacomo</title>
    <description>The latest articles on DEV Community by Lorenzo Di Giacomo (@morrys).</description>
    <link>https://dev.to/morrys</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%2F219869%2Fe58fe2ed-4667-4234-b070-a4b4a468f092.jpeg</url>
      <title>DEV Community: Lorenzo Di Giacomo</title>
      <link>https://dev.to/morrys</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/morrys"/>
    <language>en</language>
    <item>
      <title>Welcome to Relay Hooks</title>
      <dc:creator>Lorenzo Di Giacomo</dc:creator>
      <pubDate>Fri, 07 Aug 2020 11:50:47 +0000</pubDate>
      <link>https://dev.to/morrys/welcome-to-relay-hooks-53eh</link>
      <guid>https://dev.to/morrys/welcome-to-relay-hooks-53eh</guid>
      <description>&lt;p&gt;Hi everyone, my name is Lorenzo (&lt;a href="https://github.com/morrys/"&gt;morrys&lt;/a&gt;), today I want to introduce you to &lt;a href="https://www.npmjs.com/package/relay-hooks"&gt;relay-hooks&lt;/a&gt;, a library that has allowed me to meet valid people and which is also giving me a lot of satisfaction.&lt;br&gt;
The library was born after a simple chat with &lt;a href="https://github.com/sibelius/"&gt;Sibelius&lt;/a&gt; in which I asked why no one had ever implemented relay hooks.&lt;br&gt;
His response was short: &lt;strong&gt;DO IT&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In Early May sibelius added me to the &lt;a href="https://github.com/relay-tools"&gt;relay-tools&lt;/a&gt; organization and on the 13th of May 2019 I made the first commit and first official release of the library on npm.&lt;/p&gt;

&lt;p&gt;The initial purpose of the library was to provide the ability to use all react-relay HOCs as react hooks and to implement the store-or-network and store-only policies used by the &lt;a href="https://github.com/morrys/react-relay-offline"&gt;react-relay-offline&lt;/a&gt; library to manage offline relay applications.&lt;/p&gt;

&lt;p&gt;After Relay's core team shared information about the the initial differences in the issue &lt;a href="https://github.com/relay-tools/relay-hooks/issues/5"&gt;https://github.com/relay-tools/relay-hooks/issues/5&lt;/a&gt;, all the necessary changes were made in order to make relay-hooks as close as possible to their specifications.&lt;/p&gt;
&lt;h2&gt;
  
  
  current differences with upcoming Relay Hooks in react-relay
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useLazyLoadQuery&lt;/strong&gt;: in the official version returns a single data object with the query's data, and nothing else while in relay-hooks returns the same object of the render function of the QueryRenderer HOC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;useFragment&lt;/strong&gt;, &lt;strong&gt;usePagination&lt;/strong&gt;, &lt;strong&gt;useRefetchable&lt;/strong&gt; and &lt;strong&gt;useMutation&lt;/strong&gt; not use suspense&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  what's more in relay-hooks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useQuery&lt;/strong&gt;: it is the same as useLazyLoadQuery  but does not use suspense, it allows you to use hooks without having to  migrate the application in concurrent mode and its return is the same  as the QueryRenderer HOC

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useRefetch&lt;/strong&gt;: it is the same as useRefetchable, allows you to migrate the Refetch Container without changing the fragment specifications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;conditional useQuery &amp;amp; useLazyLoadQuery&lt;/strong&gt;: added skip: [Optional] If skip is true, the query will be skipped entirely&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;alternative way to create the "&lt;a href="https://reactjs.org/docs/concurrent-mode-suspense.html#approach-3-render-as-you-fetch-using-suspense"&gt;render-as-you-fetch&lt;/a&gt;" pattern&lt;/strong&gt; both for concurrent and non-concurrent modes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  why use relay-hooks?
&lt;/h2&gt;

&lt;p&gt;It is a stable library and none of its dependencies is experimental and it allows you to immediately use react hooks with &lt;strong&gt;relay-runtime&lt;/strong&gt; and it is designed for easy migration to react-relay hooks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Relay Hooks&lt;/strong&gt; is a light library and fully compatible with &lt;a href="https://reactjs.org/docs/concurrent-mode-intro.html"&gt;React Concurrent Mode&lt;/a&gt; and React Legacy Mode (i.e. outside of Concurrent Mode). They are also fully compatible with &lt;a href="https://relay.dev/docs/en/introduction-to-relay"&gt;existing Relay APIs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rAV0cDnM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3kuyicrv9t87v5w2nd1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rAV0cDnM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3kuyicrv9t87v5w2nd1.jpg" alt="Photo by Braden Collum on Unsplash"&gt;&lt;/a&gt;&lt;/p&gt;
 Photo by Braden Collum on Unsplash 


           


&lt;h1&gt;
  
  
  Getting started
&lt;/h1&gt;

&lt;p&gt;First, let's install the packages we need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# NPM Users
npm install --save relay-runtime relay-hooks
npm install --save-dev relay-compiler graphql babel-plugin-relay

# Yarn Users
yarn add relay-runtime relay-hooks
yarn add --dev relay-compiler graphql babel-plugin-relay
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  1. Configure Relay Compiler
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://relay.dev/docs/en/experimental/step-by-step#41-configure-relay-compiler"&gt;Here you will find the official documentation on how to configure relay compiler&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Configure Relay Runtime
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://relay.dev/docs/en/experimental/step-by-step#42-configure-relay-runtime"&gt;Here you will find the official documentation on how to configure relay runtime&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Connect Relay Runtime to React
&lt;/h2&gt;

&lt;p&gt;You connect Relay Runtime to React with the RelayEnvironmentProvider component. The &lt;code&gt;RelayEnvironmentProvider&lt;/code&gt; is similar to React's &lt;a href="https://reactjs.org/docs/context.html#contextprovider"&gt;Context.Provider&lt;/a&gt;.  It wraps your React app and places the client on the context, which  enables you to access it from anywhere in your component tree.&lt;/p&gt;
&lt;h3&gt;
  
  
  RelayEnvironmentProvider
&lt;/h3&gt;

&lt;p&gt;Since queries with &lt;code&gt;useQuery&lt;/code&gt; no longer set context, we will expose a new &lt;code&gt;RelayEnvironmentProvider&lt;/code&gt; component that takes an &lt;code&gt;environment&lt;/code&gt; and sets it in context; variables will no longer be part of context. A &lt;code&gt;RelayEnvironmentProvider&lt;/code&gt; should be rendered once at the root of the app, and multiple useQuery's can be rendered under this environment provider.&lt;/p&gt;


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



&lt;h2&gt;
  
  
  4. Use Relay as React hooks
&lt;/h2&gt;

&lt;h3&gt;
  
  
  useRelayEnvironment
&lt;/h3&gt;

&lt;p&gt;Hook used to access a Relay environment that was set by a &lt;code&gt;RelayEnvironmentProvider&lt;/code&gt;:&lt;/p&gt;


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


&lt;h3&gt;
  
  
  useQuery
&lt;/h3&gt;

&lt;p&gt;Hook used to fetch a GraphQL query during render for React Legacy Mode (i.e. outside of Concurrent Mode).&lt;br&gt;
&lt;code&gt;useQuery&lt;/code&gt; does not take an environment as an argument.  Instead, it reads the environment set in the context; this also implies  that it does not set any React context. In addition to &lt;code&gt;query&lt;/code&gt; (first argument) and &lt;code&gt;variables&lt;/code&gt; (second argument), &lt;code&gt;useQuery&lt;/code&gt; accepts a third argument &lt;code&gt;options&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetchPolicy&lt;/code&gt;: determine whether it should use data cached in the Relay store and whether to send a network request. The options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;store-or-network&lt;/code&gt; (default): Reuse data cached in the store; if the whole query is cached, skip the network request&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;store-and-network&lt;/code&gt;: Reuse data cached in the store; always send a network request.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;network-only&lt;/code&gt;: Don't reuse data cached in the store; always send a network request. (This is the default behavior of Relay's existing QueryRenderer.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;store-only&lt;/code&gt;: Reuse data cached in the store; never send a network request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;fetchKey&lt;/code&gt;: [Optional] A fetchKey can be passed to force a  refetch of the current query and variables when the component  re-renders, even if the variables didn't change, or even if the  component isn't remounted (similarly to how passing a different key to a  React component will cause it to remount). If the fetchKey is different  from the one used in the previous render, the current query and  variables will be refetched.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;networkCacheConfig&lt;/code&gt;: [Optional] Object containing cache  config options for the network layer. Note the the network layer may  contain an additional query response cache which will reuse network  responses for identical queries. If you want to bypass this cache  completely, pass {force: true} as the value for this option.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;skip&lt;/code&gt;: [Optional] If skip is true, the query will be skipped entirely&lt;/p&gt;


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


&lt;h3&gt;
  
  
  useLazyLoadQuery
&lt;/h3&gt;

&lt;p&gt;Hook used to fetch a GraphQL query during render for &lt;a href="https://reactjs.org/docs/concurrent-mode-intro.html"&gt;React Concurrent Mode&lt;/a&gt; and the arguments are the same as useQuery&lt;/p&gt;


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


&lt;h3&gt;
  
  
  useFragment
&lt;/h3&gt;

&lt;p&gt;useFragment allows components to specify their data requirements. A container does not directly fetch data, but instead declares a specification of the data needed for rendering, and then Relay will guarantee that this data is available before rendering occurs.&lt;br&gt;
The hook is automatically subscribed to updates to the fragment data: if the data for this particular &lt;code&gt;User&lt;/code&gt;  is updated anywhere in the app (e.g. via fetching new data, or mutating  existing data), the component will automatically re-render with the  latest updated data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fragment&lt;/code&gt;: GraphQL fragment specified using a graphql template literal.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fragmentReference&lt;/code&gt;: The fragment reference  is an opaque Relay object that Relay uses to read the data for the  fragment from the store; more specifically, it contains information  about which particular object instance the data should be read from. 

&lt;ul&gt;
&lt;li&gt;The type of the fragment reference can be imported from the generated Flow/Typescript types, from the file .graphql.js, and can be used to declare the type of your Props. The name of the fragment reference type will be: $key. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt;: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.&lt;/li&gt;
&lt;/ul&gt;


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


&lt;h3&gt;
  
  
  useRefetch
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;useRefetch&lt;/code&gt; when you want to fetch and re-render a fragment with different data and the arguments are the same as useFragment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They are the same as useFragment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tuple containing the following values&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[0] &lt;code&gt;data&lt;/code&gt;: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.&lt;/li&gt;
&lt;li&gt;[1] &lt;code&gt;refetch&lt;/code&gt;: Function used to refetch the fragment with a potentially new set of variables.&lt;/li&gt;
&lt;/ul&gt;


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


&lt;h3&gt;
  
  
  useRefetchable
&lt;/h3&gt;

&lt;p&gt;It is the same as &lt;code&gt;useRefetch&lt;/code&gt; but a refetch query no longer needs to be specified in this api, since it will be automatically generated by Relay by using a &lt;code&gt;@refetchable&lt;/code&gt; fragment.&lt;/p&gt;


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


&lt;h3&gt;
  
  
  usePagination
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;usePagination&lt;/code&gt; to render a fragment that uses a &lt;code&gt;@connection&lt;/code&gt; and paginate over it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They are the same as useFragment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tuple containing the following values&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[0] &lt;code&gt;data&lt;/code&gt;: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.&lt;/li&gt;
&lt;li&gt;[1] &lt;code&gt;paginationFunctions&lt;/code&gt;: loadMore, hasMore, isLoading, refetchConnection&lt;/li&gt;
&lt;/ul&gt;


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


&lt;h3&gt;
  
  
  useMutation
&lt;/h3&gt;

&lt;p&gt;You can use useMutation to execute a mutation in a React component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;fragment&lt;/code&gt;: GraphQL fragment specified using a graphql template literal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;commitMutationFn&lt;/code&gt;: An optional function with the same signature as commitMutation to call in its stead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tuple containing the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[0] &lt;code&gt;mutate&lt;/code&gt;: The function that will execute the mutation&lt;/li&gt;
&lt;li&gt;[1] &lt;code&gt;mutationState&lt;/code&gt;: loading, data, error&lt;/li&gt;
&lt;/ul&gt;


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


&lt;h3&gt;
  
  
  useSubscription
&lt;/h3&gt;

&lt;p&gt;Hook used to subscribe and unsubscribe to a subscription.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  5. Render-as-you-fetch
&lt;/h2&gt;

&lt;p&gt;In relay-hooks I have implemented an alternative way to create the "&lt;a href="https://reactjs.org/docs/concurrent-mode-suspense.html#approach-3-render-as-you-fetch-using-suspense"&gt;render-as-you-fetch&lt;/a&gt;" pattern both for concurrent and non-concurrent modes.&lt;/p&gt;

&lt;h3&gt;
  
  
  loadQuery (Non-React API)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;same as useQuery + environment&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return value:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;next&lt;/code&gt;: ( environment: IEnvironment, gqlQuery:  GraphQLTaggedNode, variables?: TOperationType['variables'], options?:  QueryOptions, ) =&amp;gt; Promise:  fetches data. A promise returns to allow the await in case of SSR&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dispose: () =&amp;gt; void&lt;/code&gt;: cancel the subscription and dispose of the fetch&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;subscribe: (callback: (value: any) =&amp;gt; any) =&amp;gt; () =&amp;gt; void&lt;/code&gt;:  used by the usePreloadedQuery&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getValue: (environment?: IEnvironment) =&amp;gt; RenderProps&amp;lt;TOperationType&amp;gt; | Promise&amp;lt;any&amp;gt;&lt;/code&gt;:  used by the usePreloadedQuery&lt;/li&gt;
&lt;/ul&gt;


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


&lt;h3&gt;
  
  
  loadLazyQuery
&lt;/h3&gt;

&lt;p&gt;loadLazyQuery is the same as loadQuery but must be used with &lt;a href="https://reactjs.org/docs/concurrent-mode-intro.html"&gt;React Concurrent Mode&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  usePreloadedQuery
&lt;/h3&gt;

&lt;p&gt;Hook used to access data fetched by an earlier call to &lt;code&gt;loadQuery&lt;/code&gt; or from &lt;code&gt;loadLazyQuery&lt;/code&gt;. This implements the &lt;code&gt;Render-as-You-Fetch&lt;/code&gt; pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arguments:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;loadQuery | loadLazyQuery return value&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return Value:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;same as useQuery&lt;/p&gt;


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


&lt;h3&gt;
  
  
  Render-as-you-fetch Examples:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/relay-tools/relay-hooks/tree/master/examples/suspense/nextjs-ssr-preload"&gt;Complete example with nextjs, SSR in React Concurrent&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/relay-tools/relay-hooks/tree/master/examples/relay-hook-example/nextjs-ssr-preload"&gt;Complete example with nextjs, SSR in React Legacy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Conclusions:
&lt;/h1&gt;

&lt;p&gt;Relay-hooks are great. They simplify development by improving DX, reducing the bundle size and allowing you to implement the new "render-as-you-fetch" pattern.&lt;br&gt;
Now you have to try them :)&lt;/p&gt;

</description>
      <category>relay</category>
      <category>hooks</category>
      <category>graphql</category>
      <category>react</category>
    </item>
  </channel>
</rss>
