【发布时间】: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\MainActivity和graphql\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