Thanks! We'll be in touch in the next 12 hours
Oops! Something went wrong while submitting the form.

Building Type Safe Backend Apps with Typegoose and TypeGraphQL

In this article, we will be trying to solve the most common problem encountered while trying to model MongoDB backend schema with TypeScript and Mongoose. We will also try to address and solve the difficulties of maintaining GraphQL types. 

Almost every serious JavaScript developer uses TypeScript. However, many aged libraries do not support it natively, which becomes an increasing issue as the project grows. Then, if you add up GraphQL, which is a great modern API development solution, it becomes too much of a boilerplate.

Prerequisites

This article assumes that you have working knowledge of TypeScript, MongoDB, and GraphQL. We’ll be using Mongoose for specifying models, which is the go-to Object Document Mapper (ODM) solution for MongoDB.

Let’s consider a basic example of a Mongoose model written in TypeScript. This might look something like the one mentioned below, a user model with basic model properties (email, first name, last name, and password):

CODE: https://gist.github.com/velotiotech/d79a4032b0dc5cefc8da44c8577f95ad.js

As you can see, it would be cumbersome to add and maintain interfaces manually with Mongoose. We would need at least 2-3 interfaces to occupy the typing needs to get model properties and methods working with proper typing.

Moving forward to add our queries and mutations, we need to create resolvers for the model above, assuming we have a service that deals with models. Here’s what our resolver looks like:

CODE: https://gist.github.com/velotiotech/0b0c2fe9bbe7cba3c65b46204feccefc.js

Not bad, we got our model and service and the resolver also looks good. But wait, we need to add GraphQL types as well. Here we are intentionally not including inputs to keep it short. Let’s do that:

CODE: https://gist.github.com/velotiotech/0524998dc516ba2c885e8f0cfd72dd01.js

Now, we have to club the schemas and resolvers together then pass them onto the GraphQL Express server—Apollo Server in this case:

CODE: https://gist.github.com/velotiotech/0cfa098b7a7ab8898da707d1fd29d1f7.js

With this setup, we got four files per model: model, resolver, service, and GraphQL schema file.

That’s too many things to keep in sync in real life. Imagine you need to add a new property to the above model after reaching production. You’ll end up doing at least following:

  1. Add a migration to sync the DB
  2. Update the interfaces
  3. Update the model schema
  4. Update the GraphQL schema

Possible Solution

As we know, after this setup, we’re mostly dealing with the entity models and struggling to keep its types and relations in sync.

If the model itself can handle it somehow, we can definitely save some effort, which  means we can sort things out if these entity model classes can represent both the database schema and its types.

Adding TypeGoose

Mongoose schema declarations with TypeScript can get tricky—or there might be a better way. Let’s add TypeGoose, so you no longer have to maintain interfaces (arguably). Here’s what the same user model looks like:

CODE: https://gist.github.com/velotiotech/bbcdb0db67a6d39ef5cd8cab106eb72b.js

Alright, no need for adding interfaces for the model and documents. You could have an interface for model implementation, but it’s not necessary.

With Reflect, which is used internally by TypeGoose, we managed to skip the need for additional interfaces.

If we want to add custom validations and messages, TypeGoose allows us to do that too. The prop decorator offers almost all the things you can expect from a mongoose model schema definition.

CODE: https://gist.github.com/velotiotech/0ca0e99b8c74467f8b031966bb4e6d09.js

Adding TypeGraphQL

Alright, TypeGoose has helped us with handling mongoose schema smoothly. But, we still need to define types for GraphQL. Also, we need to update the model types whenever we change our models. 

Let’s add TypeGraphQL

CODE: https://gist.github.com/velotiotech/42323ae89141a0c0c9bbe96c8e7e0738.js

What we just did is use the same TypeScript user class to define the schema as well as its GraphQL type—pretty neat.

Because we have added TypeGraphQL, our resolvers no longer need extra interfaces. We can add input classes for parameter types. Consider common input types such as CreateInput, UpdateInput, and FilterInput.

CODE: https://gist.github.com/velotiotech/235e4a1a65a1728b5c441e73655e1841.js

