Skip to content

Commit e54f31b

Browse files
Merge pull request #2556 from DataDog/bplasovska/chore/graphql-constants
GraphQLHeaders constants Co-authored-by: barboraplasovska <[email protected]>
2 parents 00bb35c + d036bd9 commit e54f31b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

DatadogInternal/Sources/Attributes/Attributes.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public struct CrossPlatformAttributes {
140140
public static let graphqlPayload = "_dd.graphql.payload"
141141

142142
/// Custom attribute passed when starting GraphQL RUM resources resources from a cross platform SDK.
143-
/// It sets the GraphQL varibles as a JSON string if they were defined by the developer.
143+
/// It sets the GraphQL variables as a JSON string if they were defined by the developer.
144144
/// Expects `String` value.
145145
public static let graphqlVariables = "_dd.graphql.variables"
146146

@@ -160,6 +160,22 @@ public struct CrossPlatformAttributes {
160160
public static let customINVValue: String = "_dd.view.custom_inv_value"
161161
}
162162

163+
/// HTTP header names used to pass GraphQL metadata from the application to the SDK.
164+
/// These headers are read from intercepted requests and mapped to internal attributes.
165+
public struct GraphQLHeaders {
166+
/// HTTP header name for GraphQL operation name.
167+
public static let operationName: String = "_dd-custom-header-graph-ql-operation-name"
168+
169+
/// HTTP header name for GraphQL operation type.
170+
public static let operationType: String = "_dd-custom-header-graph-ql-operation-type"
171+
172+
/// HTTP header name for GraphQL variables.
173+
public static let variables: String = "_dd-custom-header-graph-ql-variables"
174+
175+
/// HTTP header name for GraphQL payload.
176+
public static let payload: String = "_dd-custom-header-graph-ql-payload"
177+
}
178+
163179
public struct LaunchArguments {
164180
/// Each product should consider this argument to offer simple debugging experience.
165181
/// For example, if this flag is present it can use no sampling.

DatadogRUM/Sources/Instrumentation/Resources/URLSessionRUMResourcesHandler.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,16 @@ internal final class URLSessionRUMResourcesHandler: DatadogURLSessionHandler, RU
118118

119119
// Extract GraphQL attributes from request headers
120120
var combinedAttributes = userAttributes
121-
if let operationName = originalRequest.value(forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-name") {
121+
if let operationName = originalRequest.value(forHTTPHeaderField: GraphQLHeaders.operationName) {
122122
combinedAttributes[CrossPlatformAttributes.graphqlOperationName] = operationName
123123
}
124-
if let operationType = originalRequest.value(forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-type") {
124+
if let operationType = originalRequest.value(forHTTPHeaderField: GraphQLHeaders.operationType) {
125125
combinedAttributes[CrossPlatformAttributes.graphqlOperationType] = operationType
126126
}
127-
if let variables = originalRequest.value(forHTTPHeaderField: "_dd-custom-header-graph-ql-variables") {
127+
if let variables = originalRequest.value(forHTTPHeaderField: GraphQLHeaders.variables) {
128128
combinedAttributes[CrossPlatformAttributes.graphqlVariables] = variables
129129
}
130-
if let payload = originalRequest.value(forHTTPHeaderField: "_dd-custom-header-graph-ql-payload") {
130+
if let payload = originalRequest.value(forHTTPHeaderField: GraphQLHeaders.payload) {
131131
combinedAttributes[CrossPlatformAttributes.graphqlPayload] = payload
132132
}
133133

DatadogRUM/Tests/Instrumentation/Resources/URLSessionRUMResourcesHandlerTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,10 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase {
954954

955955
// Given
956956
var mockRequest: URLRequest = .mockWith(url: "https://graphql.example.com/api")
957-
mockRequest.setValue("GetUser", forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-name")
958-
mockRequest.setValue("query", forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-type")
959-
mockRequest.setValue("{\"userId\":\"123\"}", forHTTPHeaderField: "_dd-custom-header-graph-ql-variables")
960-
mockRequest.setValue("query GetUser($userId: ID!) { user(id: $userId) { name } }", forHTTPHeaderField: "_dd-custom-header-graph-ql-payload")
957+
mockRequest.setValue("GetUser", forHTTPHeaderField: GraphQLHeaders.operationName)
958+
mockRequest.setValue("query", forHTTPHeaderField: GraphQLHeaders.operationType)
959+
mockRequest.setValue("{\"userId\":\"123\"}", forHTTPHeaderField: GraphQLHeaders.variables)
960+
mockRequest.setValue("query GetUser($userId: ID!) { user(id: $userId) { name } }", forHTTPHeaderField: GraphQLHeaders.payload)
961961

962962
let immutableRequest = ImmutableRequest(request: mockRequest)
963963
let taskInterception = URLSessionTaskInterception(request: immutableRequest, isFirstParty: false)
@@ -989,8 +989,8 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase {
989989

990990
// Given
991991
var mockRequest: URLRequest = .mockWith(url: "https://graphql.example.com/api")
992-
mockRequest.setValue("FailedMutation", forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-name")
993-
mockRequest.setValue("mutation", forHTTPHeaderField: "_dd-custom-header-graph-ql-operation-type")
992+
mockRequest.setValue("FailedMutation", forHTTPHeaderField: GraphQLHeaders.operationName)
993+
mockRequest.setValue("mutation", forHTTPHeaderField: GraphQLHeaders.operationType)
994994

995995
let immutableRequest = ImmutableRequest(request: mockRequest)
996996
let taskInterception = URLSessionTaskInterception(request: immutableRequest, isFirstParty: false)

0 commit comments

Comments
 (0)