Lifecycles
Lifecycles — GraphQL Modules documentation.
Lifecycle Hooks
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 ongoing GraphQL operation, which means you can use the constructor as the initial hook.
After the Operation is resolved and the context is about to be destroyed, GraphQL Modules call the onDestroy method on all operation-scoped services.
Manual Control of Operation Cycle
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 sometime after the execution phase
This is why OperationController exists - it allows to fully control the life of the operation-scoped injectors.
Here’s an example:
autoDestroy
Using 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 the GraphQL execution phase ends, exactly like without an OperationController.