Server side pagination in Graphql Scala

被刻印的时光 ゝ 提交于 2020-01-05 07:12:12

问题


I need to do server side pagination in graphql in scala. I have seven reports and have used one graphql query with different operations to get the data. Now I need to add server side pagination to it and I am not able to do that. Any help would be appreciated. Below are my code:

Schema:

val PanelNominationStatusReportDataType = ObjectType(
    "PanelNominationStatus",
    "Dashboard Reports",
    fields[Unit, PanelRewardSatusDetail](
      Field("awardName", OptionType(StringType), resolve = _.value.awardName),
      Field("nominatorEmail", OptionType(StringType), resolve = _.value.nominatorEmail),
      Field("nomineeEmail", OptionType(StringType), resolve = _.value.nomineeEmail),
      Field("nomineeLocation", OptionType(StringType), resolve = _.value.nomineeLocation),
      Field("approverEmail", OptionType(StringType), resolve = _.value.approverEmail),
      Field("citation", OptionType(StringType), resolve = _.value.citation),
      Field("businessJustification", OptionType(StringType), resolve = _.value.businessJustification),
      Field("rating", OptionType(IntType), resolve = _.value.rating),
      Field("votingStatus", OptionType(StringType), resolve = _.value.votingStatus),
      Field("nominatedDate", OptionType(StringType), resolve = _.value.nominatedDate),
      Field("approvedDate", OptionType(StringType), resolve = _.value.approvedDate)
    )
  )

Graphql Query:

 Field(
        "PanelNominationStatus",
        ListType(PanelNominationStatusReportDataType),
        description = Some("Returns the Employee Reward Report Data"),
        arguments = companyIdType :: rewardNamesType :: startDateType :: endDateType :: Nil,
        resolve = { c =>
          c.ctx.getPanelStatusReport(c.arg(companyIdType), c.arg(startDateType), c.arg(endDateType), c.arg(rewardNamesType))
        })

DataRepo:

  def getPanelStatusReport(companyId: Int, startDate: Long, endDate: Long, rewardNames: Seq[String]): List[PanelRewardSatusDetail] = {
    val redemptionReport = Await.result(reportsModel.findPanelRewardStatusDetailsReport(companyId, rewardNames, startDate, endDate), 20.seconds).toList
    redemptionReport
  }

And at last the model:

  def findPanelRewardStatusDetailsReport(companyId: Int, rewardNames: Seq[String], startDate: Long, endDate: Long): Future[Seq[PanelRewardSatusDetail]] = {
    val df = new SimpleDateFormat("dd-MM-yyyy")
    val start = stringToDateTime(df.format(startDate * 1000L), None).toDateTime()
    val end = stringToDateTime(df.format(endDate * 1000L), None).toDateTime()
    val rewardFilter = if(rewardNames.nonEmpty) "AND vrrc.reward_name IN (" + rewardNames.map(a => "'" + a + "'").mkString(",") + ")" else ""
    implicit val getOrdersResult = GetResult(r => PanelRewardSatusDetail(r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<))
    val q = sql"""select vrrc.reward_name, (select login from users where id=vrur.sender_id), (select login from users where id=vrur.receiver_ids), (select city_name from cities where id=(select city_id from users where id=vrur.receiver_ids)), (select login from users where id=approver_user_id), vrur.comment, vrur.business_justification, vrpa.rating, CASE WHEN vrpa.is_approved = 1 THEN 'VOTED' ELSE 'NOT VOTED' END, date(vrpa.created_at), date(vrpa.approved_at) from vr_panel_approval vrpa inner join vr_user_reward vrur on vrur.id=vrpa.user_reward_id inner join vr_reward_config vrrc on vrrc.id=vrur.reward_config_id where vrrc.company_id = $companyId and date(vrpa.created_at) between date($start) and date($end) #$rewardFilter"""
    db.run(q.as[PanelRewardSatusDetail])
  }

My request query:

{
    "query": "query PanelNominationStatus($startDate: Long!, $endDate: Long!, $companyId: Int!, $rewardNames: [String!]!) { PanelNominationStatus( startDate: $startDate, endDate: $endDate, companyId: $companyId, rewardNames: $rewardNames ) { awardName nominatorEmail nomineeEmail nomineeLocation approverEmail citation businessJustification rating votingStatus nominatedDate approvedDate }}",
    "operationName": "PanelNominationStatus",
    "variables": {
        "startDate": 1285891200,
        "endDate": 1576108800,
        "companyId": 355,
        "rewardNames": ["PANEL AWARD FEBRUARY", "PANEL AWARD MARCH"]
    }
}

I can make it possible using limit in the query and accepting it in the query but I need to send the total count of rows in the response as well. But I am not able to figure out where to make change in schema.


回答1:


You just need to define another GraphQL type for your result which also contains the count,

val PanelNominationStatusResultType = ObjectType(
  "PanelNominationStatusResult",
  "Dashboard Reports",
  fields[Unit, List[PanelRewardSatusDetail]](
    Field(
      "count",
      IntType,
      description = Some("count of nomination-status-reports in this response"),
      resolve = _.value.length
    ),
    Field(
      "panelNominationStatusList",
      ListType(PanelNominationStatusReportDataType),
      description = Some("list of nomination-status-reports"),
      resolve = _.value
    )
  )
)

Now, as for your query,

Field(
  "panelNominationStatusResult",
  PanelNominationStatusResultType,
  description = Some("Returns the details of ............"),
  arguments = companyIdType :: rewardNamesType :: startDateType :: endDateType :: Nil,
  resolve = { c =>
      c.ctx.getPanelStatusReport(c.arg(companyIdType), c.arg(startDateType), c.arg(endDateType), c.arg(rewardNamesType))
    }
)

If you want the totalCount, then first of all you need to change your getPanelStatusReport method to also return the totalCount

def findPanelRewardStatusDetailsReport(
  companyId: Int,
  rewardNames: Seq[String],
  startDate: Long,
  endDate: Long
): Future[(Int, Seq[PanelRewardSatusDetail])] = ???

// updated result type
val PanelNominationStatusResultType = ObjectType(
  "PanelNominationStatusResult",
  "Dashboard Reports",
  fields[Unit, (Int, List[PanelRewardSatusDetail])](
    Field(
      "totalCount",
      IntType,
      description = Some("total count of nomination-status-reports"),
      resolve = _.value._1
    ),
    Field(
      "count",
      IntType,
      description = Some("count of nomination-status-reports in this response"),
      resolve = _.value._2.length
    ),
    Field(
      "panelNominationStatusList",
      ListType(PanelNominationStatusReportDataType),
      description = Some("list of nomination-status-reports"),
      resolve = _.value._2
    )
  )
)


来源:https://stackoverflow.com/questions/59581433/server-side-pagination-in-graphql-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!