diff --git a/packages/rum-core/src/domain/resource/graphql.spec.ts b/packages/rum-core/src/domain/resource/graphql.spec.ts index 1ae18ba17b..3211a846ab 100644 --- a/packages/rum-core/src/domain/resource/graphql.spec.ts +++ b/packages/rum-core/src/domain/resource/graphql.spec.ts @@ -98,10 +98,15 @@ describe('GraphQL detection and metadata extraction', () => { expect(result).toBeUndefined() }) - it('should return undefined for non-GraphQL request body', () => { + it('should return metadata with undefined fields for non-GraphQL request body', () => { const requestBody = JSON.stringify({ data: 'some data' }) const result = extractGraphQlRequestMetadata(requestBody, true) - expect(result).toBeUndefined() + expect(result).toEqual({ + operationType: undefined, + operationName: undefined, + variables: undefined, + payload: undefined, + }) }) it('should handle GraphQL queries with leading and trailing whitespace', () => { @@ -129,7 +134,12 @@ describe('GraphQL detection and metadata extraction', () => { }) const result = extractGraphQlRequestMetadata(requestBody, true) - expect(result).toBeUndefined() + expect(result).toEqual({ + operationType: undefined, + operationName: 'GetUser', + variables: '{"id":"123"}', + payload: '{ user { id name } }', + }) }) it('should return undefined for queries with invalid operation type', () => { @@ -140,7 +150,28 @@ describe('GraphQL detection and metadata extraction', () => { }) const result = extractGraphQlRequestMetadata(requestBody, true) - expect(result).toBeUndefined() + expect(result).toEqual({ + operationType: undefined, + operationName: 'GetUser', + variables: '{"id":"123"}', + payload: 'invalid GetUser { user { id name } }', + }) + }) + + it('should extract operation name and variables when query is absent', () => { + const requestBody = JSON.stringify({ + operationName: 'GetUser', + variables: { id: '123' }, + }) + + const result = extractGraphQlRequestMetadata(requestBody, true) + + expect(result).toEqual({ + operationType: undefined, + operationName: 'GetUser', + variables: '{"id":"123"}', + payload: undefined, + }) }) }) diff --git a/packages/rum-core/src/domain/resource/graphql.ts b/packages/rum-core/src/domain/resource/graphql.ts index 1f622c2d09..18f7a4b089 100644 --- a/packages/rum-core/src/domain/resource/graphql.ts +++ b/packages/rum-core/src/domain/resource/graphql.ts @@ -15,7 +15,7 @@ export interface GraphQlError { } export interface GraphQlMetadata { - operationType: 'query' | 'mutation' | 'subscription' + operationType?: 'query' | 'mutation' | 'subscription' operationName?: string variables?: string payload?: string @@ -88,7 +88,11 @@ export function extractGraphQlRequestMetadata( return } - let graphqlBody: { query?: string; operationName?: string; variables?: unknown } + let graphqlBody: { + query?: string + operationName?: string + variables?: unknown + } try { graphqlBody = JSON.parse(requestBody) @@ -97,18 +101,22 @@ export function extractGraphQlRequestMetadata( return } - if (!graphqlBody || !graphqlBody.query) { + if (!graphqlBody) { return } - const query = graphqlBody.query.trim() - const operationType = getOperationType(query) - const operationName = graphqlBody.operationName + let operationType: 'query' | 'mutation' | 'subscription' | undefined + let payload: string | undefined - if (!operationType) { - return + if (graphqlBody.query) { + const trimmedQuery = graphqlBody.query.trim() + operationType = getOperationType(trimmedQuery) + if (trackPayload) { + payload = safeTruncate(trimmedQuery, GRAPHQL_PAYLOAD_LIMIT, '...') + } } + const operationName = graphqlBody.operationName let variables: string | undefined if (graphqlBody.variables) { variables = JSON.stringify(graphqlBody.variables) @@ -118,7 +126,7 @@ export function extractGraphQlRequestMetadata( operationType, operationName, variables, - payload: trackPayload ? safeTruncate(query, GRAPHQL_PAYLOAD_LIMIT, '...') : undefined, + payload, } }