Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions packages/rum-core/src/domain/resource/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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,
})
})
})

Expand Down
26 changes: 17 additions & 9 deletions packages/rum-core/src/domain/resource/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface GraphQlError {
}

export interface GraphQlMetadata {
operationType: 'query' | 'mutation' | 'subscription'
operationType?: 'query' | 'mutation' | 'subscription'
operationName?: string
variables?: string
payload?: string
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -118,7 +126,7 @@ export function extractGraphQlRequestMetadata(
operationType,
operationName,
variables,
payload: trackPayload ? safeTruncate(query, GRAPHQL_PAYLOAD_LIMIT, '...') : undefined,
payload,
}
}

Expand Down