GraphQl获取直接要求的模型

发布于 2025-02-10 04:36:32 字数 1377 浏览 2 评论 0原文

我通过翻新实现了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-02-17 04:36:32

我搜索了解决这个问题。唯一的方法是使用Appolo库。
我会提到一些好处。

  1. 类型安全:所有模型均由架构自动创建。另外,您可以在应用程序中使用这些模型。
  2. 较少的编码:此库通过使用GraphQL示意图提供了一些代码。
  3. 无错误:您的查询和模型是由原理图自动创建的,因此,您没有任何错误。
  4. 为您提供了一些工具,以便于使用

要添加此库,您需要在build.gradle文件上添加这些依赖项。

    implementation("com.apollographql.apollo3:apollo-runtime:3.3.2")
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'

I searched for solving this problem. The only way is using the Appolo library.
It has some benefits that I'll mention.

  1. Type-safety: All models are created automatically by the schema. Also, you can use the models in your application.
  2. Less-code: this library provides some code via using the GraphQL schematic.
  3. Error-free: your queries and model are created automatically by the schematic, and as a result, you don't have any mistakes.
  4. Some tools provided for you IDE for easy using
    GraphQL tools

For adding this library you need to add these dependencies on your build.gradle file.

    implementation("com.apollographql.apollo3:apollo-runtime:3.3.2")
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文