Plugins
Plugins enable you to extend Apollo Server's core functionality by performing custom operations in response to certain events. Currently, these events correspond to individual phases of the GraphQL request lifecycle, and to the startup of Apollo Server itself (read more here). For example, a basic logging plugin might log the GraphQL query string associated with each request that's sent to Apollo Server.
Custom plugins
To create a plugin, declare a class annotated with the @Plugin
decorator exported from the @nestjs/graphql
package. Also, for better code autocompletion, implement the ApolloServerPlugin
interface from the apollo-server-plugin-base
package.
import { Plugin } from '@nestjs/graphql';
import {
ApolloServerPlugin,
GraphQLRequestListener,
} from 'apollo-server-plugin-base';
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
requestDidStart(): GraphQLRequestListener {
console.log('Request started');
return {
willSendResponse() {
console.log('Will send response');
},
};
}
}
With this in place, we can register the LoggingPlugin
as a provider.
@Module({
providers: [LoggingPlugin],
})
export class CommonModule {}
Nest will automatically instantiate a plugin and apply it to the Apollo Server.
Using external plugins
There are several plugins provided out-of-the-box. To use an existing plugin, simply import it and add it to the plugins
array:
GraphQLModule.forRoot({
// ...
plugins: [ApolloServerOperationRegistry({ /* options */})]
}),
info Hint The
ApolloServerOperationRegistry
plugin is exported from theapollo-server-plugin-operation-registry
package.