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 #380

Closed
6 tasks done
jonnydgreen opened this issue Jan 21, 2021 · 2 comments · Fixed by #385
Closed
6 tasks done

Add hooks to Mercurius #380

jonnydgreen opened this issue Jan 21, 2021 · 2 comments · Fixed by #385

Comments

@jonnydgreen
Copy link
Contributor

jonnydgreen commented Jan 21, 2021

In order to define gateway authentication and authorization as discussed in #343, we should add GraphQL hooks support to Mercurius so we can process data at different points in the request lifecycle.

Requirements

  • All necessary hooks triggered in Mercurius at the correct stage of the GraphQL request lifecycle
  • One can define multiple hooks of the same type where they run sequentially in the order of their definition
  • Make sure these hooks can be easily extended
  • Make sure these hooks are cleaned up correctly when needed (e.g. errors, timeouts)
  • Make sure just promises are supported
  • Documentation, TypeScript types and tests in Mercurius

Research and Exploration

Fastify currently uses hooks to allows users to define custom functionality at certain events within the codebase. You can define multiple hooks of the same name in this way. They are executed sequentially from the order at which they are defined.

Proposed solution

In Mercurius, we will add support for hooks by implementing core functionality also defined in Fastify. This will allow users of Mercurius and plugins to utilise the defined GraphQL lifecycle events. The hooks we will add will be:

  • preParsing: before queries are extracted from the input request. It will contain the following data:
    • MercuriusContext
    • Request
  • preValidation: before the queries are validated against the schema. It will contain the following data:
    • Extracted query data
    • MercuriusContext
    • Request
  • preExecution: this hook is triggered before queries are sent to the federated services (in the case of a gateway) or executing the query on the current GraphQL service. It will contain the following data:
    • Query AST(s) split by service
    • Destination schema AST(s) split by service
    • MercuriusContext
    • Request
  • preResolution: before the responses from each federated service or single service is crafted into a GraphQL response. It will contain the following data:
    • Response(s) split by service
    • MercuriusContext
    • Request

All new code with be 100% covered by tests and documentation will also be provided. The new hooks will be correctly typed. I have already started work on an implementation of this and will submit a PR for this once it is done and everyone is agreed on the solution :)

@mcollina
Copy link
Collaborator

Thanks for the long description, it's fantastic. SGTM with the plan, are you going to work on it?

A few notes: fastify supports both callbacks and promises. We should just support promises for this as the whole GraphQL ecosystem is all promise-based.

@jonnydgreen
Copy link
Contributor Author

jonnydgreen commented Jan 21, 2021

Thank you and no problem! Yep, I planned on starting the implementation today if that's okay with you?

Thanks for the tips about the callbacks/promise support in Fastify - I'll make sure just promises are supported :)

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 a pull request may close this issue.

2 participants