When querying with GraphQL, I receive results in the following format:
{
"data": {
"events": [
{
"_id": "65f0653eb454c315ad62b416",
"name": "Event name",
"category": [
{
"_id": "66056f64c74b2fb603ba1f59",
"name": "Category 1"
}
]
}
]
}
}
However, I would like to simplify the structure by removing the array for category:
{
"data": {
"events": [
{
"_id": "65f0653eb454c315ad62b416",
"name": "Event name",
"category":
{
"_id": "66056f64c74b2fb603ba1f59",
"name": "Category 1"
}
}
]
}
}
Here is my current schema:
type Event {
_id: String!
name: String!
category: [Category]!
}
And the resolver function used:
export const Event = {
category: async (parent, args, context, info) => {
const dbCategories = await Category.find()
const category = dbCategories.filter((category) => {
return parent.category.includes(category.id)
})
return category
}
}
I have attempted to transform the array into an object but it was unsuccessful:
return { ...category }
If you have any suggestions or ideas, please help. Thank you.