Open6

Apollo Server+PrismaでのOrderBy

ratmieratmie

クエリでソート順を受け取ってPrismaに渡したい

ratmieratmie

prismaに渡すのは

const usersWithPosts = await prisma.user.findMany({
  orderBy: [
    {
      role: 'desc',
    },
    {
      name: 'desc',
    },
  ],
  include: {
    posts: {
      orderBy: {
        title: 'desc',
      },
      select: {
        title: true,
      },
    },
  },
})

orderByにkeyにfield、valueにsortOrder型のオブジェクトを渡す

 export const SortOrder: {
    asc: 'asc',
    desc: 'desc'
  };

https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting#sorting
https://www.prisma.io/docs/concepts/components/prisma-client/pagination#cursor-based-pagination

簡単なのは当該オブジェクトをそのままクエリの方にすること

ratmieratmie

GitHubでの例

orderBy: IssueOrder

input IssueOrder {
  """
  The direction in which to order issues by the specified field.
  """
  direction: OrderDirection!

  """
  The field in which to order issues by.
  """
  field: IssueOrderField!
}

"""
Properties by which issue connections can be ordered.
"""
enum IssueOrderField {
  """
  Order issues by comment count
  """
  COMMENTS

  """
  Order issues by creation time
  """
  CREATED_AT

  """
  Order issues by update time
  """
  UPDATED_AT
}

"""
Possible directions in which to order a list of items when provided an `orderBy` argument.
"""
enum OrderDirection {
  """
  Specifies an ascending order for a given `orderBy` argument.
  """
  ASC

  """
  Specifies a descending order for a given `orderBy` argument.
  """
  DESC
}
ratmieratmie

呼び出し方は?複数のときは?