All Downloads are FREE. Search and download functionalities are using the official Maven repository.

caliban.client.GraphQLResponse.scala Maven / Gradle / Ivy

There is a newer version: 1.3.3.4
Show newest version
package caliban.client

import caliban.client.__Value.__ObjectValue
import io.circe.{ Decoder, HCursor, Json }

/**
 * Represents the result of a GraphQL query, containing a data object and a list of errors.
 */
case class GraphQLResponse(
  data: Option[__Value],
  errors: List[GraphQLResponseError] = Nil,
  extensions: Option[Json] = None
)

object GraphQLResponse {

  implicit val objectValueDecoder: Decoder[__ObjectValue] = Decoder[__Value].emap {
    case o @ __ObjectValue(_) => Right(o)
    case _                    => Left("Invalid value, should be an object.")
  }

  implicit val decoder: Decoder[GraphQLResponse] = (c: HCursor) =>
    for {
      data       <- c.downField("data").as[Option[__Value]]
      errors     <- c.downField("errors").as[Option[List[GraphQLResponseError]]]
      extensions <- c.downField("extensions").as[Option[Json]]
    } yield GraphQLResponse(data, errors.getOrElse(Nil), extensions)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy