Skip to content

Commit

Permalink
enhance(delegate): performance improvements (#6579)
Browse files Browse the repository at this point in the history
* enhance(delegate): performance improvements

* Let's go
  • Loading branch information
ardatan authored Oct 16, 2024
1 parent 4b95bb5 commit 8a16b01
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 230 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-keys-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/delegate': patch
---

Performance improvements on upstream request execution
4 changes: 0 additions & 4 deletions benchmark/federation/k6.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const isPrinted = __ENV.GITHUB_TOKEN && __ENV.PRODUCTS_SIZE == 1000;
export const options = {
vus: 1,
duration: '10s',
thresholds: {
no_errors: ['rate>0.98'],
expected_result: ['rate>0.98'],
},
};

export function handleSummary(data) {
Expand Down
129 changes: 0 additions & 129 deletions packages/delegate/src/OverlappingAliasesTransform.ts

This file was deleted.

31 changes: 27 additions & 4 deletions packages/delegate/src/Transformer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ExecutionRequest, ExecutionResult } from '@graphql-tools/utils';
import { checkResultAndHandleErrors } from './checkResultAndHandleErrors.js';
import { finalizeGatewayRequest } from './finalizeGatewayRequest.js';
import { OverlappingAliasesTransform } from './OverlappingAliasesTransform.js';
import { prepareGatewayDocument } from './prepareGatewayDocument.js';
import { DelegationContext, Transform } from './types.js';

Expand All @@ -13,6 +12,7 @@ interface Transformation<TContext> {
export class Transformer<TContext extends Record<string, any> = Record<string, any>> {
private transformations: Array<Transformation<TContext>> = [];
private delegationContext: DelegationContext<TContext>;
private hasOverlappingAliases = false;

constructor(context: DelegationContext<TContext>) {
this.delegationContext = context;
Expand All @@ -21,8 +21,6 @@ export class Transformer<TContext extends Record<string, any> = Record<string, a
for (const transform of delegationTransforms) {
this.addTransform(transform);
}
// TODO: Move this to the core, later
this.addTransform(new OverlappingAliasesTransform());
}

private addTransform(transform: Transform<any, TContext>, context = {}) {
Expand Down Expand Up @@ -50,7 +48,9 @@ export class Transformer<TContext extends Record<string, any> = Record<string, a
}
}

return finalizeGatewayRequest(request, this.delegationContext);
return finalizeGatewayRequest(request, this.delegationContext, () => {
this.hasOverlappingAliases = true;
});
}

public transformResult(originalResult: ExecutionResult) {
Expand All @@ -66,6 +66,29 @@ export class Transformer<TContext extends Record<string, any> = Record<string, a
);
}
}
if (this.hasOverlappingAliases) {
result = removeOverlappingAliases(result);
}
return checkResultAndHandleErrors(result, this.delegationContext);
}
}

function removeOverlappingAliases(result: any): any {
if (result != null) {
if (Array.isArray(result)) {
return result.map(removeOverlappingAliases);
} else if (typeof result === 'object') {
const newResult: Record<string, any> = {};
for (const key in result) {
if (key.startsWith('_nullable_') || key.startsWith('_nonNullable_')) {
const newKey = key.replace(/^_nullable_/, '').replace(/^_nonNullable_/, '');
newResult[newKey] = removeOverlappingAliases(result[key]);
} else {
newResult[key] = removeOverlappingAliases(result[key]);
}
}
return newResult;
}
}
return result;
}
Loading

0 comments on commit 8a16b01

Please sign in to comment.