- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用gaphql-kotlin做场级(非根)解析器?我的代码正常工作(API1Response中的date_range为空,因为当查询中存在date_range时我不知道如何调用第二个解析器函数getAPI2Response())当每个查询分别调用时,因为我有两个带有关注查询名称的暂挂函数,但我想调用基于字段(非根)的两个查询的一个查询的挂起函数。例如,在下面显示的代码(服务)中,如果我的查询是带有字段“name”,“id”,“date_range”的“getAPI1Response”,则graphql应该调用getAPI1Response和getAPI2Response并将其映射到数据模型“API1Response”,因此用户可以使用单个查询来代替触发两个查询。
检查了graphql-kotlin样本中的几个样本,但所有样本都指向我未使用的springboot。
//DATA MODEL
data class API1Response(
val name: String?,
val id: Int?,
val date_range: API2Response?
)
data class API2Response(
val max_date_range: ZonedDateTime?,
val min_date_range: ZonedDateTime?
)
//SERVICE
class Query() {
private val httpClient = HttpClient()
// when query name is "getAPI1Response", this function get triggers
suspend fun getAPI1Response(): API1Response {
// call API 1 and map response to API1Response model.
return resp.content.toInputStream().use {
jackson.readValue(it, API1Response::class.java)
}
}
//// when query name is "getAPI2Response", this function get triggers
suspend fun getAPI2Response(): API2Response {
// call API 2 and map response to API2Response model.
return resp.content.toInputStream().use {
jackson.readValue(it, API2Response::class.java)
}
}
}
// GRAPHQL HANDLER
package com.my.package.graphql
import com.expedia.graphql.SchemaGeneratorConfig
import com.expedia.graphql.TopLevelObject
import com.expedia.graphql.hooks.SchemaGeneratorHooks
import com.expedia.graphql.toSchema
import com.my.package.errors.ErrorType
import com.my.package.http.*
import com.my.package.json.Jackson
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.language.StringValue
import graphql.schema.*
import graphql.schema.idl.SchemaPrinter
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.response.respondText
import io.ktor.routing.Route
import io.ktor.routing.get
import io.ktor.routing.post
import java.time.LocalDate
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import kotlin.reflect.KClass
import kotlin.reflect.KType
class GraphQLHandler(
mutations: List<Any>,
queries: List<Any>
) : Handler {
private val schema = toSchema(
config = SchemaGeneratorConfig(
supportedPackages = listOf("com.my.package"),
hooks = CustomSchemaGeneratorHooks(),
topLevelQueryName = "Query",
topLevelMutationName = "Mutation"
),
mutations = mutations.map { TopLevelObject(it) },
queries = queries.map { TopLevelObject(it) }
)
private val graphQL = GraphQL.newGraphQL(schema).build()
override val path = "/graphql"
override val routes: Route.() -> Unit = {
post {
postQuery(call)
}
}
private suspend fun postQuery(call: ApplicationCall) {
val reqBody = call.receiveJson(Map::class)
val query = reqBody["query"] as? String
if (query == null) {
call.respondError(
HttpStatusCode.BadRequest,
ErrorType.bad_request,
"missing or invalid query field in json"
)
return
}
@Suppress("UNCHECKED_CAST")
val variables = reqBody["variables"] as? Map<String, Any> ?: emptyMap()
handleQuery(call, query, variables)
}
// main function which calls my concern suspend funciton mapping query name to function name
private suspend fun handleQuery(call: ApplicationCall, query: String, variables: Map<String, Any>?) {
val executionInput = ExecutionInput(query, null, call.request.authContext, null, variables)
val result = graphQL.execute(executionInput).toSpecification()
val statusCode = if (result.containsKey("errors")) HttpStatusCode.InternalServerError else HttpStatusCode.OK
call.respondJson(statusCode, result, Jackson.all)
}
}
class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier as? KClass<*>) {
ZonedDateTime::class -> graphqlZonedDateTimeType
LocalDate::class -> graphqlLocalDateType
else -> null
}
}
val graphqlLocalDateType = GraphQLScalarType("LocalDate",
" ISO date format without an offset, such as '2011-12-03' ",
object : Coercing<LocalDate, String> {
//override few functions here
}
)
val graphqlZonedDateTimeType = GraphQLScalarType("ZonedDateTime",
" ISO date-time format with an offset, such as '2011-12-03T10:15:30+01:00' ",
object : Coercing<ZonedDateTime, String> {
//override few functions here
}
)
最佳答案
这已在相关的github问题中得到了解答:
https://github.com/ExpediaDotCom/graphql-kotlin/issues/230
Example using Spring: https://github.com/ExpediaDotCom/graphql-kotlin/blob/master/example/src/main/kotlin/com/expedia/graphql/sample/query/SubQueries.kt
Since graphql-kotlin reflectively generates resolvers, you'll have to do something like the above example, or use KGraphQL or graphql-java directly
Another example to get the data from further up the query chain is to use the DataFetcherEnvironment. It was added here: #173
关于kotlin - 如何在带有ktor框架的graphql-kotlin中进行字段级解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56397973/
我正在挖掘内置配置支持,并想使用它(而不是仅仅将我自己的配置与 Ktor 的配置一起使用),但我很难弄清楚如何以一种干净的方式来实现它。我有这个,它正在工作,但它真的很难看,我觉得必须有更好的方法:
我目前正在使用 Ktor Netty Engine 创建我的应用程序 当用户向我的服务器发送请求但没有发现任何功能时,我搜索了文档以处理发送电子邮件的任何功能。 post("/api/v1/auth"
KTor 与单元测试配合得很好,但我如何启动我的 ktor 服务器进行集成测试? 我的测试是这样的: @ExperimentalCoroutinesApi class HttpClientTest {
我想弄清楚如何使用 Ktor 检查 http 请求的 http 状态代码 我有一个像这样的简单 GET 请求,其中包含一个 HttpResponseObject 保存服务器返回的数据以及我控制的任何服
我刚刚开始探索 KMM,到目前为止看起来确实不错。 基本上,我想要在一个地方全局处理所有 http 错误代码(如 401、404、500)。 但是,我不确定如何与响应交互。 我在 HttpClient
我刚刚开始探索 KMM,到目前为止看起来确实不错。 基本上,我想要在一个地方全局处理所有 http 错误代码(如 401、404、500)。 但是,我不确定如何与响应交互。 我在 HttpClient
我正在尝试调用一个 twitter 端点,该端点为您提供持续的 json 结果流回客户端 https://documenter.getpostman.com/view/9956214/T1LMiT5U
我正在使用Ktor从Moshi转换为kotlinx序列化的过程,当我尝试提出获取数据的请求时,出现此错误 kotlinx.serialization.MissingFieldException: Fi
我正在将 Ktor 与 kotlin multiplatform 一起使用,我试图弄清楚为什么我没有收到任何抛出的异常。在我的客户端配置中,我使用了 HttpResonseValidator检查返回的
我需要根据正文为请求添加 header 。如何为客户端请求添加拦截器?我试过了 httpClient.requestPipeline.intercept(HttpRequestPipeline.Se
引用Ktor Pool implementation ,有人可以解释一下这个 pop 和 push 实现背后的概念。我试着单步执行代码,但在研究代码后我仍然不聪明。 下面是我难以理解的代码片段:
我是后端开发的新手。基本上,我想创建一个强大而简单的应用程序,它将在参数中接受一个 zip 文件 URL,然后从 URL 下载 zip 文件,最后解压缩 zip 并返回其中的 bin 文件。注意:zi
我想更好地了解Ktor如何处理静态内容的路由。我的静态文件夹(工作目录)中具有以下层次结构: - static - index.html - (some files) - static -
我是 Kotlin 和 Ktor 的新手,所以我尝试做简单的发布请求。正如你在下面看到的,没有什么特别的。 routing { post("/articles/add"){
KTOR中HttpClient的正确用法是什么?我应该在每个应用程序生命周期中像单例一样使用它,还是应该在每个请求中创建它? 最佳答案 我想说,如果您需要连接多个逻辑服务,则每个应用程序可能有多个客户
我做了什么? 在 build.gradle 下添加了依赖项 implementation "io.ktor:ktor:1.3.2" implementation "io.ktor:ktor-serve
我正在使用 ktor v0.9.2,我想根据用户是否通过身份验证为同一路由发送不同的内容。 我遇到的问题是我无法访问 authenticate { } 之外的主体。堵塞。 我的设置是这样的: data
我是 Kotlin 和 Ktor 的新手,想看看身份验证部分,所以我得到了以下代码。 路由“/”和“/bye”工作正常,但路由“登录”给定空白页! package blog import kotlin
当我使用 Retrofit 时,我可以轻松地创建日志记录拦截器以在 logcat 上查看带有正文和 header 的调用: val loggingInterceptor = HttpLoggingI
我一直在关注 ktor tutorial用于制作网站并注意到每次我对资源文件进行更改时,我都必须重新编译才能在浏览器中看到它的更新。有什么方法可以热重载静态文件以加快开发速度吗?如果重要的话,我会使用
我是一名优秀的程序员,十分优秀!