最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

apollo - Are inline fragments recommended for GraphQL for native mobile app consumption? - Stack Overflow

programmeradmin3浏览0评论

New to GraphQL. Working on designing integration for native mobile app (kotlin/swift).

Planning to use Apollo GraphQL

Architecture: native app(app) -> graphQL server (server) -> REST endpoint (downstream)

Using an example of getting vehicles I might own, lets say the REST endpoint returns a response like

{
  "vehicles": [
    {
      "vehicleId": "abcd123",
      "nickname": "crawler",
      "length": "2154",
      "vehicleType": "hatchback",
      "hatchback": {
        "hatchLength": 500,
        "hatchWidth": 200
      }
    },
    {
      "vehicleId": "abcd999",
      "nickname": "quiccker",
      "length": "3234",
      "vehicleType": "convertible",
      "convertible": {
        "convertSpeed": 435,
        "convertibleType": "luxury",
        "luxury": {
          "cups": true,
          "leather": true
        }
      }
    },
    {
      "vehicleId": "112345",
      "nickname": "nice one",
      "length": "3234",
      "vehicleType": "convertible",
      "convertible": {
        "convertSpeed": 435,
        "convertibleType": "sports",
        "sports": {
          "speed": 900,
          "transmission": "manual"
        }
      }
    }
  ]
}

I'm thinking the graphql query will be something like

query Vehicles {
    me {
        vehicles {
            vehicleId
            nickname
            length
            vehicleType
            ...on hatchback {
                hatchLength
                hatchWidth
                }
            ...on convertible {
                convertSpeed
                convertibleType
                ...on luxury {
                        cups
                        leather
                        }
                ...on sports {
                        speed
                        transmission
                        }
                }
        }
    }
}

to end up with graphql response like

{
  "data": {
    "me": {
      "vehicles": [
        {
          "vehicleId": "abcd123",
          "nickname": "crawler",
          "length": "2154",
          "vehicleType": "hatchback",
          "hatchLength": 500,
          "hatchWidth": 200
        },
        {
          "vehicleId": "abcd999",
          "nickname": "quiccker",
          "length": "3234",
          "vehicleType": "convertible",
          "convertSpeed": 435,
          "convertibleType": "luxury",
          "cups": true,
          "leather": true
        },
        {
          "vehicleId": "112345",
          "nickname": "nice one",
          "length": "3234",
          "vehicleType": "convertible",
          "convertSpeed": 435,
          "convertibleType": "sports",
          "speed": 900,
          "transmission": "manual"
        }
      ]
    }
  }
}

so essentially flattening the response structure from the REST response, since the nested data was all related to the same entity, just different fields applicable for different types.

Is this practice recommended? or is it better to keep the structure from the REST endpoint(keep type specific fields within the object)?

发布评论

评论列表(0)

  1. 暂无评论