As title, I would like to re-organize my data which I query from Mysql server for my GraphQL client.
resolve: (root, { name }) => {
const weekday = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
return findCafeshopByName(name).reduce((record, row, i) => {
record.openingHours = record.openingHours || { [weekday[0]]: record.openingHour };
record.openingHours = Object.assign(
record.openingHours,
{ [weekday[i]]: row.openingHour },
)
return record;
});
}
As you can see, I do two things to my raw result from Mysql:
- Add a new attribute
openingHours
to my record. - Use reduce to mapping data row into weekday (digit).
This works like a charm but is not good for scale. Actually, I am considering move the callback in reduce into another directory for well-management. As a newbie in Javascript, I don't know how to name this directory in Javascript. Or am I just think it in the wrong way?
I developed with Ruby on Rails for a while. And we do have something alike in Rails which is called decorators
when we need some view-only logics in view-model layer.
thanks.
p.s. findCafeshopByName
calls knex querying API.
via fung
No comments:
Post a Comment