2021-12-17
2328
#graphql#typescript
Rahman Fadhil
12661
Dec 17, 2021 â‹… 8 min read

Building GraphQL APIs with TypeGraphQL and TypeORM

Rahman Fadhil Developer and content writer.

Recent posts:

Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 â‹… 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 â‹… 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 â‹… 10 min read
Handling Dates In JavaScript With Tempo

Handling dates in JavaScript with Tempo

Use the Tempo library to format dates and times in JavaScript while accounting for time zones, daylight saying time, and date internationalization.

Amazing Enyichi Agu
Apr 30, 2024 â‹… 8 min read
View all posts

16 Replies to "Building GraphQL APIs with TypeGraphQL and TypeORM"

  1. Any chance you could cover (or update this) to also show how to use typeorm and type-graphql to create an interface (i.e. interface User types teacher / student)?

  2. Good article. Looks extremely promising as a way to use GraphQL with TypeORM. How does this work with migrations? And, have you created any type of system using this approach with a large number of tables with relationships between them? I’m wondering how this will perform when there are 400+ tables.

  3. Thanks!

    If you’re dealing with a large number of tables, I suggest you take a look at DataLoader. You can use it alongside with TypeGraphQL and TypeORM. It allows you to gather every table relation query that happens in a single request and run them at the same time.

  4. I cannot for the life of me get this to build in Typescript. Dev works fine, but trying to run a built output always comes back with `import { Entity, BaseEntity, PrimaryGeneratedColumn, Column } from “typeorm”;`

  5. Thank you for this article.
    I like the simplicity of typeorm + type-graphql for basic CRUD operation.

    But it’s difficult to find real life examples with relationships (one to many, many to one, many to many)

    Ben Awad released a video, with data loader, but honestly it’s seems to be a pain in the a**

    1. Hi Jerome, you find it difficult to find examples of one to many, many to one and many to may – well here’s a few for you:

      Customer *—————–* Product (Customer buys many Products – Product Type really – and a Product Type can be bought by many customers).

      Developer *———————–*Programming Languages
      Developer may use many PLs, a PL may be used by many individual Developers.

      One to Many
      Mother 1 ———————— * Child
      A mother may have many children (0..n), a Child will have exactly one Mother.

      And so on.

      Perhaps it’s the meaning of Many to Many, One to Many that is confusing?

  6. Very nice article. I like the conventions of a single model/entity with the decorators for both TypeORM and GraphQL. The more conventional and repeatable the mechanism – the more it becomes a candidate for code generation and/or scaffolding via a CLI (i.e., Angular Schematics).

    I’m just getting started with this technology stack – however, the maturity of tools and process make it a strong candidate for enterprise applications. Ping me on twitter @Angularlicious, I’d love to continue the conversation…on my podcast, perhaps? Let me know. Thanks!

  7. Nice hint to combine Entities and type-graphql fields and type-orm entities. However I can see potential issues that could enforce a separation between the two type of objects. What if you have different database schema than what you want GQL queries structure? What if the database fields change but you don’t want to break the GQL interface?

  8. Hi , very clear article , help me a lot . i have a one question though , we are extending books by BaseEntity and when creatingBook if we try to declare return type as
    createCategory(
    @Arg(‘category’) category: AddCategoryInput
    ):Promise { ..

    then it return error “missing properties hasId , save , remove etc” which are BaseEntity properties . any suggestions to handle. thanks in advance.

  9. @John … to create Types, you’ll use the ObjectType decorator .. like this

    @ObjectType({ description: “The recipe model” })
    class Recipe {
    @Field(type => ID)
    id: string;

    @Field({ description: “The title of the recipe” })
    title: string;

    @Field(type => [Rate])
    ratings: Rate[];

    @Field({ nullable: true })
    averageRating?: number;
    }

Leave a Reply