@@ -47,6 +47,10 @@ export interface GraphQLOperationLoggingPluginOptions<TContext extends GraphQLCo
4747 * Can be used to adjust the result data before logging, e.g. redacting sensitive data
4848 */
4949 adjustResultData?: (data: Record<string, any>) => Record<string, any>
50+ /**
51+ * Can be used to augment the log entry with additional properties
52+ */
53+ augmentLogEntry?: (ctx: TContext) => Record<string, any>
5054}
5155
5256/**
@@ -63,18 +67,20 @@ export function graphqlOperationLoggingPlugin<TContext extends GraphQLContext<TL
6367 includeMutationResponseData,
6468 adjustVariables,
6569 adjustResultData,
70+ augmentLogEntry,
6671}: GraphQLOperationLoggingPluginOptions<TContext, TLogger> = {}): ApolloServerPlugin<TContext> {
6772 return {
6873 contextCreationDidFail: async ({ error }) => {
6974 contextCreationFailureLogger?.error('Context creation failed', { error })
7075 await contextCreationDidFail?.({ error })
7176 },
7277
73- requestDidStart: ({ contextValue: { started, logger } }): Promise<GraphQLRequestListener<TContext>> => {
78+ requestDidStart: ({ contextValue }): Promise<GraphQLRequestListener<TContext>> => {
7479 function log(
7580 ctx: GraphQLRequestContextWillSendResponse<TContext>,
7681 subsequentPayload?: GraphQLExperimentalFormattedSubsequentIncrementalExecutionResult,
7782 ) {
83+ const { started, logger } = contextValue
7884 const { operationName, query, variables } = ctx.request
7985 const isIntrospection = query && isIntrospectionQuery(query)
8086 if (isIntrospection && ignoreIntrospectionQueries) return
@@ -95,6 +101,8 @@ export function graphqlOperationLoggingPlugin<TContext extends GraphQLContext<TL
95101
96102 const adjustedResult = omitNil({ errors, data: adjustedData }) as Record<string, any>
97103
104+ const additionalLogEntryProperties = augmentLogEntry ? (omitNil(augmentLogEntry(contextValue)) as Record<string, any>) : undefined
105+
98106 logGraphQLOperation({
99107 logger,
100108 logLevel,
@@ -107,6 +115,7 @@ export function graphqlOperationLoggingPlugin<TContext extends GraphQLContext<TL
107115 isIntrospectionQuery: isIntrospection || undefined,
108116 isIncrementalResponse: ctx.response.body.kind === 'incremental' || undefined,
109117 isSubsequentPayload: !!subsequentPayload || undefined,
118+ ...additionalLogEntryProperties,
110119 })
111120 }
112121
0 commit comments