Android 使用 apollo graphql 客户端?

Using the apollo graphql client for Android?

我正在使用下面的示例,但在添加库时出现错误:

Using the apollo graphql client for Android

下面是我的 build.gradle (project):

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

    buildscript {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.1'
            classpath 'com.apollographql.android:gradle-plugin:0.1.0'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    allprojects {
        repositories {
            jcenter()
            mavenCentral()
        }
    }

    task clean(type: Delete) {
        delete rootProject.buildDir
    }

下面是我的 build.gradle (Module):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.admin.apollotest"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'

    compile 'com.apollographql.android:api:0.1.0' // the apollo runtime classes needed by auto-generated code
    compile 'com.squareup.retrofit2:retrofit:2.1.0' // retrofit2
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' // rxjava2 to use the Observables stuff
    compile 'com.apollographql.android:converter-pojo:0.1.0' // converts retrofit responses to pojos (ApolloConverterFactory)
}
apply plugin: 'com.apollographql.android'

apollo {
    // this tells the apollo compiler to generate actual static classes instead of just interfaces (more on that later)
    generateClasses = true
}

让我遇到错误:

错误是"Couldn't find a schema file. Please ensure a valid schema.json file exists in the sourceSet directory"。

使用 apollo-codegen download-schema 命令创建此 JSON。这将 GraphQL 端点的 URL 作为参数,后跟 --output 和应该存储 JSON 的路径。 JSON 应该进入项目的 src/main/graphql/,并在其中进入表示您希望代码进入的 Java 包的子目录(例如,--output src/main/graphql/com/commonsware/graphql/trips/api/schema.json)。

因此,总的来说,您可能有:

apollo-codegen download-schema https://graphql-demo.commonsware.com/0.1/graphql --output src/main/graphql/com/commonsware/graphql/trips/api/schema.json

看来您使用的是旧版本的 Apollo。这些警告已在最近的稳定版本中修复。

我建议你更换

classpath 'com.apollographql.android:gradle-plugin:0.1.0'

classpath 'com.apollographql.apollo:gradle-plugin:0.3.0'

并删除 generateClasses = true 块以及 converter-pojoapollo-api 依赖项。最新版本不再需要这些。

运行 之后的干净构建。

A​​pollo CLI 的实现 v2.17.2 (apollo-android v1.0.2)

特别是 GitHub API:

apollo client:download-schema /src/main/graphql/com/graphql/example/schema.json --endpoint https://api.github.com/graphql --header "Authorization: Bearer <github-token>"

Github. Creating a personal access token for the command line