-
-
Save dnafication/595be1a651da65175de9528075b9e3a8 to your computer and use it in GitHub Desktop.
Empty plugin sample for Apollo Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ApolloServerPlugin, | |
GraphQLRequestContext, | |
GraphQLRequestListener, | |
GraphQLServiceContext, | |
GraphQLResponse, | |
} from 'apollo-server-plugin-base'; | |
import { | |
ApolloError, | |
AuthenticationError, | |
ForbiddenError, | |
} from 'apollo-server-errors'; | |
export interface ApolloIdempotentConfig { | |
myOption: boolean | |
} | |
export class ApolloIdempotent implements ApolloServerPlugin, GraphQLRequestListener { | |
private myOption: boolean; | |
constructor(config: ApolloIdempotentConfig) { | |
this.myOption = config.myOption; | |
} | |
/** | |
* Server Lifecycle Handlers | |
*/ | |
public serverWillStart(service: GraphQLServiceContext) { | |
console.log("ApolloIdempotent::serverWillStart", service); | |
} | |
/** | |
* requestDidStart | |
* Triggered at the beginning of every request cycle, and returns an object (GraphQLRequestListener) | |
* that has the functions for each request lifecycle event. | |
*/ | |
public requestDidStart(context: GraphQLRequestContext): GraphQLRequestListener | void { | |
console.log("ApolloIdempotent::requestDidStart", context); | |
// Assume the common 'source' and 'metrics' fields are available here for all | |
console.log("ApolloIdempotent::requestDidStart source", context.source); | |
console.log("ApolloIdempotent::requestDidStart metrics", context.metrics); | |
return this; | |
} | |
/** | |
* Request Lifecycle Handlers | |
*/ | |
/** | |
* The parsingDidStart event fires whenever Apollo Server will parse a GraphQL request to create its associated document AST. | |
* If Apollo Server receives a request with a query string that matches a previous request, the associated document might already be | |
* available in Apollo Server's cache. In this case, parsingDidStart is not called for the request, because parsing does not occur. | |
* @param context 'metrics' | 'source' | |
*/ | |
public parsingDidStart(context: GraphQLRequestContext): void { | |
console.log("ApolloIdempotent::parsingDidStart", context); | |
} | |
/** | |
* The validationDidStart event fires whenever Apollo Server will validate a request's document AST against your GraphQL schema. | |
* Like parsingDidStart, this event does not fire if a request's document is already available in Apollo Server's cache | |
* (only successfully validated documents are cached by Apollo Server). | |
* The document AST is guaranteed to be available at this stage, because parsing must succeed for validation to occur. | |
* @param context 'metrics' | 'source' | 'document' | |
*/ | |
public validationDidStart(context: GraphQLRequestContext): void { | |
console.log("ApolloIdempotent::validationDidStart", context); | |
} | |
/** | |
* The didResolveOperation event fires after the graphql library successfully determines the operation to execute from a request's document AST. | |
* At this stage, both the operationName string and operation AST are available. | |
* @param context 'metrics' | 'source' | 'document' | 'operationName' | 'operation' | |
*/ | |
public didResolveOperation(context: GraphQLRequestContext) { | |
console.log("ApolloIdempotent::didResolveOperation", context); | |
console.log("ApolloIdempotent::didResolveOperation document", context.document); | |
console.log("ApolloIdempotent::didResolveOperation operationName", context.operationName); | |
console.log("ApolloIdempotent::didResolveOperation operation", context.operation); | |
} | |
/** | |
* The responseForOperation event is fired immediately before GraphQL execution would take place. | |
* If its return value resolves to a non-null GraphQLResponse, that result is used instead of executing the query. | |
* Hooks from different plugins are invoked in series and the first non-null response is used. | |
* @param context 'metrics' | 'source' | 'document' | 'operationName' | 'operation' | |
*/ | |
public responseForOperation(context: GraphQLRequestContext): GraphQLResponse | null { | |
console.log("ApolloIdempotent::responseForOperation", context); | |
console.log("ApolloIdempotent::responseForOperation document", context.document); | |
console.log("ApolloIdempotent::responseForOperation operationName", context.operationName); | |
console.log("ApolloIdempotent::responseForOperation operation", context.operation); | |
return null; | |
} | |
/** | |
* The executionDidStart event fires whenever Apollo Server begins executing the GraphQL operation specified by a request's document AST. | |
* @param context 'metrics' | 'source' | 'document' | 'operationName' | 'operation' | |
*/ | |
public executionDidStart(context: GraphQLRequestContext): void { | |
console.log("ApolloIdempotent::executionDidStart", context); | |
console.log("ApolloIdempotent::executionDidStart document", context.document); | |
console.log("ApolloIdempotent::executionDidStart operationName", context.operationName); | |
console.log("ApolloIdempotent::executionDidStart operation", context.operation); | |
} | |
/** | |
* The didEncounterErrors event fires when Apollo Server encounters errors while parsing, validating, or executing a GraphQL operation. | |
* @param context 'metrics' | 'source' | 'errors' | |
*/ | |
public didEncounterErrors(context: GraphQLRequestContext): void { | |
console.log("ApolloIdempotent::didEncounterErrors", context); | |
console.log("ApolloIdempotent::didEncounterErrors errors:", context.errors); | |
} | |
/** | |
* The willSendResponse event fires whenever Apollo Server is about to send a response for a GraphQL operation. | |
* This event fires (and Apollo Server sends a response) even if the GraphQL operation encounters one or more errors. | |
* @param context 'metrics' | 'response' | |
*/ | |
public willSendResponse(context: GraphQLRequestContext): void { | |
console.log("ApolloIdempotent::willSendResponse", context); | |
console.log("ApolloIdempotent::willSendResponse errors:", context.response); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment