如何使用 Apollo 在代码生成时修复“主 GraphQL 源”错误?

yaw*_*yaw 7 android apollo graphql

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

ERROR: Value 'main GraphQL source' specified for property '$1' cannot be converted to a file.
Run Code Online (Sandbox Code Playgroud)

我在媒体上做了很多教程,使用了 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'
 } 
Run Code Online (Sandbox Code Playgroud)

在 app-gradle 上,我添加了一行:

apply plugin: 'com.apollographql.android'
Run Code Online (Sandbox Code Playgroud)

我还在主文件夹中创建了一个文件夹“graphql”,并将 getPosts.graphql 文件放入:

query allPostsQuery{
    findAllUsers{
        username
        email
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我使用(我的查询的一部分)生成了 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
    },
Run Code Online (Sandbox Code Playgroud)

模式生成:

 apollo-codegen introspect-schema http://localhost:1100/graphql --output schema.json
Run Code Online (Sandbox Code Playgroud)

Ary*_*yan 5

我尝试了 Sneha 的建议,这对我来说很好用。

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

classpath 'com.apollographql.apollo:gradle-plugin:0.4.1'
Run Code Online (Sandbox Code Playgroud)

classpath 'com.apollographql.apollo:apollo-gradle-plugin:1.0.0'
Run Code Online (Sandbox Code Playgroud)

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

还请在您的应用模块文件中更新apollo-runtime并更新apollo android support library到最新版本build.gradle

implementation 'com.apollographql.apollo:apollo-runtime:1.0.0'
implementation "com.apollographql.apollo:apollo-android-support:1.0.0"
Run Code Online (Sandbox Code Playgroud)

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