mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.35k stars 237 forks source link

behavior of hooks #554

Open YuriFontella opened 3 years ago

YuriFontella commented 3 years ago

Hello, I would like to know if it is normal behavior for a hook to be triggered with graphql queries? Considering, that it has its own hooks function.

hook that is loaded:

app.addHook('preHandler', async () => {
  ...
})

graphql specific hook in mercurius:

app.graphql.addHook('preParsing', async () => {
  ...
})

Demo project: https://github.com/YuriFontella/graphql-add-hook-test

The hook loaded as a plugin, is called for every graphql query executed.

smolinari commented 3 years ago

@YuriFontella - Every request to a Mercurius drivin graphql server triggers a hook.

https://mercurius.dev/#/docs/lifecycle

Scott

YuriFontella commented 3 years ago

@YuriFontella - Every request to a Mercurius drivin graphql server triggers a hook.

https://mercurius.dev/#/docs/lifecycle

Scott

I looked at it, and it was weird. Because in the lifecycle there is no preHandler hook. And mercurius has its own function to add hooks "app.graphql.addHook"

app.addHook('preHandler', async () => {
  console.log('Im executed in graphql queries')
})

It didn't make sense for me to call this hook in graphql queries

smolinari commented 3 years ago

Yeah, so app.addhook would be for a fastify hook, right? And the same goes, they are for all requests.

https://www.fastify.io/docs/latest/Hooks/

Scott