-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use request extensions to identify GET requests
- Loading branch information
1 parent
f53133f
commit df9145c
Showing
6 changed files
with
44 additions
and
36 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
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 |
---|---|---|
@@ -1,25 +1,36 @@ | ||
package caliban | ||
|
||
import caliban.CalibanError.ValidationError | ||
import caliban.Value.StringValue | ||
import zio.stacktracer.TracingImplicits.disableAutoTrace | ||
import zio.{ FiberRef, Trace, Unsafe, ZIO } | ||
|
||
private[caliban] sealed trait HttpRequestMethod | ||
|
||
private[caliban] object HttpRequestMethod { | ||
case object GET extends HttpRequestMethod | ||
case object GET extends HttpRequestMethod { | ||
val asStringValue: StringValue = StringValue("GET") | ||
} | ||
|
||
case object POST extends HttpRequestMethod | ||
|
||
final val ExtensionKey = "requestHttpMethod" | ||
|
||
val MutationOverGetError: ValidationError = ValidationError("Mutations are not allowed for GET requests", "") | ||
|
||
private val fiberRef: FiberRef[HttpRequestMethod] = Unsafe.unsafe(implicit u => FiberRef.unsafe.make(POST)) | ||
def updateRequest(isGetRequest: Boolean)(req: GraphQLRequest): GraphQLRequest = | ||
if (isGetRequest) | ||
req.withExtension(HttpRequestMethod.ExtensionKey, HttpRequestMethod.GET.asStringValue) | ||
else req | ||
|
||
def isGetRequest(req: GraphQLRequest): Boolean = | ||
req.extensions.flatMap(_.get(ExtensionKey)).contains(GET.asStringValue) | ||
|
||
@deprecated("To be removed in the next major release") | ||
def getWith[R, E, A](zio: HttpRequestMethod => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] = | ||
fiberRef.getWith(zio) | ||
zio(HttpRequestMethod.POST) | ||
|
||
@deprecated("To be removed in the next major release") | ||
def setWith[R, E, B](method: HttpRequestMethod)(zio: ZIO[R, E, B])(implicit trace: Trace): ZIO[R, E, B] = | ||
method match { | ||
case POST => zio | ||
case GET => fiberRef.locally(method)(zio) | ||
} | ||
zio | ||
} |
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