gpt4 book ai didi

kotlin - 如何使用 Kotlin 的 Apollo Android,但没有 Android?

转载 作者:行者123 更新时间:2023-12-02 13:12:06 25 4
gpt4 key购买 nike

我想使用 Apollo Android 查询 GitHub GraphQL api,将 Kotlin 与 Gradle Kotlin DSL 结合使用,但不在 Android 上。

错误信息

我确实遇到了一个特定的问题,但这可能是因为我在其他地方的设置有问题。

总而言之,我有一个 ViewLogin.graphql文件,其中一个 ViewLoginQuery已生成,但是当我尝试使用 ViewLoginQuery.builder()然后 unresolved reference: builder是问题所在。

设置

https://github.com/apollographql/apollo-android/issues/1573 中所述,应该可以在没有安卓的情况下使用Apollo Android。

环境

我正在使用 Arch Linux 和 IntelliJ,并安装了 JS GraphQL IntelliJ 插件。

Gradle Kotlin DSL 构建文件

在编写构建文件时我已经遇到了一个问题:我没有设法避免使用 deprecated buildscript堵塞。

https://plugins.gradle.org/plugin/com.apollographql.apollo 中显示的插件显然是 https://github.com/apollographql/apollo-android/issues/1573#issuecomment-575613238 中提到的孵化插件这还没有准备好。

以下构建文件似乎正确应用了插件:

buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
jcenter()
}
dependencies {
classpath("com.apollographql.apollo:apollo-gradle-plugin:1.2.2")
}
}

apply(plugin = "com.apollographql.android")

plugins {
val kotlinVersion = "1.3.60"

application
kotlin("jvm") version kotlinVersion
java
idea
}

dependencies {
implementation(kotlin("stdlib"))

// Apollo and dependencies
implementation("com.apollographql.apollo:apollo-runtime:1.2.2")

implementation("com.squareup.okio:okio:2.4.3")
implementation("org.jetbrains:annotations:13.0")
testImplementation("org.jetbrains:annotations:13.0")
}

repositories {
mavenCentral()
mavenLocal()
jcenter()
}

tasks.withType<com.apollographql.apollo.gradle.ApolloCodegenTask> {
generateKotlinModels.set(true)
}

所以在同步之后,我就有了可用的 apollo Gradle 任务。

下载架构

我安装了 Apollo cli interface然后,按照 Apollo docs ,我跑了
apollo schema:download --endpoint=https://api.github.com/graphql --header="Authorization: Bearer mytoken"

这给了我一个 schema.json我放入 src/main/graphql/nl/mypackage/myapplication/schema.json .

示例查询

我有一个文件 src/main/graphql/nl/mypackage/myapplication/ViewLogin.graphql其中包含 query ViewLogin { viewer { login }} .
IntelliJ 在 viewer 上均显示错误和 login ,说 Unknown field "viewer" on object type "Query". Did you mean "viewer"? .但这可能是 JS GraphQL 插件的错误配置。
我尝试通过添加文件 src/main/graphql/nl/mypackage/myapplication/.graphqlconfig 来配置 JS GraphQL包含
{
"name": "GitHub GraphQL Schema",
"schemaPath": "schema.json",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "https://api.github.com/graphql",
"headers": {
"user-agent": "JS GraphQL"
},
"introspect": true
}
}
}
}

但错误仍然存​​在。

然后我执行了 Gradle 任务 generateApolloClasses这产生了 build/generated/source/apollo/classes/main/nl/mypackage/myapplication/ViewLoginQuery.kt文件。
当我打开该文件时,没有显示任何错误,一切看起来都很好。

执行查询

在文件中 src/main/kotlin/nl/mypackage/myapplication/GraphqlExample.kt我尝试使用查询,遵循 https://www.apollographql.com/docs/android/essentials/get-started/#consuming-code 上的文档
但是当我尝试使用 ViewLoginQuery.builder()然后 builder不被识别(被 IntelliJ 识别,当我尝试运行它时)。

我尝试在 ViewLoginQuery 的父类(super class)中进行搜索,但找不到任何关于构建器的信息。

最佳答案

我不知道在哪里 builder应该是,但您可以直接实例化查询并使用它。

