-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathgraphql.scenario.ts
More file actions
165 lines (151 loc) · 6.07 KB
/
graphql.scenario.ts
File metadata and controls
165 lines (151 loc) · 6.07 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
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()
})
})