<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leonardo Ascione on Medium]]></title>
        <description><![CDATA[Stories by Leonardo Ascione on Medium]]></description>
        <link>https://medium.com/@leonardoascione?source=rss-beb1382f78d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MGKWnDKy2raZXZVq.</url>
            <title>Stories by Leonardo Ascione on Medium</title>
            <link>https://medium.com/@leonardoascione?source=rss-beb1382f78d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:33:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leonardoascione/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[GraphQL Code Generator — Introducing Hooks support for React Apollo]]></title>
            <link>https://medium.com/the-guild/graphql-code-generator-introducing-hooks-support-for-react-apollo-2cdc8a7b526d?source=rss-beb1382f78d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cdc8a7b526d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[apollo]]></category>
            <dc:creator><![CDATA[Leonardo Ascione]]></dc:creator>
            <pubDate>Fri, 01 Mar 2019 17:00:02 GMT</pubDate>
            <atom:updated>2019-03-01T17:00:02.572Z</atom:updated>
            <content:encoded><![CDATA[<h3>GraphQL Code Generator -Introducing Hooks support for React Apollo plugin</h3><p>If you follow the React community, you’ll know for sure that <a href="https://reactjs.org/docs/hooks-intro.html">React Hooks</a> had been one of the most awaited feature in the ecosystem since their first gist. They have been available since React v16.7-alpha, and many libraries already started adopting them — <a href="https://react.i18next.com/latest/usetranslation-hook">officially</a> or with <a href="https://github.com/facebookincubator/redux-react-hook">auxiliary libraries</a>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fdpw9EHDh2bM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Ddpw9EHDh2bM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fdpw9EHDh2bM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/18155363c13115aea52730d2391542fe/href">https://medium.com/media/18155363c13115aea52730d2391542fe/href</a></iframe><p>In case you don’t know what hooks are, you may be wondering <strong>what all this buzz is about</strong>. Let the <a href="https://reactjs.org/docs/hooks-intro.html">React docs</a> speak for itself:</p><blockquote>Hooks let you use state and other React features without writing a class.</blockquote><p>This could be a huge improvement by itself (you know, you create a Functional Component, mess with it and then you need a bit of state so.. let’s refactor this to a class, hooray! 🎉 — <em>sarcasm is intentional</em>), but there is more.</p><h3>React Apollo and Hooks</h3><blockquote><strong>Note:</strong> If you already know all about hooks and <a href="https://github.com/trojanowski/react-apollo-hooks/">react-apollo-hooks</a>, and want to see the news about graphql-code-generator, just skip this section.</blockquote><blockquote>If you are interested in the long story instead, keep reading!</blockquote><p>There are many hooks, like useEffect or useReducer, that may simplify your code, but I’ll leave this <a href="https://reactjs.org/docs/hooks-overview.html">to your curiosity</a>. I suggest you to read the Dan Abramov (@gaereon) <a href="https://medium.com/@dan_abramov/making-sense-of-react-hooks-fdbde8803889">“Making sense of React Hooks”</a> story if you didn’t already.</p><p>What I want to talk about, instead, is useContext and how you will fall in love with it, especially when talking about react-apollo.</p><blockquote><strong>Note:</strong> If you haven’t used Context before, just think of it as a way to pass props down the components tree without passing props down component-by-component. <br>It should not replace normal React usage, but is useful when talking about cross-application values like state, translations, themes, etc.</blockquote><p>The new hook useContext allows us to access a React Context (<a href="https://reactjs.org/docs/context.html">with its Consumer/Provider api</a>) directly from a functional component, without props nor contextType:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E5CeVhx3ELbjCMy5GvDCNA.png" /></figure><p>Furthermore, since <strong>hooks</strong> are composable, we can do silly things, like accessing context <em>inside</em> another hook:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*Qva7yfK1tdGbOAgwSvWGoQ.png" /><figcaption>Pretty silly — I know 👀</figcaption></figure><p>Given this sweet feature, we can now think about all our HOCs / Render Props in our codebase, well, almost all: Every time we need to access context (<em>State Management, API Calls, Translations, Localization</em>) we can now use hooks.</p><p>Especially using TypeScript, deep <a href="https://www.reddit.com/r/reactjs/comments/85h93f/higher_order_component_hell/">HOCs tree-hell</a> or <a href="https://medium.com/@ntgard/why-i-dont-use-render-props-in-react-10f18abdff11">render-props callback hell</a> are a nightmare (Reminding Node.js callback hell, anyone?). Typings are always wrong, you need to define twenty different interfaces, etc.</p><p>With hooks, you can just use them in a straight, linear, fashion:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xO60q_NJBAEXeDYKXw0cKg.png" /><figcaption>Sometimes I think this is too good to be true and to wake up in an AngularJS 1.0 world.</figcaption></figure><p><a href="https://github.com/apollographql/react-apollo"><strong>React Apollo</strong></a> fits perfectly the requirements, and even if the official library still doesn’t support hooks, we can already use them thanks to the awesome<a href="https://github.com/trojanowski/react-apollo-hooks">react-apollo-hooks</a> library made by <a href="https://medium.com/u/a44eec263a83">Daniel Trojanowski</a>. Kudos!</p><p>If you are used to Query component, in the next example you’ll see how we are replacing it with just the useQuery hook:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ju5bZG6qy1ftO7jE2drLpg.png" /><figcaption>Farewell render props!</figcaption></figure><h3>React Apollo Hooks and GraphQL Code Generator</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*pqVeVv_U4guQfYmML6v2uA.png" /></figure><p>Since the first time I saw hooks, I thought about removing the callback hell caused by render props in my codebase. Given the awesome work done by <a href="https://medium.com/u/a44eec263a83">Daniel Trojanowski</a> with <a href="https://github.com/trojanowski/react-apollo-hooks">react-apollo-hooks</a>, I wanted to use <strong>hooks</strong> in our projects, replacing React Apollo classic components (<em>render-props based</em>).</p><p>However, I love even more the <a href="https://graphql-code-generator.com/">graphql-code-generator</a> project, since I want proper <strong>typings</strong> with my Query, Mutation and Subscription components. Accessing data with proper autocomplete and type checking is definitely a game-changer!</p><blockquote>I’m glad to have the honor to announce the next release of GraphQL Code Generator, that will add <strong><em>React Apollo</em></strong><em> </em><strong><em>hooks generation</em> </strong>to its arsenal</blockquote><p>With this enhancement, now you can choose between React Apollo Components, HOCs or Hooks and even mix-and-match if you’ve got an existing project and want to start using Hooks right now!</p><p>Just use GraphQL Code Generator’s <a href="https://graphql-code-generator.com/docs/plugins/typescript-react-apollo">Typescript-React-Apollo Plugin</a>, set withHooks: true to your GraphQL Code Generator config, and add react-apollo-hooks to your dependencies if you already didn’t.</p><p>This is an example generated hook, with proper typings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nyumNbXGEni5k1IyAthxOg.png" /></figure><p>And here we can see autocomplete at work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HeoEFfjK0uxQlNrAYLIRHg.png" /><figcaption>VSCode</figcaption></figure><p>If you want to see <em>graphgql-code-generator</em> in action, you can look at the awesome <a href="https://github.com/Urigo/WhatsApp-Clone-Client-React">WhatsApp-Clone-Client-React</a> project made by <a href="https://medium.com/the-guild">The Guild</a>. <a href="https://github.com/Urigo/WhatsApp-Clone-Client-React/compare/25cfe65bf8ca06ef6ee99bb7d3fd9df7ce1a0e7c...27987e87a2616e33782193fa3879d1660484ef64">Here is the diff</a> (thanks to <a href="https://medium.com/u/5a6962cd709e">Eytan Manor</a>) showcasing the generated hooks applied to a real codebase.</p><h3>Conclusions</h3><p>React Hooks will probably be a powerful tool in our toolbelt, and I’m sure we will see many patterns evolving. Libraries like React Apollo fits perfectly, and I hope having tools to generate typings like GraphQL Code Generator will increase their adoption.</p><p>I’d like to thank the awesome team behind The Guild, especially <a href="https://medium.com/u/5a6962cd709e">Eytan Manor</a> for its continuous effort reviewing my hooks proposal, <a href="https://medium.com/u/dc34560b69e4">Arda TANRIKULU</a> and <a href="https://medium.com/u/50dbdbe919ad">Dotan Simha</a> for their support and, obviously, the creation of grapqhl-code-generator. Thanks indeed to <a href="https://medium.com/u/a44eec263a83">Daniel Trojanowski</a> for the great work on react-apollo-hooks.</p><p>Thank you for reading this story, I hope you found it useful/interesting.<br>May the hook be with you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cdc8a7b526d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-guild/graphql-code-generator-introducing-hooks-support-for-react-apollo-2cdc8a7b526d">GraphQL Code Generator — Introducing Hooks support for React Apollo</a> was originally published in <a href="https://medium.com/the-guild">The Guild</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>