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)?