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

Omit array type

Open kdaniel21 opened this issue 3 years ago • 3 comments

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!

kdaniel21 avatar Jan 18 '22 20:01 kdaniel21

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>)

unlight avatar Jan 19 '22 08:01 unlight

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.

kdaniel21 avatar Jan 19 '22 16:01 kdaniel21

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

unlight avatar Jan 20 '22 07:01 unlight