There are two life cycle hooks in GraphQL Modules, one represents an incoming request and the other is called when the execution is done. Hooks are only available in Operation scoped Injector.
Every Operation scoped service is created for each incoing GraphQL operation, which means you can use the constructor as the initial hook.
After Operation is resolved and the context about to be destroyed, GraphQL Modules call the
onDestroy method on all operation-scoped services.
As you know, GraphQL operation represents an incoming request with GraphQL query and variables meaning it ends when GraphQL API resolves data and sends back the response. An operation is created when GraphQL execution starts (
execute function call). With operation comes Operation-scoped Dependency Injection.
In some cases you wish to:
- access operation-scoped services before the execution phase happens
- destroy a session and operation-scoped injectors some time after execution phase
This is why
OperationController exists - it allows to fully control the life of the operation-scoped injectors.
Here's an example:
OperationController means you're in charge of the operation flow and you need to destroy the session manually by default.
To improve the developer experience, we decided to introduce
autoDestroy flag that automatically destroys the session right after GraphQL execution phase ends, exactly like without an OperationController.
Keep on mind that using
autoDestroymeans the controller completes its job immediately after execution phase and everything is cleaned up.