You can learn more about the syntax and input definition in the official docs.

That’s it. We are ready with our setup, and we can now simply build a schema and pass it to the server entry point just like that. No need to import schema files and merge resolvers. Simply pass array of resolvers to buildSchema:

CODE: https://gist.github.com/velotiotech/7ad5045aa37cac15bf800daa0d5daa94.js

Once implemented, this is how our custom demo project architecture might look:

Application Architecture
Fig:- Application Architecture

Limitations and Alternatives

Though these packages save some work for us, one may decide not to go for them since they use experimental features such as experimental decorators. However, the acceptance of these experimental features is growing.

TypeGoose:

Though TypeGoose offers a great extension to Mongoose, they've recently introduced some breaking changes. Upgrading from recent versions might be a risk. One alternative to TypeGoose for decorator-based schema definitions is TypeORM. Though, it currently has basic experimental support for MongoDB. 

TypeGraphQL:

TypeGraphQL is a well-maintained library. There are other options, like Nest.js and graphql-schema-decorators, which supports decorators for GraphQL schema. 

However, as Nest.js’s GraphQL support is more framework-oriented, it might be more than needed. The other one is not supported any longer. You can even integrate TypeGraphQL with Nest.js with some caveats.

Conclusion

Unsurprisingly, both of these libraries use experimental decorators API with Reflect Metadata. Reflect Metadata adds additional metadata support to the class and its members. The concept might look innovative but it’s nothing new. Languages like C# and Java support attributes or annotations that add metadata to types. With these added, it becomes handy to create and maintain well-typed applications.

One thing to note here would be—though the article introduces the benefits of using TypeGraphQL and TypeGoose together—it does not mean you can’t use them separately. Depending upon your requirements, you may use either of the tools or a combination of them.

This article covers a very basic setup for introduction of the mentioned technologies. You might want to learn more about advanced real-life needs with these tools and techniques from some of the articles mentioned below.

Further Reading


You can find the referenced code at this repo.

Get the latest engineering blogs delivered straight to your inbox.
No spam. Only expert insights.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings

Building Type Safe Backend Apps with Typegoose and TypeGraphQL

In this article, we will be trying to solve the most common problem encountered while trying to model MongoDB backend schema with TypeScript and Mongoose. We will also try to address and solve the difficulties of maintaining GraphQL types. 

Almost every serious JavaScript developer uses TypeScript. However, many aged libraries do not support it natively, which becomes an increasing issue as the project grows. Then, if you add up GraphQL, which is a great modern API development solution, it becomes too much of a boilerplate.

Prerequisites

This article assumes that you have working knowledge of TypeScript, MongoDB, and GraphQL. We’ll be using Mongoose for specifying models, which is the go-to Object Document Mapper (ODM) solution for MongoDB.

Let’s consider a basic example of a Mongoose model written in TypeScript. This might look something like the one mentioned below, a user model with basic model properties (email, first name, last name, and password):

CODE: https://gist.github.com/velotiotech/d79a4032b0dc5cefc8da44c8577f95ad.js

As you can see, it would be cumbersome to add and maintain interfaces manually with Mongoose. We would need at least 2-3 interfaces to occupy the typing needs to get model properties and methods working with proper typing.

Moving forward to add our queries and mutations, we need to create resolvers for the model above, assuming we have a service that deals with models. Here’s what our resolver looks like:

CODE: https://gist.github.com/velotiotech/0b0c2fe9bbe7cba3c65b46204feccefc.js

Not bad, we got our model and service and the resolver also looks good. But wait, we need to add GraphQL types as well. Here we are intentionally not including inputs to keep it short. Let’s do that:

CODE: https://gist.github.com/velotiotech/0524998dc516ba2c885e8f0cfd72dd01.js

Now, we have to club the schemas and resolvers together then pass them onto the GraphQL Express server—Apollo Server in this case:

CODE: https://gist.github.com/velotiotech/0cfa098b7a7ab8898da707d1fd29d1f7.js

With this setup, we got four files per model: model, resolver, service, and GraphQL schema file.

