typegraphql-prisma-nestjs-example

June 27, 2025 ยท View on GitHub

Example use NestJS + Prisma2 + Typegraphql

Install

git clone https://github.com/EndyKaufman/typegraphql-prisma-nestjs-example.git
cd typegraphql-prisma-nestjs-example
npm ci

Run

npm run start:watch

Open browser

http://localhost:3000/graphql - graphql gui

https://typegraphql-prisma-nestjs-example.vercel.app/graphql - online graphql gui

https://github.com/EndyKaufman/typegraphql-prisma-nestjs-example/tree/master/src/dal - generated files for this example application

Example of queries

Mutation by Prisma2 + TypeGraphQL generator = Generated Crud

Create role

Query

mutation {
    createOneRole(data: { name: "User" }) {
        id
        name
    }
}

Result

{
  "data": {
    "createOneRole": {
      "id": 1,
      "name": "User"
    }
  }
}

Create user

Query

mutation {
    createOneUser(data: { username: "user", email: "user@user.com", password: "secret", Role: { connect: { name: "User" } } }) {
        email
        username
        password
        Role {
            id
            name
        }
    }
}

Result

{
  "data": {
    "createOneUser": {
      "email": "user@user.com",
      "username": "user",
      "password": "secret",
        "Role": {
          "id": 1,
          "name": "User"
        }
    }
  }
}

Query by Prisma2 + TypeGraphQL generator = Generated Crud

Query

query {
    users {
        id
        email
        password
        Role {
            id
            name
        }
    }
}

Result

{
  "data": {
    "users": [
      {
        "id": 1,
        "email": "user@user.com",
        "password": "secret",
        "Role": {
          "id": 1,
          "name": "User"
        }
      }
    ]
  }
}

Custom query by NestJS + TypeGraph

Query

query {
    recipes(skip: 0) {
        title
        ingredients
    }
}

Result

{
  "data": {
    "recipes": [
      {
        "title": "Recipe 1",
        "ingredients": [
          "apple",
          "orange"
        ]
      }
    ]
  }
}

Using "_count" for this https://github.com/EndyKaufman/typegraphql-prisma-nestjs/issues/49

Query

query {
  roles {
    id
    name
    _count{User,RolePermissions}
  }
}

Result

{
  "data": {
    "roles": [
      {
        "id": 1,
        "name": "User",
        "_count": {
          "User": 1,
          "RolePermissions": 0
        }
      }
    ]
  }
}

Mutation by Prisma2 + TypeGraphQL generator + use data from headers in data for insert to prisma = Generated Crud

Rule:

setTransformArgsIntoPrismaArgs((info: GraphQLResolveInfo, args: any, ctx: any) => {
    if (info.fieldName === UserCrudResolver.prototype.createOneUser.name && ctx.req.headers.email) {
        (args as CreateOneUserArgs).data.email = ctx.req.headers.email;
    }
    return args;
});

Query

mutation {
    createOneUser(data: { username: "user", email: "user@user.com", password: "secret" }) {
        email
        username
        password
    }
}

Http headers:

{"email":"newnew"}

Result

{
  "data": {
    "createUser": {
      "email": "newnew",
      "username": "user",
      "password": "secret"
    }
  }
}

https://github.com/nestjs/nest/tree/master/sample/23-type-graphql

https://github.com/prisma/prisma2/blob/master/docs/getting-started/quickstart-existing-project.md

https://github.com/MichalLytek/typegraphql-prisma

https://github.com/MichalLytek/type-graphql

https://github.com/EndyKaufman/typegraphql-prisma-nestjs