Skip to content

Commit

Permalink
Propagate isHttpGetRequest via GraphQLRequest param instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Jul 9, 2024
1 parent df9145c commit 66584cf
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ final private class QuickRequestHandler[R](
private def executeRequest(method: Method, req: GraphQLRequest)(implicit
trace: Trace
): ZIO[R, Response, GraphQLResponse[Any]] =
interpreter.executeRequest(HttpRequestMethod.updateRequest(method == Method.GET)(req))
interpreter.executeRequest(if (method == Method.GET) req.asHttpGetRequest else req)

private def responseHeaders(headers: Headers, cacheDirective: Option[String]): Headers =
cacheDirective match {
Expand Down Expand Up @@ -202,7 +202,7 @@ final private class QuickRequestHandler[R](
encodeSingleResponse(resp, keepDataOnErrors = !isBadRequest, hasCacheDirective = cacheDirective.isDefined)
)
case resp =>
val isBadRequest = resp.errors.contains(HttpRequestMethod.MutationOverGetError)
val isBadRequest = resp.errors.contains(HttpUtils.MutationOverGetError)
Response(
status = if (isBadRequest) Status.BadRequest else Status.Ok,
headers = responseHeaders(ContentTypeJson, cacheDirective),
Expand Down
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ lazy val enableMimaSettingsJVM =
Def.settings(
mimaFailOnProblem := enforceMimaCompatibility,
mimaPreviousArtifacts := previousStableVersion.value.map(organization.value %% moduleName.value % _).toSet,
mimaBinaryIssueFilters := Seq()
mimaBinaryIssueFilters := Seq(
ProblemFilters.exclude[DirectMissingMethodProblem]("caliban.GraphQLRequest.*"),
ProblemFilters.exclude[Problem]("caliban.HttpRequestMethod*")
)
)

lazy val enableMimaSettingsJS =
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/caliban/GraphQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ trait GraphQL[-R] { self =>
if (
req.operationType == OperationType.Mutation &&
!cfg.allowMutationsOverGetRequests &&
HttpRequestMethod.isGetRequest(gqlReq)
) Exit.fail(HttpRequestMethod.MutationOverGetError)
gqlReq.isHttpGetRequest
) Exit.fail(HttpUtils.MutationOverGetError)
else Exit.succeed(req)
}
}
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/scala/caliban/GraphQLRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ case class GraphQLRequest(
query: Option[String] = None,
operationName: Option[String] = None,
variables: Option[Map[String, InputValue]] = None,
extensions: Option[Map[String, InputValue]] = None
extensions: Option[Map[String, InputValue]] = None,
isHttpGetRequest: Boolean = false
) { self =>

def withExtension(key: String, value: InputValue): GraphQLRequest =
Expand All @@ -24,9 +25,11 @@ case class GraphQLRequest(
def withFederatedTracing: GraphQLRequest =
withExtension(`apollo-federation-include-trace`, StringValue(ftv1))

private[caliban] def asHttpGetRequest: GraphQLRequest =
new GraphQLRequest(query, operationName, variables, extensions, isHttpGetRequest = true)

private[caliban] def isEmpty: Boolean =
operationName.isEmpty && query.isEmpty && extensions.isEmpty

}

object GraphQLRequest {
Expand Down
36 changes: 0 additions & 36 deletions core/src/main/scala/caliban/HttpRequestMethod.scala

This file was deleted.

4 changes: 4 additions & 0 deletions core/src/main/scala/caliban/HttpUtils.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package caliban

import caliban.CalibanError.ValidationError
import caliban.ResponseValue.{ ObjectValue, StreamValue }
import caliban.Value.NullValue
import caliban.wrappers.Caching
Expand All @@ -8,6 +9,9 @@ import zio.{ Cause, Chunk, Trace }

private[caliban] object HttpUtils {

val MutationOverGetError: ValidationError =
ValidationError("Mutations are not allowed for GET requests", "")

object DeferMultipart {
private val Newline = "\r\n"
private val ContentType = "Content-Type: application/json; charset=utf-8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object HttpInterpreter {
graphQLRequest: GraphQLRequest,
serverRequest: ServerRequest
)(implicit streamConstructor: StreamConstructor[BS]): ZIO[R, TapirResponse, CalibanResponse[BS]] = {
val req = HttpRequestMethod.updateRequest(serverRequest.method == Method.GET)(graphQLRequest)
val req = if (serverRequest.method == Method.GET) graphQLRequest.asHttpGetRequest else graphQLRequest
interpreter.executeRequest(req).map(buildHttpResponse[E, BS](serverRequest))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ object TapirAdapter {
encodeTextEventStreamResponse(resp)
)
case resp =>
val isBadRequest = response.errors.contains(HttpRequestMethod.MutationOverGetError: Any)
val isBadRequest = response.errors.contains(HttpUtils.MutationOverGetError: Any)
(
MediaType.ApplicationJson,
if (isBadRequest) StatusCode.BadRequest else StatusCode.Ok,
Expand Down

0 comments on commit 66584cf

Please sign in to comment.