prisma-nestjs-graphql icon indicating copy to clipboard operation
prisma-nestjs-graphql copied to clipboard

[Question] Custom extra field not in Prisma Schema

Open ming-nw opened this issue 3 years ago • 4 comments
trafficstars

First of all, thank you for creating this awesome plugin. It helps a lot. 👍

Here is my question

Prisma Schema

model Post {
  id Int @id @db.UnsignedInt @default(autoincrement())
  authorId Int @db.UnsignedInt @map("author_id")
  author User @relation(fields: [authorId], references: [id])
  topicId Int @db.UnsignedInt @map("topic_id")
  // and other fields
}

This will generate post.model.ts as following code. Question, is there any way for me to add an extra field that refers to other ObjectType that is not in my Prisma Schema? In this case, lets say a Topic @ObjectType.

The reason is my topicId is referencing another microservice. I will have a @ResolveField('Topic') to resolve the topic information and return accordingly.

@ObjectType()
export class Post {

    @Field(() => ID, {nullable:false})
    id!: number;

    @Field(() => Int, {nullable:false})
    authorId!: number;

    @Field(() => User, {nullable:false})
    author?: User;
    
    @Field(() => Int, {nullable:false})
    authorId!: number;

    // how do I achieve this without modify the generated file
    @Field(() => Topic)
    topic?: Topic;
}

Thank you 🙇

ming-nw avatar Jun 03 '22 10:06 ming-nw

The only right way to add unexisting field in prisma schema to graphql schema is @ResolveField But if you are building federated graphql, you can try @Directive https://github.com/unlight/prisma-nestjs-graphql#directive https://docs.nestjs.com/graphql/federation#code-first-1

But I did not have experience with federated graphql

unlight avatar Jun 03 '22 20:06 unlight

https://github.com/unlight/prisma-nestjs-graphql/issues/116

unlight avatar Jun 03 '22 20:06 unlight

Hey, thank you for the fast reply.

The example and the #116 provided helped to resolve my problem.

Thank you so much 👍

ming-nw avatar Jun 04 '22 08:06 ming-nw

I have a suggestion. In prisma I would went to do something like this to generate a computed field

...
model A {
   ...
   /// @Field(() => Int, {nullable:true})
   /// computedField1?: number;
}

Computed fields only needed to be generated on the generated model object type export class A. Something like this


@ObjectType()
export class A {
   ...
   @Field(() => Int, {nullable:true})
   computedField1?: number;

}

Current workaround

I hope this example will help anyone who went to use computed fields. This is a procedure I do to create a one. Feel free to make any suggestion.

First create a new class extending model A generated object type

...
/* Import your autogenerated object type A */
import { A } from "path/to/autogenerated/prisma-nestjs-graphql";

export class ExtendedA extends A {
   @Field(() => Int, {nullable:true})
   computedField1?: number;
}

In your nestjs resolver, change from object type A to ExtendedA.


import {ExtendedA} from './path/to/extended/a/type';

@Resolver(() => 'A')
export class AResolver {

  @Mutation(() => ExtendedA)
  createA () {}

  @Query (() => ExtendedA) 
  listA () {}

}

As you know, computed fields can be complex to compute. So make sure to compute them when needed. You can use @Info info to check if it is included as part of the query / mutation or not.

In service. How to declare and return the correct type is to do something like this


const _a = await this.prisma.a.findMany(
  where: aInput
);

const a : ExtendedA = {
  ..._a,
  computedField1: ...
};

return a;

AlkindiX avatar Jul 19 '22 06:07 AlkindiX