GraphQl获取直接要求的模型
我通过翻新实现了GraphQL样本。我有这样的响应:
if (response.isSuccessful) {
Log.e("response", response.body().toString())
另外,这是我的接口类:
suspend fun postDynamicQuery(@Body body: String): Response<String>
现在我想通过直接模型来更改我的方法。这是服务器的答案。
{
"data": {
"getCityByName": {
"id": "112931",
"name": "Tehran",
"country": "IR",
"coord": {
"lon": 51.4215,
"lat": 35.6944
}
}
}
为了给出模型答案,我应该有这样的模型:
data class CityModel(
val data: Data
)
data class Data(
val getCityByName: GetCityByName
)
data class GetCityByName(
val id: String,
val name: String,
val country: String,
val coord: Coord
)
data class Coord(
val lon: Double,
val lat: Double
)
这两个更改:
if (response.isSuccessful) {
cityModel = response.body()}
以及
suspend fun postDynamicQuery(@Body body: String): Response<CityModel>
问题:我想要一个城市模型而不创建数据模型和CityModel模型。这是为每个API制作两个额外型号的样板。
我使用gsonConverterFactory转换为模型:
.addConverterFactory(calararsConverterFactory.Create()) .addConverterFactory(gsonconverterfactory.create())
I implemented a sample of Graphql by Retrofit. I have a response like this:
if (response.isSuccessful) {
Log.e("response", response.body().toString())
Also, this is my interface class:
suspend fun postDynamicQuery(@Body body: String): Response<String>
Now I want to change my method by giving a direct model. this is the servers' answer.
{
"data": {
"getCityByName": {
"id": "112931",
"name": "Tehran",
"country": "IR",
"coord": {
"lon": 51.4215,
"lat": 35.6944
}
}
}
To give a model answer, I should have a model like this:
data class CityModel(
val data: Data
)
data class Data(
val getCityByName: GetCityByName
)
data class GetCityByName(
val id: String,
val name: String,
val country: String,
val coord: Coord
)
data class Coord(
val lon: Double,
val lat: Double
)
And these two changes:
if (response.isSuccessful) {
cityModel = response.body()}
and
suspend fun postDynamicQuery(@Body body: String): Response<CityModel>
PROBLEM: I want a city model without creating a Data model and a CityModel model. this is a boilerplate to make two extra models for each API.
I used GsonConverterFactory for converting to model:
.addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我搜索了解决这个问题。唯一的方法是使用Appolo库。
我会提到一些好处。
要添加此库,您需要在build.gradle文件上添加这些依赖项。
I searched for solving this problem. The only way is using the Appolo library.
It has some benefits that I'll mention.
For adding this library you need to add these dependencies on your build.gradle file.