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:

Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 â‹… 13 min read
Solving Eventual Consistency In Frontend

Solving eventual consistency in frontend

Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.

Kayode Adeniyi
Nov 19, 2024 â‹… 6 min read
How To Use Lazy Initialization Pattern With Rust 1.80

How to use the lazy initialization pattern with Rust 1.80

Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.

Yashodhan Joshi
Nov 18, 2024 â‹… 5 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