Joellao
10/13/2018, 2:57 PMUsulPro
10/13/2018, 3:52 PMFran Dios
10/13/2018, 4:13 PMJoellao
10/14/2018, 5:36 PMme(parent, args, ctx, info) {
const id = getUserId(ctx);
return ctx.db.query.user({ where: { id } }, info);
}
When this user is returned i want to attach an extra filed on the response if it's being asked. In the schema i declared it like ``rateAVG: Float`` and this should return a float number based on the avg function. So to calculate the average i was thinking on raw sql or maybe something like this let rates = ctx.db.query.rates({});
let sum = 0;
for (let i = 0; i < rates.length; i++) {
sum += rates[i];
}
let average = rates.length != null ? sum / rates.length : 0;
But no clue on how to achieve that, thanks for the help thoJidé
10/14/2018, 9:55 PMasync me(parent, args, ctx, info) {
const id = getUserId(ctx);
const user = await ctx.db.query.user({ where: { id } }, info);
const average = //... Do your thing.
return {
...user,
average
};
}
Jidé
10/14/2018, 9:58 PMtype CustomUser {
# ... fields from User type
average: Float
}
Jidé
10/14/2018, 10:00 PMtype Query {
#... other queries
me: CustomUser
}