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

Add hooks to Mercurius #385

Merged
merged 16 commits into from
Feb 2, 2021

Conversation

jonnydgreen
Copy link
Contributor

Closes: #380

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done a quick pass, I need to get deeper but I think there are a few things to fix already.

lib/hooks.js Outdated Show resolved Hide resolved
lib/hooks.js Outdated Show resolved Hide resolved
lib/hooks.js Outdated Show resolved Hide resolved
test/gateway/hooks.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! How do the hooks play with subscriptions?

lib/handlers.js Outdated Show resolved Hide resolved
lib/handlers.js Outdated Show resolved Hide resolved
lib/handlers.js Show resolved Hide resolved
lib/handlers.js Show resolved Hide resolved
lib/handlers.js Outdated Show resolved Hide resolved
@jonnydgreen
Copy link
Contributor Author

Thanks! I have tested on a very basic level and seems to be playing okay. I'll write some thorough tests for subscriptions for both normal server and gateway mode to make sure it is all working as expected :) I also noticed that the CI checks are intermittently failing for some of the subscription tests. At first glance it seems to be an environmental issue (other checks for the same node version pass fine) - did you have any thoughts on this? (Example run: https://github.com/mercurius-js/mercurius/pull/385/checks?check_run_id=1791951386)

@mcollina
Copy link
Collaborator

I've seen the flaky tests... can't do much about it right now. If you'd like to investigate and fix them, I'd be very grateful!

@jonnydgreen
Copy link
Contributor Author

For sure, very happy to take a look at the tests! :)

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! I did another pass and I've left a few comments.

lib/hooks.js Outdated Show resolved Hide resolved
test/hooks.js Outdated Show resolved Hide resolved
test/hooks.js Outdated Show resolved Hide resolved
test/hooks.js Outdated Show resolved Hide resolved
test/hooks.js Outdated Show resolved Hide resolved
test/gateway/hooks.js Outdated Show resolved Hide resolved
test/gateway/hooks.js Outdated Show resolved Hide resolved
test/gateway/hooks.js Outdated Show resolved Hide resolved
test/hooks.js Outdated Show resolved Hide resolved
lib/gateway/make-resolver.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, one last nit.

lib/handlers.js Show resolved Hide resolved
Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you link the new docs pages from the README as well?

@jonnydgreen
Copy link
Contributor Author

jonnydgreen commented Feb 2, 2021

Could you link the new docs pages from the README as well?

Sure thing!

Good work! How do the hooks play with subscriptions?

After a bit of experimentation, I think we need to add a few more hook events within lib/subscription-connection.js. From my observations with subscriptions so far, I have seen that preGatewayExecution is executed when using federated subscriptions. We don't see any other hooks triggered because subscriptions don't go through the normal request route. To fix this, I suggest we add preParsing, preExecution and onResolution (not preValidation because we don't do any validation in subscriptions) to lib/subscription-connection.js to make sure we capture the full GraphQL lifecycle for subscriptions.

My only concern with this approach is the meaning of context when applied to the hooks. Because it is not strictly associated with a request over the wire, one could argue that we should have subscription specific hooks for this situation instead. I wondered what your thoughts were on this?

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 361e953 into mercurius-js:master Feb 2, 2021
@mcollina
Copy link
Collaborator

mcollina commented Feb 2, 2021

My only concern with this approach is the meaning of context when applied to the hooks. Because it is not strictly associated with a request over the wire, one could argue that we should have subscription specific hooks for this situation instead. I wondered what your thoughts were on this?

It's better to have a subscription specific hook, ideally in a follow up pull request. This is good to ship right now, so I'm shipping it as a few people are waiting on this!

@jonnydgreen
Copy link
Contributor Author

Yep, makes sense - happy to do a follow up PR for this as you suggested! Thanks for shipping this afternoon! :)

@jonnydgreen jonnydgreen deleted the feature/mercurius-hooks branch February 14, 2021 19:27
By the time the `preValidation` hook triggers, the query string has been parsed into a GraphQL Document AST.

```js
fastify.addHook('preValidation', async (schema, document, context) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be fastify.graphql.addHook? only the first example in this document has that, not the others

Copy link
Contributor Author

@jonnydgreen jonnydgreen Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, yep - I'll submit a PR to fix this over lunch! Unless you want to! :)

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

Successfully merging this pull request may close these issues.

Add hooks to Mercurius
4 participants