GitHub GraphQL API的Apollo客户端响应不是JSON格式

时间:2019-01-25 22:27:18

标签: android graphql apollo apollo-client

我正在使用apollo-client在Android上使用GraphQL进行实验,并且正在使用GitHub的GraphQL API。我正在点击一个API,以便给我一个用户拥有的回购清单。一切正常,但是我得到的响应不是JSON格式,而是String格式。

响应如下:

Data{user=User{__typename=User, 
repositories=Repositories{__typename=RepositoryConnection, nodes= 
[Node{__typename=Repository, name=testrepository}]}}

当我尝试通过Insomnia(GraphQL rest客户端)访问url时,以JSON格式获得响应,但在我的应用程序中,以上述格式获得响应。我尝试在标头中传递content-type:“ application / json; charset = utf-8”,但没有成功。

这是我获取回复的方式:

public void fetchRepoList(String userName, String authHeader, final ResponseCallback responseCallback) {
    GraphQL.getInstance().getApolloClient(authHeader)
            .query(githubRepoList.repoListAPI.FindQuery.builder().repoOwner(userName).build())
            .enqueue(new ApolloCall.Callback<githubRepoList.repoListAPI.FindQuery.Data>() {
                @Override
                public void onResponse(@Nonnull Response<githubRepoList.repoListAPI.FindQuery.Data> response) {
                  Log.d(TAG, "response" + response)
                }

                @Override
                public void onFailure(@Nonnull final ApolloException exception) {

                }
            });
}

我想将响应放入Model类的列表中,为此,我需要JSON格式的响应。搜索了此问题,但没有任何适当的解决方案。

我正在使用apollo客户端0.3.2

[编辑:1]

我尝试使用Okhttp调用GitHub GraphQL API,这次我得到了以下响应:

{"data":{"__schema":{"queryType":{"name":"Query"},"mutationType":{"name":"Mutation"},"subscriptionType":null,"types":[{"kind":"SCALAR","name":"Boolean","description":"Represents `true` or `false` values.","fields":null,"inputFields":null,"interfaces":null,"enumValues":null,"possibleTypes":null},{"kind":"SCALAR","name":"String","description":"Represents textual data as UTF-8 character sequences. This type is most often used by GraphQL to represent free-form human-readable text.","fields":null,"inputFields":null,"interfaces":null,"enumValues":null,"possibleTypes":null},{"kind":"OBJECT","name":"Query","description":"The query root of GitHub's GraphQL interface.","fields":[{"name":"codeOfConduct","description":"Look up a code of conduct by its key","args":[{"name":"key","description":"The code of conduct's key","type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"String","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"CodeOfConduct","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"codesOfConduct","description":"Look up a code of conduct by its key","args":[],"type":{"kind":"LIST","name":null,"ofType":{"kind":"OBJECT","name":"CodeOfConduct","ofType":null}},"isDeprecated":false,"deprecationReason":null},{"name":"license","description":"Look up an open source license by its key","args":[{"name":"key","description":"The license's downcased SPDX ID","type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"String","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"License","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"licenses","description":"Return a list of known open source licenses","args":[],"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"LIST","name":null,"ofType":{"kind":"OBJECT","name":"License","ofType":null}}},"isDeprecated":false,"deprecationReason":null},{"name":"marketplaceCategories","description":"Get alphabetically sorted list of Marketplace categories","args":[{"name":"includeCategories","description":"Return only the specified categories.","type":{"kind":"LIST","name":null,"ofType":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"String","ofType":null}}},"defaultValue":null},{"name":"excludeEmpty","description":"Exclude categories with no listings.","type":{"kind":"SCALAR","name":"Boolean","ofType":null},"defaultValue":null},{"name":"excludeSubcategories","description":"Returns top level categories only, excluding any subcategories.","type":{"kind":"SCALAR","name":"Boolean","ofType":null},"defaultValue":null}],"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"LIST","name":null,"ofType":{"kind":"NON_NULL","name":null,"ofType":{"kind":"OBJECT","name":"MarketplaceCategory","ofType":null}}}},"isDeprecated":false,"deprecationReason":null},{"name":"marketplaceCategory","description":"Look up a Marketplace category by its slug.","args":[{"name":"slug","description":"The URL slug of the category.","type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"String","ofType":null}},"defaultValue":null},{"name":"useTopicAliases","description":"Also check topic aliases for the category slug","type":{"kind":"SCALAR","name":"Boolean","ofType":null},"defaultValue":null}],"type":{"kind":"OBJECT","name":"MarketplaceCategory","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"marketplaceListing","description":"Look up a single Marketplace listing","args":[{"name":"slug","description":"Select the listing that matches this slug. It's the short name of the listing used in its URL.","type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"String","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"MarketplaceListing","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"marketplaceListings","description":"Look up Marketplace listings","args":[{"name":"after","description":"Returns the elements in the list that come after the specified cursor.","type"

此响应甚至没有关于存储库的必需数据。它甚至与存储库列表都不相关。

因此,我返回到旧方法,并使用阿波罗进行了调用。现在,由于apollo通过标准GraphQL查询创建了这些模型类,因此如何创建该模型类的列表。

我在github上浏览了apollo sample-app,并遇到了这段代码:

List<FeedEntry> feedResponseToEntriesWithRepositories(Response<FeedQuery.Data> response) {
List<FeedEntry> feedEntriesWithRepos = new ArrayList<>();
final FeedQuery.Data responseData = response.data();
if (responseData == null) {
  return Collections.emptyList();
}
final List<FeedEntry> feedEntries = responseData.feedEntries();
if (feedEntries == null) {
  return Collections.emptyList();
}
for (FeedEntry entry : feedEntries) {
  if (entry.repository() != null) {
    feedEntriesWithRepos.add(entry);
  }
}
return feedEntriesWithRepos;
}

此处,feedEntries()方法返回供稿列表,该方法位于apollo目录中自动生成的模型类文件中。我去检查了我的模型文件,没有方法返回回购清单(以我的情况为例)。该文件太大,无法在此处发布,但是如果社区希望查看,可以在此处发布。

顺便说一句,我用我的代码尝试过类似的事情:

List<githubRepoList.repoListAPI.FindQuery.Node> repoList = new ArrayList<>();
final githubRepoList.repoListAPI.FindQuery.Data repoListData = response.data();
final List<githubRepoList.repoListAPI.FindQuery.Node> finalRepoList = repoListData.(which method to call from the auto-generated class file?)

在这里,Node是我在apollo目录中自动生成的模型文件中的一个类,该类应该具有一个返回回购模型类列表的方法。

我知道我在这里做错了。我认为还有其他方法可以创建这些模型类的列表。

1 个答案:

答案 0 :(得分:1)

  

响应不是JSON格式

响应,格式为JSON。现在它以githubRepoList.repoListAPI.FindQuery.Data对象的形式。该类是根据您的GraphQL文档为您生成的代码。引用the Apollo-Android documentation,并重点强调:

  

Apollo-Android是兼容GraphQL的客户端,可从标准GraphQL查询生成Java模型

尤其是,Apollo-Android在这些Java模型类上生成toString()实现。就您而言:

  • DataUser字段中保存一个user
  • UserRepositoriesConnection字段中拥有一个Repositories,重命名为repositories
  • repositories集合包含一个Repository

使用Apollo-Android的原因是您不必自己处理JSON。相反,如果您想自己解析JSON,请摆脱Apollo-Android,然后使用OkHttp进行Web服务调用。