【问题标题】:Apollo directory not generated未生成 Apollo 目录
【发布时间】:2021-11-06 18:36:01
【问题描述】:

我在初始实施时遇到了困难。 我的问题是以下构建无法生成 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)
}

还有这个毕业典礼

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 文件夹

【问题讨论】:

    标签: android gradle graphql apollo apollo-client


    【解决方案1】:

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

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


    首先您需要使用以下命令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
      }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多