almost-full-stack / graphcraft

Rapildy build and extend GraphQL API based on Sequelize models. This library helps you focus on business logic while taking care of GraphQL schema automatically.
https://almost-full-stack.github.io/graphcraft/
MIT License
51 stars 19 forks source link

Make context available to Sequelize hooks? #77

Open charlie-s opened 2 years ago

charlie-s commented 2 years ago

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 commented 2 years ago

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

JoeHogan commented 1 year ago

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} } })

2) 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 commented 1 year ago

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 commented 1 year ago

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 :).