prisma-nestjs-graphql
prisma-nestjs-graphql copied to clipboard
Omit array type
Consider the following schema:
model User {
/// @FieldType({ name: 'PaginatedPosts', from: 'custom-type', output: true })
posts: Post[]
}
model Post {...}
Which results in this:
export class User {
@Field(() => [PaginatedPosts], { nullable: true })
posts?: Array<Post>
}
Yet, in this case the desired output could be this:
export class User {
@Field(() => PaginatedPosts, { nullable: true })
events?: Array<Post>
}
Excuse me if I have missed it, yet I couldn't find a neat solution to avoid having the array type, which is in some not desired. The example above could be something as simple as implementing Relay-like pagination where the paginated result has a top-level object and only a nested edges is an array.
On the other, thank you for creating and maintaining the package, it spares not only tons of time, but also lots of complexity and possibly saves from errors!
No, sorry, there is no way to choose type between array and item.
And how PaginatedPosts is look like? Will it resolves with nestjs @Field decorator to list? (Typescript type is Array<Post>)
The PaginatedPosts looks something like this:
@ObjectType()
class PaginationEdge {
@Field(() => Post)
node: Post
@Field()
cursor: string
}
@ObjectType()
class PageInfo {
@Field()
lastCursor: string
@Field()
hasNextPage: boolean
}
@ObjectType()
class PaginatedPosts {
@Field(() => [PaginationEdge])
edges: PaginationEdge[]
@Field(() => PageInfo)
pageInfo: PageInfo
@Field()
totalCount: number
}
This is far from the full implementation, still, I hope it gets the point through.
I'm still thinking that something wrong with this and it will not work.
export class User {
@Field(() => PaginatedPosts, { nullable: true })
events?: Array<Post>
}
I can suggest to make generated User class abstract https://github.com/unlight/prisma-nestjs-graphql#objecttype then make custom class by inheriting from generated see https://docs.nestjs.com/graphql/resolvers#generics for details