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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 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