-
-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move circe codecs to the core module
- Loading branch information
Showing
8 changed files
with
173 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package caliban | ||
|
||
import caliban.interop.circe._ | ||
|
||
import scala.language.higherKinds | ||
|
||
/** | ||
* Represents a GraphQL request, containing a query, an operation name and a map of variables. | ||
*/ | ||
case class GraphQLRequest( | ||
query: String, | ||
operationName: Option[String], | ||
variables: Option[Map[String, InputValue]] | ||
) | ||
|
||
object GraphQLRequest { | ||
implicit def circeDecoder[F[_]: IsCirceDecoder]: F[GraphQLRequest] = | ||
GraphQLRequestCirce.graphQLRequestDecoder.asInstanceOf[F[GraphQLRequest]] | ||
} | ||
|
||
private object GraphQLRequestCirce { | ||
import io.circe._ | ||
import io.circe.derivation._ | ||
val graphQLRequestDecoder: Decoder[GraphQLRequest] = deriveDecoder[GraphQLRequest] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
package caliban | ||
|
||
import caliban.interop.circe._ | ||
|
||
import scala.language.higherKinds | ||
|
||
/** | ||
* Represents the result of a GraphQL query, containing a data object and a list of errors. | ||
*/ | ||
case class GraphQLResponse[+E](data: ResponseValue, errors: List[E]) | ||
|
||
object GraphQLResponse { | ||
implicit def circeEncoder[F[_]: IsCirceEncoder, E]: F[GraphQLResponse[E]] = | ||
GraphQLResponceCirce.graphQLResponseEncoder.asInstanceOf[F[GraphQLResponse[E]]] | ||
} | ||
|
||
private object GraphQLResponceCirce { | ||
import io.circe._ | ||
import io.circe.syntax._ | ||
val graphQLResponseEncoder: Encoder[GraphQLResponse[CalibanError]] = Encoder | ||
.instance[GraphQLResponse[CalibanError]]( | ||
response => | ||
Json.obj( | ||
"data" -> response.data.asJson, | ||
"errors" -> Json.fromValues(response.errors.map(err => Json.fromString(err.toString))) | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package caliban.interop.circe | ||
|
||
import io.circe._ | ||
|
||
import scala.language.higherKinds | ||
|
||
/** | ||
* This class is an implementation of the pattern described in https://blog.7mind.io/no-more-orphans.html | ||
* It makes it possible to mark circe dependency as optional and keep Encoders defined in the companion object. | ||
*/ | ||
private[caliban] trait IsCirceEncoder[F[_]] | ||
private[caliban] object IsCirceEncoder { | ||
implicit val isCirceEncoder: IsCirceEncoder[Encoder] = null | ||
} | ||
|
||
/** | ||
* This class is an implementation of the pattern described in https://blog.7mind.io/no-more-orphans.html | ||
* It makes it possible to mark circe dependency as optional and keep Decoders defined in the companion object. | ||
*/ | ||
private[caliban] trait IsCirceDecoder[F[_]] | ||
private[caliban] object IsCirceDecoder { | ||
implicit val isCirceDecoder: IsCirceDecoder[Decoder] = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package caliban | ||
|
||
import io.circe._ | ||
import zio.test.Assertion._ | ||
import zio.test._ | ||
|
||
object GraphQLRequestSpec | ||
extends DefaultRunnableSpec( | ||
suite("GraphQLRequestSpec")( | ||
test("can be parsed from JSON") { | ||
val request = Json | ||
.obj("query" -> Json.fromString("{}"), "operationName" -> Json.fromString("op"), "variables" -> Json.obj()) | ||
assert( | ||
request.as[GraphQLRequest], | ||
isRight( | ||
equalTo(GraphQLRequest(query = "{}", operationName = Some("op"), variables = Some(Map.empty))) | ||
) | ||
) | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package caliban | ||
|
||
import caliban.Value._ | ||
import io.circe._ | ||
import io.circe.syntax._ | ||
import zio.test.Assertion._ | ||
import zio.test._ | ||
|
||
object GraphQLResponseSpec | ||
extends DefaultRunnableSpec( | ||
suite("GraphQLResponseSpec")( | ||
test("can be converted to JSON") { | ||
val response = GraphQLResponse(StringValue("data"), Nil) | ||
assert( | ||
response.asJson, | ||
equalTo(Json.obj("data" -> Json.fromString("data"), "errors" -> Json.arr())) | ||
) | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters