Merge pull request #1960 from apollographql/adam/18/11/client-info-default-to-headers

AER/Default to headers for client identity
This commit is contained in:
Adam Zionts 2018-11-13 17:51:25 -08:00 committed by GitHub
commit e40a2e90c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<TContext = any>
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: {