New features in TypeScript that can improve our GraphQL workflow.
This talk has been presented at GraphQL Galaxy 2020, check out the latest edition of this Tech Conference.
New features in TypeScript that can improve our GraphQL workflow.
This talk has been presented at GraphQL Galaxy 2020, check out the latest edition of this Tech Conference.
The integration of GraphQL and TypeScript is crucial for maintaining type consistency between the GraphQL schema and TypeScript code, particularly in resolvers. This synchronization helps prevent runtime errors by ensuring that any changes in the schema are accurately reflected in the TypeScript implementation.
GraphQL Cogen is an open-source tool that automates the generation of code and types from GraphQL schemas to enhance developer experience and type safety. It supports multiple languages and allows for the creation of custom plugins to tailor the output to specific needs.
The TypeScript plugin in GraphQL Cogen generates TypeScript types directly from a GraphQL schema. It facilitates type safety by creating types that are dynamic and based on the specific GraphQL operations used, such as queries and mutations.
The TypeScript React Apollo plugin generates ready-to-use React hooks based on Apollo Client and GraphQL operations. It pre-compiles these operations into hooks that include built-in type validation and autocomplete for variables and result types, simplifying the development process.
A Type Document Node is a concept in GraphQL Cogen where a GraphQL operation is converted into an object that includes type information for both the variables and the result. This allows developers to use these types without having to manually specify them, enhancing type safety and reducing the risk of errors.
TypeScript 4.1 introduces enhanced capabilities for manipulating strings at the compiler level. This includes creating types from strings that can dynamically extract and manipulate substrings based on specified patterns, increasing the flexibility and power of type creation in TypeScript.
The next evolution in integrating TypeScript with GraphQL involves using TypeScript to parse and validate GraphQL strings directly, aiming to create types and resolvers on the fly. This approach seeks to further tighten the integration and enhance the developer experience by leveraging TypeScript's capabilities at the compiler level.
I'm Lhothan, the CTO of the Guild, a group of open source developers focused on GraphQL and TypeScript integration. Keeping the GraphQL schema and TypeScript code in sync is crucial to reduce runtime errors. GraphQL Cogen is a flexible tool that generates TypeScript types and code to improve developer experience and type safety. Let's dive into the most popular plugins, starting with TypeScript, which generates types based on the GraphQL schema and operations. We also generate code, like the TypeScript React Apollo plugin.
Hi, everyone. My name is Lhothan. I am the CTO of a group called the Guild. We're a group of open source developers mainly focused on GraphQL and its ecosystem. I'm here today to talk with you about GraphQL, TypeScript, the integration of the two, and tooling around it. And also a bit about the next generation of TypeScript and GraphQL integration.
So, let's start with the basics. Why do we even need this kind of integration? Each language has its own type system. GraphQL has its own type system where you define your GraphQL schema. It's called SDL and there is TypeScript which has its own type system. So, while writing your GraphQL schema, you need to keep in sync your GraphQL SDL where you define the actual schema and your TypeScript code where you write your actual implementation in resolvers. You can either do it manually and keep it in sync manually or you can use tooling for that. This is what I'm going to discuss in this case.
Keeping the two type systems in sync is very important because, consider a field that you have removed or added to your GraphQL schema, it needs to be reflected in the TypeScript code. So keeping this two in sync is important and will probably reduce most of your runtime errors related to GraphQL and TypeScript. So a few years ago I had issues with GraphQL and TypeScript, so I started with a small open source called GraphQL Cogen. This was four years ago and today we have a huge community and we're not generating only TypeScript it also flows C Sharp, Java much more, and we're generating more than just types, we're generating like an actual code that can help you to improve your developer experience and your type safety. Cogen is a very flexible tool you can write your own plugins and configure the output hook into the generation process and add your own code so it's pretty cool. I'm going to talk a bit about the most popular plugins that we have today that are related to TypeScript.
So the first one is TypeScript, the one that I mentioned before. This plugin just generates TypeScript types out of your GraphQL schema and this seems very similar because the type systems are kind of similar and we aim to generate code in TypeScript that is similar as possible to the GraphQL schema. But GraphQL schema isn't all. We also have queries mutations and everything that involves a selection set which is one of the most powerful features of GraphQL, the ability to choose fields and select exactly what you need and not more than that. So given the fact that you can choose the fields it means that your types are kind of dynamic and based on your selection of fields. So we are also generating types based on your GraphQL operations. So when you use it in your code you'll have fully typed safety. But this is only types. We're also generating more. As I said we're generating code. So here is a small example for another plugin called TypeScript React Apollo.
The plugin generates ready-to-use React hooks based on Apollo Client and your operations. It pre-compiles the GraphQL operations and creates hooks based on the operation name. No manual type specification is needed, as types are burned into the generated hooks. The tool also generates a complete Resolvers signature, utilities for various frameworks, and supports multiple languages and integration with various tools. Give it a try with the live demo on our website.
And this plugin generates ready-to-use React hooks based on Apollo Client and on your operations. So it basically takes your GraphQL operations, pre-compiles those and creates ready-to-use hooks based on your operation name. So when you use it in your code, all you need to do is just to import the hook, provide your variables and then use the data.
And the idea is that we are burning the types into the generated hooks. So you don't need to specify manually types or anything like that. You just get autocomplete and type validation for the variables and the result type. So this is pretty cool. As I said, we're not generating just types and that, we're generating more.
There is Resolvers signature. So if you are a backend developer writing your GraphQL schema and implementation, you can generate a complete Resolvers signature out of your schema and integrate your own model types into that signature. We're generating a few utilities for Apollo Client, but not only, also for Oracle, GraphQL request, Vue, Stencil, Angular, whatever, you name it. And it's not only JavaScript and TypeScript, we're also generating more for more languages, and we're integrating basically with every tool that's available on this kind of ecosystem. So, you can use it with Webpack and with Gatsby and Next.js. And recently we introduced a new plugin for React query, if you're using it. So, give it a try, we have a very nice live demo on our website, you can just try it and see what is the output based on the input which you can just change or add. So, give it a try.
We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career
Comments