GraphQL Code Generator v3: generate GraphQL types has never been easier!

Rate this content
Bookmark

GraphQL Code Generator v3 brings the easiest and most lightweight way to get typed GraphQL in your front-ends by only generating types. In this talk, we will see that generating GraphQL types has never been so easy!

Charly Poly
Charly Poly
22 min
08 Dec, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Hi, everyone! Today, I'll be discussing the future of GraphQL Code Generator and the new features that enhance the developer experience. Code Gen has evolved significantly since its creation in 2016, from generating simple types to hooks and now to a new feature. It has expanded to support multiple languages and framework-specific plugins. Code Gen is widely used in the GraphQL stack, with 7 million monthly downloads on NPM. The new features include Type Document Node, which allows typing GraphQL operations without hooks, and the Client Preset, an improved version of Type Document Node. The Client Preset provides a seamless developer experience by generating the GraphQL function that returns the proper type document node type to your variable. Another interesting feature is fragment masking, which simplifies the usage of fragments in GraphQL and allows for better understanding of data dependencies within the component tree.

1. Introduction to GraphQL Code Generator

Short description:

Hi, everyone! Today, I'll be discussing the future of GraphQL Code Generator and the new features that enhance the developer experience. Code Gen has evolved significantly since its creation in 2016, from generating simple types to hooks and now to a new feature. It has expanded to support multiple languages and framework-specific plugins. Code Gen is widely used in the GraphQL stack, with 7 million monthly downloads on NPM.

Hi, everyone, and thank you for being here. I'm happy to be here to talk about the future of GraphQL Code Generator and to new features that we added recently that brings a brand new developer experience. So let's get started. My name is Charlie. I've been working full time on Code Gen for the last six months and especially working on the feature I'm going to showcase today and on the upcoming roadmap for Code Gen.

So, before looking at this new developer experience that Code Gen is bringing, let's take a look at the history of Code Gen and how we went from generating simple type to hooks to the new feature that we're going to see today. So, Code Gen has been created in 2016 to first generate schema types, plain TypeScript types, and then a bit later, in the same year, generating operation types — so, for the backend and also for frontend. But the first version of Code Gen, before 1.0, was just generating plain TypeScript types. Then, a bit later on, Code Gen started to bring support for generating types to many types of languages beyond TypeScript and JavaScript, such as Java, C Sharp, but also Kotlin, etcetera. And a bit later on, again, due to high demand on the front end, Code Gen started to generate like framework-specific plugins, such as Apollo Client, Yorkle, Vue, Angular, and so on. Code Gen, nowadays, is quite famous and present in most GraphQL stack, especially on the client side, with 7 million downloads per month on NPM.

2. Type Document Node and its Benefits

Short description:

Code Gen is widely used on the front end, especially for TypeScript stack, and on the back end with any kind of GraphQL server implementation. While generated hooks and SDK are popular, they come with limitations such as replicating signature changes and an exponential number of options. These drawbacks led to the experiment called Type Document Node, which allows typing GraphQL operations without hooks. Type Document Node provides a perfect integration with clients, offering type checking on variables and results. It is supported by various GraphQL clients in the TypeScript ecosystem.

And if we look at popular usage, Code Gen is very famous on the front end side, especially for TypeScript stack, but also on the back end, with TypeScript stack, but any kind of GraphQL server implementation. And if we look closer on the front end, because this is the subject of today, we're gonna talk about a new experience on the front end, we see that, nowadays, people are very, very happy with generated hooks in SDK.

So while generated hooks and SDK are very famous, we took a step back and looked at also the drawbacks that comes with this certain approach. First, the limitation of hooks and SDK that are generated by Code Gen are, first, replicating signature changes. So since Code Gen now is responsible of wrapping a lot of libraries on the frontend, like React, Query, Apollo, your call in React, but your call in Vue, Apple Angular, etc. All those libraries are living their own life and changing quite often and offering different spectrum of different options.

So we have to make sure that every time we wrap a library, we are providing access to the underlying specific options of each library and we are staying aligned with the different major versions. So this leads to a lot of pull requests, issues of implementation that are sometimes community-driven, that are not following the evolution of the wrapped libraries. The second one is the exponential number of options. So Kodrain itself has a lot of options to make it more tailored, more configurable and to make sure it fits well your usage of TypeScript in your GraphQL stack. But since we are wrapping existing libraries and it has to be flexible, also each plugin gets its own set of options on top of the dozens of options that core packages of Kodrain are offering. So this makes it very hard in the long run to make sure that every introduced new option is compatible with existing one, and that upgrading or changing the behavior of existing options is not going to bring anything in any type of plugin that started to diverge from each other. And finally, the last point is bundle size impact. So we are in a new era of front end and web application where bundle performance and you know bandwidth impact is very important. And Kodrain, when generating like wrappers such as SDK, but especially generating hooks, is generating all this code just to make sure that we forward the types properly and we type check the variable properly.

So all those of the version led to a very specific experiment that has been called Type Document Node. So Type Document Node, it's an experiment that started in 2020, and it started with a simple question. What if we could type GraphQL operations without hooks? What if we could bring the same awesome developer experience that everybody likes around using hooks that are similar to React but without actually generating hooks but using only types?

So this is a query on the left that returns a rate, for example for product, on a given currency. On the right, this is the code that is generated by CodeGen when using the TypeDocumentNode plugin. So as you can see, it's very similar to the early days of CodeGen. Very similar to what the TypeScript and TypeScript operations plugins are generating. The only difference is that here we use a TypeDocumentNode and TypeDocumentNode is a superset of the DocumentNode object and type from the GraphQL.js library and TypeDocumentNode, unlike DocumentNode, is typed properly by carrying the type of the query and the type of the variables. And this allows to have a perfect integration with your client. Here, in this example, you can see that we just import our document, so like you do in any application, you import your document and you pass it directly to the use query of Apollo. And immediately, you get type checking on the variable and you get type result. So here is the data is typed properly with the right query type from TypeScript operations and same with sub properties. So this has been made possible because Dotan and other people from the community has been working very hard to make sure that TypeDocumentNode, this new type version of DocumentNode from GraphQL, JS is supported by all the GraphQL clients in the TypeScript ecosystem. So over the years, all the following work led to a perfect integration in the GraphQL front end TypeScript ecosystem. You can already use TypeDocumentNode with Apollo Client, ReoCall, Preact, React Query, even is the VR. And you can use it also in Svelte, Vue, et cetera.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.