That’s too many things to keep in sync in real life. Imagine you need to add a new property to the above model after reaching production. You’ll end up doing at least following:

  1. Add a migration to sync the DB
  2. Update the interfaces
  3. Update the model schema
  4. Update the GraphQL schema

Possible Solution

As we know, after this setup, we’re mostly dealing with the entity models and struggling to keep its types and relations in sync.

If the model itself can handle it somehow, we can definitely save some effort, which  means we can sort things out if these entity model classes can represent both the database schema and its types.

Adding TypeGoose

Mongoose schema declarations with TypeScript can get tricky—or there might be a better way. Let’s add TypeGoose, so you no longer have to maintain interfaces (arguably). Here’s what the same user model looks like:

CODE: https://gist.github.com/velotiotech/bbcdb0db67a6d39ef5cd8cab106eb72b.js

Alright, no need for adding interfaces for the model and documents. You could have an interface for model implementation, but it’s not necessary.

With Reflect, which is used internally by TypeGoose, we managed to skip the need for additional interfaces.

If we want to add custom validations and messages, TypeGoose allows us to do that too. The prop decorator offers almost all the things you can expect from a mongoose model schema definition.

CODE: https://gist.github.com/velotiotech/0ca0e99b8c74467f8b031966bb4e6d09.js

Adding TypeGraphQL

Alright, TypeGoose has helped us with handling mongoose schema smoothly. But, we still need to define types for GraphQL. Also, we need to update the model types whenever we change our models. 

Let’s add TypeGraphQL

CODE: https://gist.github.com/velotiotech/42323ae89141a0c0c9bbe96c8e7e0738.js

What we just did is use the same TypeScript user class to define the schema as well as its GraphQL type—pretty neat.

Because we have added TypeGraphQL, our resolvers no longer need extra interfaces. We can add input classes for parameter types. Consider common input types such as CreateInput, UpdateInput, and FilterInput.

CODE: https://gist.github.com/velotiotech/235e4a1a65a1728b5c441e73655e1841.js

You can learn more about the syntax and input definition in the official docs.

That’s it. We are ready with our setup, and we can now simply build a schema and pass it to the server entry point just like that. No need to import schema files and merge resolvers. Simply pass array of resolvers to buildSchema:

CODE: https://gist.github.com/velotiotech/7ad5045aa37cac15bf800daa0d5daa94.js

Once implemented, this is how our custom demo project architecture might look:

Application Architecture
Fig:- Application Architecture

Limitations and Alternatives

Though these packages save some work for us, one may decide not to go for them since they use experimental features such as experimental decorators. However, the acceptance of these experimental features is growing.

TypeGoose:

Though TypeGoose offers a great extension to Mongoose, they've recently introduced some breaking changes. Upgrading from recent versions might be a risk. One alternative to TypeGoose for decorator-based schema definitions is TypeORM. Though, it currently has basic experimental support for MongoDB. 

TypeGraphQL:

TypeGraphQL is a well-maintained library. There are other options, like Nest.js and graphql-schema-decorators, which supports decorators for GraphQL schema. 

However, as Nest.js’s GraphQL support is more framework-oriented, it might be more than needed. The other one is not supported any longer. You can even integrate TypeGraphQL with Nest.js with some caveats.

Conclusion

Unsurprisingly, both of these libraries use experimental decorators API with Reflect Metadata. Reflect Metadata adds additional metadata support to the class and its members. The concept might look innovative but it’s nothing new. Languages like C# and Java support attributes or annotations that add metadata to types. With these added, it becomes handy to create and maintain well-typed applications.

One thing to note here would be—though the article introduces the benefits of using TypeGraphQL and TypeGoose together—it does not mean you can’t use them separately. Depending upon your requirements, you may use either of the tools or a combination of them.

This article covers a very basic setup for introduction of the mentioned technologies. You might want to learn more about advanced real-life needs with these tools and techniques from some of the articles mentioned below.

Further Reading


You can find the referenced code at this repo.

Did you like the blog? If yes, we're sure you'll also like to work with the people who write them - our best-in-class engineering team.

We're looking for talented developers who are passionate about new emerging technologies. If that's you, get in touch with us.

Explore current openings