- Introduction
- Overview of GraphWeaver and Apollo Server
- The Role of Apollo Server Plugins in GraphWeaver
- GraphWeaver Apollo Server Plugins
- Clear DataLoader Cache
- Clear Database Context
- Connect To Database
- Cors
- Format GraphQL Error
- Log Errors
- Log Requests
- Mutex Requests In Development
- Custom GraphWeaver Plugins
- Conclusion
Introduction
One of the key features of Graphweaver is its utilisation of Apollo Server plugins.
In this article, we will explore how Graphweaver harnesses the power of Apollo Server plugins to extend and customise the capabilities of its GraphQL servers.
Overview of GraphWeaver and Apollo Server
Graphweaver is built on top of Apollo Server, which is a popular GraphQL server implementation.
Apollo Server provides a solid foundation for building GraphQL APIs, and Graphweaver builds upon this foundation to offer additional functionality and convenience for developers.
The Role of Apollo Server Plugins in GraphWeaver
Apollo Server plugins play a crucial role in enhancing the capabilities of Graphweaver.
They allow Graphweaver to tap into various stages of the GraphQL request lifecycle and apply custom logic or features. With the help of plugins, Graphweaver can modify the behaviour of the server, introduce additional functionality, and integrate seamlessly with other systems or services.
Graphweaver comes bundled with a set of preconfigured Apollo Server plugins that provide valuable features out of the box.
These plugins are specifically selected and configured to enhance the Graphweaver experience.
They cover various aspects such as caching, logging, error handling, authentication, and more.
By including these plugins in the Graphweaver configuration, developers can leverage their benefits effortlessly.
Letβs look at these plugins in more detail.
GraphWeaver Apollo Server Plugins
Clear DataLoader Cache
This plugin ensures that the Data Loader cache is cleared on each request. It helps maintain data consistency and prevents any stale data from being served.
Clear Database Context
The purpose of this plugin is to clear the Entity Manager on each request. It helps ensure a clean database context and avoids any potential data conflicts or inconsistencies.
Connect To Database
This plugin is responsible for connecting to the database.
It takes an array of connection options and establishes connections using the ConnectionManager. It enables seamless interaction with the underlying database for data retrieval and manipulation.
Cors
This plugin handles Cross-Origin Resource Sharing (CORS) for Graphweaver requests.
It sets the appropriate headers to allow or restrict access based on the origin of the request. It ensures secure and controlled communication between the client and the server.
Format GraphQL Error
This plugin provides a custom error formatter for GraphQL errors. It modifies the error message to remove unnecessary details like request bodies, making the error response cleaner and more readable.
Log Errors
This plugin logs encountered errors during request processing. It captures and logs errors, helping with debugging and troubleshooting.
Log Requests
This plugin logs each incoming request, including the query and its variables.
It aids in monitoring and understanding the GraphQL traffic, facilitating analysis and performance optimizsation.
Mutex Requests In Development
This plugin is used only in development environments. It utilises a mutex to synchronise requests, ensuring that only one request is processed at a time.
This can be helpful in scenarios where concurrent processing may lead to conflicts or inconsistencies during development.
Custom GraphWeaver Plugins
GraphWeaver also supports the creation of custom plugins to extend its functionality even further.
Developers can build plugins that cater to their specific requirements and integrate them seamlessly into the Graphweaver ecosystem.
These custom plugins can be used to add additional caching layers, implement specialised authentication strategies, integrate with external services, or introduce any other custom logic needed to meet the application's unique needs. To add them pass your custom plugins into the Apollo Server Options for Graphweaver. Here is an example:
const graphweaver = new Graphweaver<Context>({
...
apolloServerOptions: {
plugins: [myCustomApolloServerPlugin],
},
...
});
Conclusion
Apollo Server plugins are a fundamental aspect of Graphweaver's architecture, enabling the extension and customisation of GraphQL server capabilities.
By leveraging these plugins, developers can enhance the behaviour and functionality of their GraphQL APIs, seamlessly integrating additional features and services.
Graphweaver simplifies the utilisation of Apollo Server plugins by providing a curated set of preconfigured plugins out of the box. These plugins cover essential aspects such as caching, logging, error handling, authentication, and more.
Furthermore, Graphweaver empowers developers to create custom plugins tailored to their specific requirements.
By seamlessly integrating custom plugins into the Graphweaver ecosystem, developers can extend the capabilities of their GraphQL servers to new horizons.
To utilise custom plugins, developers can pass them as options in the Apollo Server configuration for Graphweaver, allowing for seamless integration and extensibility.