Skip to content

Commit c6ee3b5

Browse files
committed
Add augmentLogEntry
1 parent 425bfe3 commit c6ee3b5

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@makerx/graphql-apollo-server",
3-
"version": "1.4.2",
3+
"version": "1.5.0",
44
"private": false,
55
"description": "A set of MakerX plugins for Apollo Server",
66
"author": "MakerX",

src/plugins/graphql-operation-logging-plugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)