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:

Exploring Hurl An Alternative To Postman

Exploring Hurl, a command line alternative to Postman

Hurl is an excellent Postman alternative that improves the DX of working with APIs through the command line.

Nwani Victory
May 20, 2024 â‹… 8 min read
How To Integrate WunderGraph With Your Frontend Application

How to integrate WunderGraph with your frontend application

Unify and simplify APIs using WunderGraph to integrate REST, GraphQL, and databases in a single endpoint.

Boemo Mmopelwa
May 17, 2024 â‹… 5 min read
Understanding The Latest Webkit Features In Safari 17.4

Understanding the latest Webkit features in Safari 17.4

The Safari 17.4 update brought in many modern features and bug fixes. Explore the major development-specific updates you should be aware of.

Rahul Chhodde
May 16, 2024 â‹… 10 min read
Using Webrtc To Implement Peer To Peer Video Streaming In A Node Js Project

Using WebRTC to implement P2P video streaming

Explore one of WebRTC’s major use cases in this step-by-step tutorial: live peer-to-peer audio and video streaming between systems.

Oduah Chigozie
May 16, 2024 â‹… 18 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