这是一个消费者代码的示例(最后测试版本为 2.1.0):

import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.exception.ApolloException
import okhttp3.OkHttpClient


fun main(args: Array<String>) {
val serverUrl = "https://api.github.com/graphql"

val authHeader = args[0]

// Add authentication header for GitHub
val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val builder = chain.request().newBuilder()
builder.header("Authorization", "Bearer $authHeader")
chain.proceed(builder.build())
}
.build()

val apolloClient = ApolloClient.builder()
.serverUrl(serverUrl)
.okHttpClient(okHttpClient)
.build()

val query = ViewLoginQuery()

apolloClient.query(query).enqueue(object : ApolloCall.Callback<ViewLoginQuery.Data?>() {
override fun onResponse(dataResponse: Response<ViewLoginQuery.Data?>) {
val data = dataResponse.data

if (data == null) {
println("No data received")
println(dataResponse.errors)
} else {
println(dataResponse.data?.viewer?.login)
}
}

override fun onFailure(e: ApolloException) {
println(e.message)
}
})
}

要修复 .graphql 文件中的“未知字段”错误,您必须将架构下载为 schema.graphql。而不是 schema.json ,见 https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/305为问题报告。
您还必须在 header 中提供 github token 。更改您的 .graphqlconfig到以下:
{
"name": "GitHub GraphQL Schema",
"schemaPath": "./schema.graphql",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "https://api.github.com/graphql",
"headers": {
"user-agent": "JS GraphQL",
"Authorization": "Bearer ${env:GITHUB_GRAPHQL_TOKEN}"
},
"introspect": true
}
}
}
}

您现在可以通过单击 "url": 旁边的“运行自省(introspection)查询”装订线图标轻松下载新模式。线。
您将收到一个弹出窗口,要求您输入 token 。不幸的是,该插件还不支持从任何地方读取环境变量,例如 https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/285

但是,Apollo Android 需要 schema.json ,所以你需要保留你下载的那个!
由于 Apollo Android 将(至少对我而言)在 schema.graphql 上出错文件,通过将 Gradle 任务配置更改为来告诉它忽略该文件
apollo {
generateKotlinModels.set(true)
graphqlSourceDirectorySet.srcDir("src/main/graphql")
graphqlSourceDirectorySet.include("**/*.graphql")
graphqlSourceDirectorySet.exclude("**/schema.graphql")
}

有关这些选项的文档,请参阅 https://www.apollographql.com/docs/android/essentials/plugin-configuration/

总结一下:对于您想要轻松使用 graphql 文件的 JS GraphQL 插件,您需要 schema.graphql.graphqlconfig 下载.对于 Apollo Android,您需要 schema.json ,您可以使用 Apollo cli 界面下载。

[编辑 2020 年 6 月] 对于 Apollo Android 2,您可以使用新的 Gradle 插件,因此完整的 build.gradle.kts 变为:

请记住在使用之前始终检查依赖项更新,例如使用 use-latest-versions Gradle 插件中的 help/useLatestVersions Gradle 任务。
plugins {

val kotlinVersion = "1.3.72"

application
kotlin("jvm") version kotlinVersion
java
idea

// Plugin which checks for dependency updates with help/dependencyUpdates task.
id("com.github.ben-manes.versions") version "0.28.0"

// Plugin which can update Gradle dependencies, use help/useLatestVersions
id("se.patrikerdes.use-latest-versions") version "0.2.14"

id("com.apollographql.apollo") version "2.1.0"
}

dependencies {
implementation(kotlin("stdlib"))

// Apollo and dependencies
implementation("com.apollographql.apollo:apollo-runtime:2.1.0")
implementation("com.squareup.okio:okio:2.4.3")
implementation("org.jetbrains:annotations:19.0.0")
testImplementation("org.jetbrains:annotations:19.0.0")
}

repositories {
mavenCentral()
mavenLocal()
jcenter()
}

apollo {
generateKotlinModels.set(true)
graphqlSourceDirectorySet.srcDir("src/main/graphql")
graphqlSourceDirectorySet.include("**/*.graphql")
graphqlSourceDirectorySet.exclude("**/schema.graphql")
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

关于kotlin - 如何使用 Kotlin 的 Apollo Android,但没有 Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com