Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make context available to Sequelize hooks? #77

Open
charlie-s opened this issue Aug 23, 2022 · 4 comments
Open

Make context available to Sequelize hooks? #77

charlie-s opened this issue Aug 23, 2022 · 4 comments

Comments

@charlie-s
Copy link

Could the context be passed through to Sequelize hooks through options?

In our authorizer function we attach the current user to ctx and use that for logging and related activity through a request's lifecycle. We can use graphql.before.create/update to achieve this, but that only applies to the graphql request and not any subsequent Sequelize model work.

@alirizwan
Copy link
Collaborator

@charlie-s we can schedule a call and I can help you with this.

@JoeHogan
Copy link

after digging in to the library a bit, I was able to accomplish this by doing a few things... want to document them here for my own benefit.

  1. Make sure you pass context to graphql, like this:

this.schema = new GraphQLSchema(schema); return createHandler({ schema: this.schema, context: (req): {req: Request} => { return {req: req.raw} } })

  1. Update all all Sequelize models (at app start-up) with an 'options' scope function, and add the graphql scope option to the model like this:

Object.keys(sequelize.models).forEach(key => { let model: any = sequelize.models[key]; model.options.scopes = { options(context: any) { return {context}; } } model.graphql = { scopes: ['options', 'context'] } })

Thats it. Since this library calls the model.scope() function when performing queries, and the 'args' and 'context' graphql options are available in that scope function, this should add the 'context' property to your Sequelize options with the graphql context. You can then use this in Sequelize hooks.

@JoeHogan
Copy link

I guess i should mention that perhaps the easier way to do this is through a change to the library... for example, in Update.js, change line 173

FROM:
await model.scope(scope).update(input, { where, transaction });

TO:
await model.scope(scope).update(input, { where, transaction, context });

Happy to put in a PR for this, but perhaps there is a reason it is not already done...

@alirizwan
Copy link
Collaborator

this approach is not future proof. With the next release being TS based, this won't be possible. The firs approach or acquiring models in a context would make more sense. But I can think of something around it, something which is also future proof :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants