Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Move default client signature generation into its own function. #2668

Merged
merged 1 commit into from
May 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {};
}
}