【问题标题】:How to fix 'main GraphQL source' error at code generation using Apollo?如何在使用 Apollo 生成代码时修复“主要 GraphQL 源”错误?
【发布时间】:2019-09-29 19:45:37
【问题描述】:

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

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

我在 medium 上制作了很多教程,使用了 github 指令,但没有一个对我没有帮助。为了生成 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

【问题讨论】:

  • 在您的main 文件夹中,将您的graphql 文件夹移动到另一个包中。最好是与您的 java 文件所在的包匹配的包。类似com.example.app.graphql
  • 在文件夹 main 我有:java\com\example\graphqlapollo\MainActivitygraphql\com\example\graphqlapollo 。这可能是正确的。
  • 好的,我按照你说的做了,生成的文件夹多了,但是class还是没有生成。我还收到一个新警告:Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api' - 我没有在我的 gradle 中使用 compile,我不知道该警告来自哪里。
  • 让我们关注graphql\com\example\graphqlapollo 内的main。这是您需要放置.graphql 文件的地方,您的schema.json 也应该在这里。您收到的新警告可能是由于您使用 compile 而不是 api/implementation 的依赖项之一。我正在尝试构建一个尝试复制您的问题的项目。
  • 尝试遵循官方的 Apollo android 指南。似乎您依赖于插件的过去版本。在此处查找文档:github.com/apollographql/apollo-android

标签: android graphql apollo


【解决方案1】:

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

如果仍然无法正常工作,请将 Gradle 版本更改为旧版本。

希望它会起作用。对我来说也一样。

【讨论】:

    【解决方案2】:

    我尝试了 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 文件中。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-25
      • 2019-08-08
      • 2020-01-22
      • 1970-01-01
      • 2019-07-26
      • 2020-05-03
      • 2018-01-28
      • 2022-07-19
      相关资源
      最近更新 更多