Skip to content

Commit

Permalink
chore: Move default client signature generation into its own function.
Browse files Browse the repository at this point in the history
This makes no actual changes to the function, but it moves this rather
large block of code out of the `constructor` for `EngineReportingExtension`.
  • Loading branch information
abernix committed May 13, 2019
1 parent 96897e3 commit e613c41
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions packages/apollo-engine-reporting/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,7 @@ export class EngineReportingExtension<TContext = any>
this.trace.root = root;
this.nodes.set(responsePathAsString(undefined), root);
this.generateClientInfo =
options.generateClientInfo ||
// 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 {};
}
});
options.generateClientInfo || defaultGenerateClientInfo;
}

public requestDidStart(o: {
Expand Down Expand Up @@ -453,3 +427,28 @@ function dateToTimestamp(date: Date): google.protobuf.Timestamp {
function durationHrTimeToNanos(hrtime: [number, number]) {
return hrtime[0] * 1e9 + hrtime[1];
}

function defaultGenerateClientInfo({ request }: GraphQLRequestContext) {
// 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)
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 {};
}
}

0 comments on commit e613c41

Please sign in to comment.