未生成 Apollo 目录

Apollo directory not generated

我在最初的实施中遇到了困难。 我的问题是以下构建无法生成 apollo 目录。

有了这个 gradle(应用级别)

plugins {
  id 'com.android.application'
  id 'kotlin-android'
  id 'kotlin-kapt'
  id 'com.apollographql.apollo'
}

android {
  compileSdk 31

  defaultConfig {
    applicationId "com.xxxx.xxxx"
    minSdk 21
    targetSdk 31
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    jvmTarget = '1.8'
  }

}

dependencies {

  implementation 'androidx.core:core-ktx:1.6.0'
  implementation 'androidx.appcompat:appcompat:1.3.1'
  implementation 'com.google.android.material:material:1.4.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
  testImplementation 'junit:junit:4.+'
  androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


  // The core runtime dependencies
  implementation"com.apollographql.apollo:apollo-runtime:2.5.9"
  // Coroutines extensions for easier asynchronicity handling
  implementation"com.apollographql.apollo:apollo-coroutines-support:2.5.9"

}

apollo {
  generateKotlinModels.set(true)
}

还有这个gradle

buildscript {
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    classpath "com.android.tools.build:gradle:7.0.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
    classpath "com.apollographql.apollo:apollo-gradle-plugin:2.5.9"

  }
}

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

使用这个schema.graphql

schema {
    query: Query
}

type Continent {
    code: ID!
    countries: [Country!]!
    name: String!
}

type Country {
    capital: String
    code: ID!
    continent: Continent!
    currency: String
    emoji: String!
    emojiU: String!
    languages: [Language!]!
    name: String!
    native: String!
    phone: String!
    states: [State!]!
}

type Language {
    code: ID!
    name: String
    native: String
    rtl: Boolean!
}

type Query {
    continent(code: ID!): Continent
    continents(filter: ContinentFilterInput): [Continent!]!
    countries(filter: CountryFilterInput): [Country!]!
    country(code: ID!): Country
    language(code: ID!): Language
    languages(filter: LanguageFilterInput): [Language!]!
}

type State {
    code: String
    country: Country!
    name: String!
}

enum CacheControlScope {
    PRIVATE
    PUBLIC
}

input ContinentFilterInput {
    code: StringQueryOperatorInput
}

input CountryFilterInput {
    code: StringQueryOperatorInput
    continent: StringQueryOperatorInput
    currency: StringQueryOperatorInput
}

input LanguageFilterInput {
    code: StringQueryOperatorInput
}

input StringQueryOperatorInput {
    eq: String
    glob: String
    in: [String]
    ne: String
    nin: [String]
    regex: String
}


"The `Upload` scalar type represents a file upload."
scalar Upload

由此配置生成

{
  "name": "Untitled GraphQL Schema",
  "schemaPath": "schema.graphql",
  "extensions": {
    "endpoints": {
      "Default GraphQL Endpoint": {
        "url": "https://countries.trevorblades.com/",
        "headers": {
          "user-agent": "JS GraphQL"
        },
        "introspect": false
      }
    }
  }
}

我无法生成 Apollo 文件夹

你为什么把你发布的代码作为 schema.graphql

您可以在 this GitHub repo 中找到以下示例。
(这是我使用 trevorblades countries API and Apollo GraphQL 2.5.9 制作的示例应用程序,所以我认为它非常适合您的需求)


首先您需要download the server's schema使用以下命令(将路径替换为您的应用名称和包)

mkdir -p app/src/main/graphql/com/example/rocketreserver/
./gradlew :app:downloadApolloSchema --endpoint='https://countries.trevorblades.com/' --schema='app/src/main/graphql/com/example/rocketreserver/schema.json'

这将在指定路径中生成 schema.json,之后您可以 write all the queries 创建文件,例如 countries.graphqlcontinents.graphql.
确保将它们放在与 schema.json.

相同的文件夹中

在我的示例中,您可以找到一个针对国家列表的查询和一个针对国家详细信息的查询。

CountryList.graphql

query CountriesList {
  countries {
    code
    name
    continent{
      code
      name
    }
    languages{
      code
      name
    }
    emoji
  }
}

CountryDetail.graphql

query CountryDetail($code:ID!) {
  country(code: $code) {
    code
    name
    phone
    continent{
      code
      name
    }
    capital
    currency
    languages{
      code
      name
    }
    emoji
  }
}

此时你只需要将项目构建到generateapollo目录和你定义的模型即可。