Michał Tyszkiewicz
2/15/2021
The big trend for a while now has been GraphQL and its associated technologies growing popularity among the dev community. However there’s also another language that has been making the rounds, namely TypeScript. Last year it became the 4th most popular programming language on GitHub. If you’re looking to get ahead of the curve it's probably high time to get to know both and if not it's always good to check out what’s new in the community. Anyway let’s take a look at what using Typescript with GraphQL can do for you.
To put it short GraphQL is a query language for APIs, its most touted feature is fetching the exact data you queried for without over or under fetching. I’ve already written a bit about its features so if you’re looking for more info about it you can check it out. Let’s focus more on TypeScript then, it's basically a superset of JavaScript. This means all valid JavaScript code is valid TypeScript code and if you know one getting to know the other will be a lot easier. That’s not all as TypeScript is often called JavaScript on steroids because it has a lot of additional features.
So both languages use types, but in a different way. As we know with GraphQL you use its Schema Definition Language to build a schema and define types, mutations and queries. Then you also need to write resolvers, which are just JavaScript functions so they don't carry over GraphQL types from the schema. This can mean it may not match the types from other sources of data, like from Apollo for example. It can be solved by using a language which supports types, like (surprise) TypeScript. This way your GraphQL resolvers will be typed (with args, parent type, inputs and return value) and you can use your type models and have everything type safe from API to database. GraphQL will handle types for the schema and Typescript for the resolvers. Sounds pretty rosy, but you’re probably thinking about writing all this and how much time and effort that would take.
If only there was a way to make it all easier. Well as you might’ve guessed it we’re in luck there's a little thing which can do a lot of the work for us, a framework aptly named TypeGraphQL.
Normally you’d have to write all the schema types using SDL, create data models using ORM classes, write resolvers for all the queries, mutations and fields and create TypeScript interfaces for all arguments, inputs and object types. After all that you can implement the resolvers using generic signatures and you’ll still need to manually do things like validation, authorization and loading dependencies. This leads to a lot of code redundancy as changing a type in one place will require making changes in a number of places from the schema to the interface. It's also obviously tiresome as a single mistake will make you double check everything again. This is where TypeGraphQL comes in.
TypeGraphQL is currently in version 1.0 and has a lot of plans for the future which looks pretty bright with how popular both GraphQL and TypeScript are becoming. The easy to see conclusion is that combining the strengths of TypeScript and GraphQL leads to a properly structured code, which is easy to reuse, maintain and scale. Yes there is some learning curve and initial growing pains needed to get a hang of all the kinks and features but the payoff is already substantial and will only grow as more projects start to use both technologies. That looking at the trends seems to be inevitable.