无法从 Kotlin 中生成的 Apollo 类构建查询(.builder() 不存在)
Posted
技术标签:
【中文标题】无法从 Kotlin 中生成的 Apollo 类构建查询(.builder() 不存在)【英文标题】:Cannot build a query from generated Apollo class in Kotlin (.builder() is absent) 【发布时间】:2020-07-03 11:17:49 【问题描述】:我无法构建由 android Apollo 库生成的查询。
我有以下 .gpaphql 文件:
mutation LogIn($username: String!, $password: String!)
tokenAuth(username: $username, password: $password)
token
它为此生成了一个 Kotlin 请求文件:
// AUTO-GENERATED FILE. DO NOT MODIFY.
//
// This class was automatically generated by Apollo GraphQL plugin from the GraphQL queries it found.
// It should not be modified by hand.
//
import com.apollographql.apollo.api.InputFieldMarshaller
import com.apollographql.apollo.api.Mutation
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.OperationName
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.api.ResponseField
import com.apollographql.apollo.api.ResponseFieldMapper
import com.apollographql.apollo.api.ResponseFieldMarshaller
import com.apollographql.apollo.api.ResponseReader
import com.apollographql.apollo.api.internal.SimpleOperationResponseParser
import com.apollographql.apollo.internal.QueryDocumentMinifier
import com.apollographql.apollo.response.ScalarTypeAdapters
import com.apollographql.apollo.response.ScalarTypeAdapters.DEFAULT
import java.io.IOException
import kotlin.Any
import kotlin.Array
import kotlin.String
import kotlin.Suppress
import kotlin.collections.Map
import kotlin.jvm.Throws
import kotlin.jvm.Transient
import okio.BufferedSource
@Suppress("NAME_SHADOWING", "UNUSED_ANONYMOUS_PARAMETER", "LocalVariableName",
"RemoveExplicitTypeArguments", "NestedLambdaShadowedImplicitParameter")
data class LogInMutation(
val username: String,
val password: String
) : Mutation<LogInMutation.Data, LogInMutation.Data, Operation.Variables>
@Transient
private val variables: Operation.Variables = object : Operation.Variables()
override fun valueMap(): Map<String, Any?> = mutableMapOf<String, Any?>().apply
this["username"] = this@LogInMutation.username
this["password"] = this@LogInMutation.password
override fun marshaller(): InputFieldMarshaller = InputFieldMarshaller writer ->
writer.writeString("username", this@LogInMutation.username)
writer.writeString("password", this@LogInMutation.password)
override fun operationId(): String = OPERATION_ID
override fun queryDocument(): String = QUERY_DOCUMENT
override fun wrapData(data: Data?): Data? = data
override fun variables(): Operation.Variables = variables
override fun name(): OperationName = OPERATION_NAME
override fun responseFieldMapper(): ResponseFieldMapper<Data> = ResponseFieldMapper
Data(it)
@Throws(IOException::class)
override fun parse(source: BufferedSource, scalarTypeAdapters: ScalarTypeAdapters): Response<Data>
= SimpleOperationResponseParser.parse(source, this, scalarTypeAdapters)
@Throws(IOException::class)
override fun parse(source: BufferedSource): Response<Data> = parse(source, DEFAULT)
data class TokenAuth(
val __typename: String = "ObtainJSONWebToken",
val token: String?
)
fun marshaller(): ResponseFieldMarshaller = ResponseFieldMarshaller writer ->
writer.writeString(RESPONSE_FIELDS[0], this@TokenAuth.__typename)
writer.writeString(RESPONSE_FIELDS[1], this@TokenAuth.token)
companion object
private val RESPONSE_FIELDS: Array<ResponseField> = arrayOf(
ResponseField.forString("__typename", "__typename", null, false, null),
ResponseField.forString("token", "token", null, true, null)
)
operator fun invoke(reader: ResponseReader): TokenAuth = reader.run
val __typename = readString(RESPONSE_FIELDS[0])
val token = readString(RESPONSE_FIELDS[1])
TokenAuth(
__typename = __typename,
token = token
)
data class Data(
/**
* Obtain JSON Web Token mutation
*/
val tokenAuth: TokenAuth?
) : Operation.Data
override fun marshaller(): ResponseFieldMarshaller = ResponseFieldMarshaller writer ->
writer.writeObject(RESPONSE_FIELDS[0], this@Data.tokenAuth?.marshaller())
companion object
private val RESPONSE_FIELDS: Array<ResponseField> = arrayOf(
ResponseField.forObject("tokenAuth", "tokenAuth", mapOf<String, Any>(
"username" to mapOf<String, Any>(
"kind" to "Variable",
"variableName" to "username"),
"password" to mapOf<String, Any>(
"kind" to "Variable",
"variableName" to "password")), true, null)
)
operator fun invoke(reader: ResponseReader): Data = reader.run
val tokenAuth = readObject<TokenAuth>(RESPONSE_FIELDS[0]) reader ->
TokenAuth(reader)
Data(
tokenAuth = tokenAuth
)
companion object
const val OPERATION_ID: String =
"acc7a24c081a8c5ab67096b47de8c07e405411185c4016e47a75c0fdffe726e9"
val QUERY_DOCUMENT: String = QueryDocumentMinifier.minify(
"""
|mutation LogIn($'$'username: String!, $'$'password: String!)
| tokenAuth(username: $'$'username, password: $'$'password)
| __typename
| token
|
|
""".trimMargin()
)
val OPERATION_NAME: OperationName = OperationName "LogIn"
生成的 LogInMutation 类没有 .builder() 方法,它是一个数据类。如何从此请求创建 Query 类的实例,以便能够将其用作 ApolloClient.query() 方法中的参数?
【问题讨论】:
【参考方案1】:你可以像这样直接使用生成模型的构造函数:
ApolloClient.query(LogInMutation("name", "pass"))
【讨论】:
以上是关于无法从 Kotlin 中生成的 Apollo 类构建查询(.builder() 不存在)的主要内容,如果未能解决你的问题,请参考以下文章
Apollo Android 客户端 - 无法访问类路径上生成的类