diff --git a/packages/apollo-engine-reporting/src/extension.ts b/packages/apollo-engine-reporting/src/extension.ts index a95d09b7faf..37ca3adfcc1 100644 --- a/packages/apollo-engine-reporting/src/extension.ts +++ b/packages/apollo-engine-reporting/src/extension.ts @@ -19,6 +19,10 @@ import { EngineReportingOptions, GenerateClientInfo } from './agent'; import { defaultSignature } from './signature'; import { GraphQLRequestContext } from 'apollo-server-core/dist/requestPipelineAPI'; +const clientNameHeaderKey = 'apollographql-client-name'; +const clientReferenceIdHeaderKey = 'apollographql-client-reference-id'; +const clientVersionHeaderKey = 'apollographql-client-version'; + // EngineReportingExtension is the per-request GraphQLExtension which creates a // trace (in protobuf Trace format) for a single request. When the request is // done, it passes the Trace back to its associated EngineReportingAgent via the @@ -55,10 +59,32 @@ export class EngineReportingExtension this.nodes.set(responsePathAsString(undefined), root); this.generateClientInfo = options.generateClientInfo || - // Default to using the clientInfo field of the request's extensions, when - // the ClientInfo fields are undefined, we send the empty string - (({ request }) => - (request.extensions && request.extensions.clientInfo) || {}); + // Default to using the `apollo-client-x` header fields if present. + // If none are present, fallback on the `clientInfo` query extension + // for backwards compatibility. + // The default value if neither header values nor query extension is + // set is the empty String for all fields (as per protobuf defaults) + (({ request }) => { + if ( + request.http && + request.http.headers && + (request.http.headers.get(clientNameHeaderKey) || + request.http.headers.get(clientVersionHeaderKey) || + request.http.headers.get(clientReferenceIdHeaderKey)) + ) { + return { + clientName: request.http.headers.get(clientNameHeaderKey), + clientVersion: request.http.headers.get(clientVersionHeaderKey), + clientReferenceId: request.http.headers.get( + clientReferenceIdHeaderKey, + ), + }; + } else if (request.extensions && request.extensions.clientInfo) { + return request.extensions.clientInfo; + } else { + return {}; + } + }); } public requestDidStart(o: {