PHP*_*ate 1 kotlin apollo-android gradle-kotlin-dsl
我想使用 Apollo Android 来查询 GitHub GraphQL api,使用 Kotlin 和 Gradle Kotlin DSL,但不在Android 上。
错误信息
我确实遇到了一个特定的问题,但这可能是因为我在其他地方的设置有问题。
总而言之,我有一个ViewLogin.graphql文件,从中ViewLoginQuery生成了 a,但是当我尝试使用ViewLoginQuery.builder()then时unresolved reference: builder出现了问题。
正如https://github.com/apollographql/apollo-android/issues/1573中提到的,应该可以在没有 Android 的情况下使用 Apollo Android。
我正在使用 Arch Linux 和 IntelliJ,并安装了 JS GraphQL IntelliJ 插件。
在编写构建文件时,我已经遇到了一个问题:我无法避免使用已弃用的 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)
}
Run Code Online (Sandbox Code Playgroud)
因此,同步后,我可以使用 apollo Gradle 任务。
我安装了Apollo cli 界面,然后按照Apollo 文档,我运行
apollo schema:download --endpoint=https://api.github.com/graphql --header="Authorization: Bearer mytoken"
Run Code Online (Sandbox Code Playgroud)
这给了我一个schema.json我放入的src/main/graphql/nl/mypackage/myapplication/schema.json.
我有一个文件src/main/graphql/nl/mypackage/myapplication/ViewLogin.graphql,其中包含query ViewLogin { viewer { login }}. viewerIntelliJ 在和 上都显示错误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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但错误仍然存在。
generateApolloClasses然后我执行了生成文件的Gradle 任务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()then时builder无法识别(通过IntelliJ,以及当我尝试运行它时)。
我尝试在 ViewLoginQuery 的超类中搜索,但找不到有关构建器的任何信息。
我不知道它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)
}
})
}
Run Code Online (Sandbox Code Playgroud)
要修复 .graphql 文件中的“未知字段”错误,您必须将架构下载为 aschema.graphql而不是schema.json,请参阅https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/issues/305了解该问题报告。您还必须在标头中提供 github 令牌。将您的更改.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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过单击该行旁边的“运行内省查询”装订线图标轻松下载新架构"url":。您将看到一个弹出窗口,要求提供令牌。不幸的是,该插件尚不支持从任何地方读取环境变量,例如参见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")
}
Run Code Online (Sandbox Code Playgroud)
有关这些选项的文档,请参阅https://www.apollographql.com/docs/android/essentials/plugin-configuration/
总结一下:对于 JS GraphQL 插件,您想要轻松使用 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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5465 次 |
| 最近记录: |