Skip to content

Commit 4694cfb

Browse files
ithinkdancanrgaignault
authored andcommitted
Add support for persisted GraphQL Queries
1 parent 38dd992 commit 4694cfb

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/rum-core/src/domain/resource/graphql.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ describe('GraphQL detection and metadata extraction', () => {
142142
const result = extractGraphQlRequestMetadata(requestBody, true)
143143
expect(result).toBeUndefined()
144144
})
145+
146+
it('should extract query operation name and variables for persisted queries', () => {
147+
const requestBody = JSON.stringify({
148+
extensions: {
149+
persistedQuery: {
150+
sha256Hash: 'somehashvalue',
151+
},
152+
},
153+
operationName: 'GetUser',
154+
variables: { id: '123' },
155+
})
156+
157+
const result = extractGraphQlRequestMetadata(requestBody, true)
158+
159+
expect(result).toEqual({
160+
operationName: 'GetUser',
161+
variables: '{"id":"123"}',
162+
})
163+
})
145164
})
146165

147166
describe('request payload truncation', () => {

packages/rum-core/src/domain/resource/graphql.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface GraphQlError {
1515
}
1616

1717
export interface GraphQlMetadata {
18-
operationType: 'query' | 'mutation' | 'subscription'
18+
operationType?: 'query' | 'mutation' | 'subscription'
1919
operationName?: string
2020
variables?: string
2121
payload?: string
@@ -88,7 +88,12 @@ export function extractGraphQlRequestMetadata(
8888
return
8989
}
9090

91-
let graphqlBody: { query?: string; operationName?: string; variables?: unknown }
91+
let graphqlBody: {
92+
query?: string
93+
operationName?: string
94+
variables?: unknown
95+
extensions?: { persistedQuery?: unknown }
96+
}
9297

9398
try {
9499
graphqlBody = JSON.parse(requestBody)
@@ -97,6 +102,13 @@ export function extractGraphQlRequestMetadata(
97102
return
98103
}
99104

105+
if (graphqlBody && graphqlBody.extensions?.persistedQuery) {
106+
return {
107+
operationName: graphqlBody.operationName,
108+
variables: graphqlBody.variables ? JSON.stringify(graphqlBody.variables) : undefined,
109+
}
110+
}
111+
100112
if (!graphqlBody || !graphqlBody.query) {
101113
return
102114
}

0 commit comments

Comments
 (0)