Here is an example of a returned data object:
{
data: {
posts: {
edges: [
{
post: {
id: "1",
title: "Foo"
}
},
{
post: {
id: "2",
title: "Bar"
}
}
]
}
}
}
The query used to obtain this data is as follows:
query MyQuery {
posts {
edges {
post: node {
id
title
}
}
}
}
While this structure works, it requires creating nested interfaces. Is there a way to simplify the results or transform them using JavaScript map()
instead?
Ideally, I would prefer the response to be formatted like this:
{
data: {
posts: [
{
id: "1",
title: "Foo"
},
{
id: "2",
title: "Bar"
}
]
}
}
Note: Any solution provided should be implemented on the client side since updating the server-side GraphQL schema is not an option.
Thank you!
EDIT
Below is my Angular/TypeScript code that handles the GraphQL requests and responses...
post.service.ts
import { Injectable } from '@angular/core';
...
model/post.ts
interface CategoryNode {
...
}
export interface Post {
...
}
As shown above, too many nested interfaces are required for the current implementation.
Actual sample response (used for unit testing)
{
data: {
posts: {
edges : [
{
post: {
id: "cG9zdDoxMjc=",
title: "Lorem Ipsum",
date: "2022-01-06T22:00:53",
uri: "\/2022\/01\/06\/lorem-ipsum\/",
categories: {
edges: [
{
category: {
id: "dGVybToy",
name: "General",
uri: "\/category\/general\/"
}
}
]
}
},
cursor: "YXJyYXljb25uZWN0aW9uOjEyNw=="
},
{
post: {
id: "cG9zdDoxMjc=",
title: "Lorem Ipsum",
date: "2022-01-06T22:00:53",
uri: "\/2022\/01\/06\/lorem-ipsum\/",
categories: {
edges: [
{
category: {
id: "dGVybToy",
name: "General",
uri: "\/category\/general\/"
}
},
{
category: {
id: "dGVybToy",
name: "General",
uri: "\/category\/general\/"
}
}
]
}
},
cursor: "YXJyYXljb25uZWN0aW9uOjEyNw=="
},
],
pageInfo: {
startCursor: "YXJyYXljb25uZWN0aW9uOjEyNw==",
hasPreviousPage: false,
hasNextPage: false,
endCursor: "YXJyYXljb25uZWN0aW9uOjEyNw=="
}
}
}
};