TypeORM, TypeGraphQL, and Apollo together are a powerful stack for Node.js for creating GraphQL APIs.
Unfortunately there are practically no decent and up-to-date example code repos or boilerplates, so I created my own code sample / boilerplate here:
Boilerplate **GitHub Link: https://github.com/JeremyBernier/typeorm-typegraphql-apollo-boilerplate**
(Note: The code repo is in progress. When it is done I’ll completely rewrite this article)
In this article I will introduce the libraries, explain the essentials to get things working, and address bugs and problems.
TypeORM is an ORM for Node.js
TypeGraphQL is a library that enables you to define your schema with Typescript types and decorators.
Apollo server is a GraphQL server that can work as a middleware to any other Node.js server (eg. Express)
GraphQL allows the client/frontend to query the specific data requested like this:
{
places {
name
address
rating
reviews {
rating
content
user {
name
}
}
}
}
Here we’re querying places
, along with each place’s reviews
, and the user
name of each reviewer.
{
user(id: "some-user-id") {
name
description
reviews {
rating
content
place {
name
}
}
}
}
Here we’re querying a specific user’s profile information, as well as a list of his reviews and the places they’ve reviewed.
One the backend, the first query involves querying places
, while the second queries user
@Query((returns) => User)
async user(@Arg("id") id: string) {
return this.userRepo.findOne({
where: {
id,
},
});
}
But notice that this simple query won’t return any reviews because reviews are stored in a separate database table and need to be joined on users.