Skip to content

Commit

Permalink
Merge pull request #60 from jjcollinge/jjcollinge/gql-sql-hooks
Browse files Browse the repository at this point in the history
Add support to define before/after graphql-sequelize hooks
  • Loading branch information
alirizwan authored Feb 16, 2022
2 parents a7d2436 + c89cd2d commit 31ab7e7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ readonly: false, // exclude create/delete/update mutations automatically
fetchDeleted: false, // same as fetchDeleted as global except it lets you override global settings
restoreDeleted: false // same as restoreDeleted as global except it lets you override global settings
```
```javascript
// define hooks to be invoked on find queries {before, after}
find: {}
```
### Usage in Model
```javascript
Product.graphql = {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ const defaultModelGraphqlOptions = {
joins: false, // make a query using join (left/right/inner) instead of batch dataloader, join will appear in all subtype args. Right join won't work for sqlite
readonly: false, // exclude create/delete/update mutations automatically
fetchDeleted: false, // same as fetchDeleted as global except it lets you override global settings
restoreDeleted: false // same as restoreDeleted as global except it lets you override global settings
restoreDeleted: false, // same as restoreDeleted as global except it lets you override global settings
find: {} // define graphql-sequelize find hooks {before, after}
};

const GenerateQueries = require('./libs/generateQueries');
Expand Down
9 changes: 8 additions & 1 deletion src/resolvers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module.exports = (options) => {

// sequelize-graphql before hook to parse orderby clause to make sure it supports multiple orderby
const before = (findOptions, args) => {
// hook coming from graphql.find.before
if (model.graphql?.find?.before) {
model.graphql.find.before(findOptions, args, context);
}

if (isAssociation && model.through) {
findOptions.through = {
Expand Down Expand Up @@ -74,12 +78,15 @@ module.exports = (options) => {
return findOptions;
};

const after = model.graphql?.find?.after;

// see if a scope is specified to be applied to find queries.
const variablePath = { args, context };
const scope = Array.isArray(graphql.scopes) ? { method: [graphql.scopes[0], _.get(variablePath, graphql.scopes[1], graphql.scopes[2] || null)] } : graphql.scopes;
const resolverOptions = {
before,
separate: isAssociation
after,
separate: isAssociation,
};

const data = await resolver((isAssociation ? model : model.scope(scope)), resolverOptions)(source, args, context, info);
Expand Down

0 comments on commit 31ab7e7

Please sign in to comment.