Resolve
Resolving data
Will now see how graphql-thinky can resolve your definition to an optimised query to run in RethinkDB. The big help is given from thinky the powerful ORM.
This function is the one that allow to request thinky todo certain moves depending the request.
- It automatically bind all the arguments to a filter argument.
- it can automatically limit result if a limit argument has been requested
- it can automatically resolve a order if provided
Resolving data
Fore example, we are going to force the limit of the result to 10.
We also forcing the order by name:desc
.
We use the before and after hooks that allow us to run custom logic, before and after the Database query resolution.
// UserQuery.js
import userType from './userType';
import GT from './graphql-thinky';
const { resolve } = GT;
export default {
users: {
type: userType,
// Run before query is instructed to thinky
resolve: resolve('user',null,{
before: (opts, args, context, info) => {
opts.limit = 10;
opts.orderBy = 'name:desc';
// opts.attributes.concat(['otherfield']);
// opt.filter.name = 'jhon';
return opts;
},
// After the result set has been fetched
// allows you to decorate
after: (result) => {
return result.map((item) => item.name.toUpperCase());
}
})
}
}
Updated less than a minute ago