Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator

Arda TANRIKULU
Dec 4, 2018 · 2 min read
GraphQL Code Generator will boost your DX

Last year we’ve released GraphQL-Modules — which applies modular approach to large scale, GraphQL-based applications.

We’ve also released a new version of GraphQL Code Generator which generates server-side typings and signatures for resolvers from GraphQL Schemas.

Those are two separate projects, but because we use both in production across many different applications, we wanted to write about the benefits of the tight integration of those projects and how it provides us with great development experience while implementing our projects in TypeScript.

GraphQL Code Generator can take a JS/TS export from GraphQL-Modules to generate server-side typings (or anything else that its plugins can generate). So, let’s export typeDefs without any business logic that includes resolvers, DI, providers etc…

You can create a schema.ts file to expose typeDefs to GraphQL Code Generator:

Expose type definitions without business logic

Create a codegen.yml config file, including schema.ts as a schema source, and generate common and server typings for TypeScript.
We use transpile-only to prevent errors related to TypeScript on typing generation phase to let ts-node handle this on running the actual application.

Simple configuration to connect your GraphQL-Modules application to GraphQL Code Generator

We can add a script to package.json to run:

With a single command, you can generate necessary typings

Then, we can use these typings in our project. This example shows a resolvers handler that implements resolvers for Query type with generated types.

With a small change in your existing resolvers code, you can get a strict-typed resolver

At the end, we have a strictly-typed backend project, based of separate feature modules, and each of those modules uses GraphQL and TypeScript.

You can check our working example that is created by using this approach;
https://github.com/darkbasic/graphql-modules-seed

All posts about GraphQL Modules

  1. GraphQL Modules — Feature based GraphQL Modules at scale
  2. Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?
  3. Why did we implement our own Dependency Injection library for GraphQL-Modules?
  4. Scoped Providers in GraphQL-Modules Dependency Injection
  5. Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator
  6. Authentication and Authorization in GraphQL (and how GraphQL-Modules can help)
  7. Authentication with AccountsJS & GraphQL Modules
  8. Manage Circular Imports Hell with GraphQL-Modules

Follow us on GitHub and Medium, we are planning to release many more posts in the next couple of weeks about what we’ve learned using GraphQL in recent years.

Thanks to Dotan Simha

Arda TANRIKULU

Written by

JavaScript Enthusiast, member of The Guild https://github.com/ardatan

The Guild

The Guild

The Guild

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade