Skip to content

Update adding-fields.md #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions docs/advanced/adding-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,44 @@ title: Adding fields to model type
sidebar_label: Adding model fields
---

If you want to add a field to the generated type like `User`, you have to create a proper `@FieldResolver` for that:

### Adding Custom Fields to a Generated Model

By default, `typegraphql-prisma` generates GraphQL types directly from your Prisma schema. However, if you need to add fields to your GraphQL schema that **do not exist in your Prisma database schema**, you can define them as **computed fields**. These fields are dynamically resolved at runtime rather than stored in the database.

**Because of this, they cannot be used in Prisma queries for filtering or sorting.** Instead, you can use a `@FieldResolver` to compute them on demand.

### Why Use `@FieldResolver`?

`@FieldResolver` allows you to define **computed fields**—fields that are derived from existing data rather than being stored in the database. These fields are useful for:

- Formatting or transforming existing fields
- Aggregating related data
- Resolving additional information dynamically

### Example: Adding a `favoritePost` Field to the `User` Model

Let’s say you want to add a `favoritePost` field to the `User` type, which fetches the user's first post. This field does not exist in the Prisma schema but can be derived dynamically.

Here’s how you can implement it:

```ts
import { Resolver, FieldResolver, Root, Ctx } from "type-graphql";
import { User, Post } from "@generated/type-graphql";
import { Context } from "../context";

@Resolver(of => User)
export class CustomUserResolver {
@FieldResolver(type => Post, { nullable: true })
async favoritePost(
@Root() user: User,
@Ctx() { prisma }: Context,
): Promise<Post | undefined> {
// Fetch the first post of the user
const [favoritePost] = await prisma.user
.findUniqueOrThrow({ where: { id: user.id } })
.posts({ first: 1 });
.posts({ take: 1 });

return favoritePost;
}
}
```