graphcraft icon indicating copy to clipboard operation
graphcraft copied to clipboard

Make context available to Sequelize hooks?

Open charlie-s opened this issue 2 years ago • 4 comments

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.

charlie-s avatar Aug 23 '22 16:08 charlie-s

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

alirizwan avatar Sep 01 '22 19:09 alirizwan

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 avatar May 16 '23 16:05 JoeHogan

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

JoeHogan avatar May 16 '23 16:05 JoeHogan

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

alirizwan avatar May 18 '23 00:05 alirizwan