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:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 â‹… 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 â‹… 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 â‹… 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 â‹… 11 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