forked from DataDog/browser-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql.scenario.ts
More file actions
272 lines (247 loc) · 10.3 KB
/
Copy pathgraphql.scenario.ts
File metadata and controls
272 lines (247 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { test, expect } from '@playwright/test'
import { createTest } from '../../lib/framework'
function buildGraphQlConfig({
trackPayload = false,
trackResponseErrors = false,
}: {
trackPayload?: boolean
trackResponseErrors?: boolean
} = {}) {
return {
allowedGraphQlUrls: [{ match: (url: string) => url.includes('graphql'), trackPayload, trackResponseErrors }],
}
}
test.describe('GraphQL tracking', () => {
createTest('track GraphQL query via XHR and include payload')
.withRum(buildGraphQlConfig({ trackPayload: true }))
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const xhr = new XMLHttpRequest()
xhr.open('POST', '/graphql')
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(
JSON.stringify({
query: 'query GetUser($id: ID!) { user(id: $id) { name email } }',
operationName: 'GetUser',
variables: { id: '123' },
})
)
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.method).toBe('POST')
expect(resourceEvent.resource.graphql).toEqual({
operationType: 'query',
operationName: 'GetUser',
variables: '{"id":"123"}',
payload: 'query GetUser($id: ID!) { user(id: $id) { name email } }',
})
})
createTest('track GraphQL mutation via fetch without payload')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() =>
window.fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'mutation CreateUser($input: UserInput!) { createUser(input: $input) { id name } }',
operationName: 'CreateUser',
variables: { input: { name: 'John Doe', email: 'john@example.com' } },
}),
})
)
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.method).toBe('POST')
expect(resourceEvent.resource.graphql).toEqual({
operationType: 'mutation',
operationName: 'CreateUser',
variables: '{"input":{"name":"John Doe","email":"john@example.com"}}',
payload: undefined,
})
})
createTest('should not track GraphQL for non-matching URLs')
.withRum(buildGraphQlConfig({ trackPayload: true }))
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const xhr = new XMLHttpRequest()
xhr.open('GET', '/ok')
xhr.send()
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/ok'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql).toBeUndefined()
})
createTest('track GraphQL response errors via fetch')
.withRum(buildGraphQlConfig({ trackPayload: false, trackResponseErrors: true }))
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() =>
window.fetch('/graphql?scenario=validation-error', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'query GetUser { user { unknownField } }',
operationName: 'GetUser',
}),
})
)
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql).toEqual({
operationType: 'query',
operationName: 'GetUser',
variables: undefined,
payload: undefined,
error_count: 1,
errors: [
{
message: 'Field "unknownField" does not exist',
code: 'GRAPHQL_VALIDATION_FAILED',
locations: [{ line: 2, column: 5 }],
path: ['user', 'unknownField'],
},
],
})
})
createTest('track GraphQL response with multiple errors via XHR')
.withRum(buildGraphQlConfig({ trackResponseErrors: true }))
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const xhr = new XMLHttpRequest()
xhr.open('POST', '/graphql?scenario=multiple-errors')
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(
JSON.stringify({
query: 'query GetUser { user { name } }',
})
)
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql?.error_count).toBe(2)
expect(resourceEvent.resource.graphql?.errors).toEqual([
{ message: 'User not found' },
{ message: 'Insufficient permissions', code: 'UNAUTHORIZED' },
])
})
createTest('should not track response errors when trackResponseErrors is false')
.withRum(buildGraphQlConfig({ trackPayload: true, trackResponseErrors: false }))
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() =>
window.fetch('/graphql?scenario=validation-error', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'query Test { test }',
}),
})
)
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql?.error_count).toBeUndefined()
expect(resourceEvent.resource.graphql?.errors).toBeUndefined()
})
createTest('track GraphQL GET request with persisted query')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const operationName = 'GetUser'
const variables = JSON.stringify({ id: '123' })
const extensions = JSON.stringify({
persistedQuery: { version: 1, sha256Hash: 'abc123' },
})
const url = `/graphql?operationName=${encodeURIComponent(operationName)}&variables=${encodeURIComponent(variables)}&extensions=${encodeURIComponent(extensions)}`
return window.fetch(url, { method: 'GET' })
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.method).toBe('GET')
expect(resourceEvent.resource.graphql).toEqual({
operationType: undefined,
operationName: 'GetUser',
variables: '{"id":"123"}',
payload: undefined,
})
})
createTest('use operationType from POST body when query field is absent')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() =>
window.fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
operationName: 'CreateUser',
operationType: 'mutation',
variables: { name: 'Alice' },
}),
})
)
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql).toEqual({
operationType: 'mutation',
operationName: 'CreateUser',
variables: '{"name":"Alice"}',
payload: undefined,
})
})
createTest('query field operationType takes precedence over explicit operationType in POST body')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() =>
window.fetch('/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'query GetUser { user { id } }',
operationName: 'GetUser',
operationType: 'mutation',
}),
})
)
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql?.operationType).toBe('query')
})
createTest('use operationType from GET URL params when query param is absent')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const url = `/graphql?operationName=CreateUser&operationType=mutation&variables=${encodeURIComponent(JSON.stringify({ name: 'Bob' }))}`
return window.fetch(url, { method: 'GET' })
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.method).toBe('GET')
expect(resourceEvent.resource.graphql).toEqual({
operationType: 'mutation',
operationName: 'CreateUser',
variables: '{"name":"Bob"}',
payload: undefined,
})
})
createTest('query param operationType takes precedence over explicit operationType GET URL param')
.withRum(buildGraphQlConfig())
.run(async ({ intakeRegistry, flushEvents, page }) => {
await page.evaluate(() => {
const url = `/graphql?query=${encodeURIComponent('query GetUser { user { id } }')}&operationName=GetUser&operationType=mutation`
return window.fetch(url, { method: 'GET' })
})
await flushEvents()
const resourceEvent = intakeRegistry.rumResourceEvents.find((event) => event.resource.url.includes('/graphql'))!
expect(resourceEvent).toBeDefined()
expect(resourceEvent.resource.graphql?.operationType).toBe('query')
})
})