Note
This integration supports both Apollo iOS 1.0+ and Apollo iOS 2.0+. The setup differs between versions, so please follow the instructions for your Apollo iOS version below.
To include the integration for Apollo iOS in your project, add the following to your Package.swift file:
dependencies: [
// For Apollo iOS 1.0+
.package(url: "https://github.com/DataDog/dd-sdk-ios-apollo-interceptor", .upToNextMajor(from: "1.0.0"))
// For Apollo iOS 2.0+
.package(url: "https://github.com/DataDog/dd-sdk-ios-apollo-interceptor", .upToNextMajor(from: "2.0.0"))
]Alternatively, you can add it using Xcode:
- Go to File → Add Package Dependencies
- Enter the repository URL:
https://github.com/DataDog/dd-sdk-ios-apollo-interceptor - Select the package version that matches your Apollo major version (choose 1.x.x for Apollo iOS 1.0+ or 2.x.x for Apollo iOS 2.0+).
- Set up RUM monitoring with the Datadog iOS SDK:
- Set up network instrumentation for Apollo's built-in URLSessionClient:
import Apollo
URLSessionInstrumentation.enable(with: .init(delegateClass: URLSessionClient.self))- Add the Datadog interceptor to your Apollo Client setup:
import Apollo
import DatadogApollo
class CustomInterceptorProvider: DefaultInterceptorProvider {
override func interceptors<Operation: GraphQLOperation>(for operation: Operation) -> [ApolloInterceptor] {
var interceptors = super.interceptors(for: operation)
interceptors.insert(DatadogApolloInterceptor(), at: 0)
return interceptors
}
}- Set up RUM monitoring with the Datadog iOS SDK:
- Set up network instrumentation.
Apollo 2.0 requires creating a custom URLSession delegate (unlike Apollo 1.0 which uses the built-in URLSessionClient):
First, create a custom URLSession delegate class:
class ApolloURLSessionDelegate: NSObject, URLSessionDataDelegate {
// Datadog will automatically instrument this delegate
}Then, enable instrumentation for your custom delegate:
URLSessionInstrumentation.enable(with: .init(delegateClass: ApolloURLSessionDelegate.self))Configure your Apollo Client to use the custom URLSession with this delegate:
// Create custom URLSession with the delegate for Datadog tracking
let configuration = URLSessionConfiguration.default
let sessionDelegate = ApolloURLSessionDelegate()
let customSession = URLSession(
configuration: configuration,
delegate: sessionDelegate,
delegateQueue: nil
)
// Pass the custom session to your RequestChainNetworkTransport
let networkTransport = RequestChainNetworkTransport(
urlSession: customSession,
interceptorProvider: NetworkInterceptorProvider(),
store: store,
endpointURL: url
)- Create an interceptor provider with the Datadog interceptor:
import Apollo
import DatadogApollo
struct NetworkInterceptorProvider: InterceptorProvider {
func graphQLInterceptors<Operation>(for operation: Operation) -> [any GraphQLInterceptor] where Operation : GraphQLOperation {
return [DatadogApolloInterceptor()] + DefaultInterceptorProvider.shared.graphQLInterceptors(for: operation)
}
}Note
This automatically adds Datadog headers to your GraphQL requests, enabling them to be tracked by Datadog. Note that while query and mutation operations are tracked, subscription operations are not.
Sending GraphQL payloads is disabled by default. To enable it, set the sendGraphQLPayloads flag in the DatadogApollo interceptor constructor as shown below:
let datadogInterceptor = DatadogApolloInterceptor(sendGraphQLPayloads: true)Contributions are welcome! For details, see the Contributing Guide.
Apache License, v2.0