如何修复使用 Apollo 生成代码时的 'main GraphQL source' 错误?

How to fix 'main GraphQL source' error at code generation using Apollo?

我正在尝试使用 Apollo 代码生成将 Android 应用程序与 GraphQL 连接起来。我收到此类错误:

ERROR: Value 'main GraphQL source' specified for property '' cannot be converted to a file.

我在媒体上制作了很多教程,使用了 github 说明,但其中 none 对我没有帮助。为了生成 schema.json,我使用了 apollo 并且弃用了 apollo-codegen。

项目级 gradle 依赖项:

 dependencies {
     classpath 'com.android.tools.build:gradle:3.4.0'
     classpath 'com.apollographql.apollo:gradle-plugin:0.4.1'
 } 

在 app-gradle 我添加了一行:

apply plugin: 'com.apollographql.android'

我还在我的主文件夹中创建了一个文件夹'graphql',我把getPosts.graphql文件:

query allPostsQuery{
    findAllUsers{
        username
        email
        }
    }

然后我生成 schema.json 使用(我的查询的一部分):

    {
      "kind": "OBJECT",
      "name": "Query",
      "description": "",
      "fields": [
        {
          "name": "findAllUsers",
          "description": "",
          "args": [],
          "type": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "LIST",
              "name": null,
              "ofType": {
                "kind": "OBJECT",
                "name": "User",
                "ofType": null
              }
            }
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "findUser",
          "description": "",
          "args": [
            {
              "name": "username",
              "description": "",
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                }
              },
              "defaultValue": null
            }
          ],
          "type": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "OBJECT",
              "name": "User",
              "ofType": null
            }
          },
          "isDeprecated": false,
          "deprecationReason": null
        }
      ],
      "inputFields": null,
      "interfaces": [],
      "enumValues": null,
      "possibleTypes": null
    },

架构生成:

 apollo-codegen introspect-schema http://localhost:1100/graphql --output schema.json

在项目结构(文件 -> 项目结构 -> 项目)中将 Gradle 插件版本更改为 3.3.2,将 Gradle 版本更改为 4.10.1。

如果仍然无效,请将 Gradle 版本更改为旧版本。

希望它能奏效。对我也一样。

我尝试了 Sneha 的建议,对我来说效果很好。

如果您不想降级 Android Gradle pluginGradle 版本,您可以更改

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

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

在您的项目级别 build.gradle 文件中。

另外,请将 apollo-runtimeapollo android support library 更新到应用模块 build.gradle 文件中的最新版本。

implementation 'com.apollographql.apollo:apollo-runtime:1.0.0'
implementation "com.apollographql.apollo:apollo-android-support:1.0.0"

希望这能解决您的问题,如果您还有任何问题,请告诉我。