dependencies {
implementation 'com.apollographql.apollo:apollo-runtime:2.4.1'
implementation 'com.apollographql.apollo:apollo-http-cache:2.4.1'
implementation 'com.apollographql.apollo:apollo-coroutines-support:2.4.1'
// For Java projects only
implementation 'com.apollographql.apollo:apollo-normalized-cache-sqlite:2.4.1'
}
确保网络连接正常并且服务器可访问。
检查 Apollo 配置是否正确,并确保生成的 apollo.config.js
文件位于项目的根目录中。
如果仍然无法下载模式或生成类,可以手动下载模式文件并将其放置在项目的 src/main/graphql
目录中。例如,如果 GraphQL API 的 URL 是 https://api.example.com/graphql
,则模式文件应该位于 src/main/graphql/com/example/schema.json
。
使用手动下载的模式文件创建一个 ApolloClient
实例。例如:
val apolloClient = ApolloClient.builder()
.serverUrl("https://api.example.com/graphql")
.normalizedCache(MyNormalizedCache(myContext))
.okHttpClient(okHttpClient)
.addCustomTypeAdapter(CustomType.DATETIME, DateTimeAdapter())
.build()
其中 MyNormalizedCache
是自定义的 NormalizedCache
实现,用于缓存查询结果,okHttpClient
是 OkHttpClient
实例,DateTimeAdapter
是自定义的 CustomTypeAdapter
实现,用于序列化和反序列化日期时间类型。