Skip to content

Commit

Permalink
Prevent OOM on complex queries
Browse files Browse the repository at this point in the history
Use a 2 pass strategy
  • Loading branch information
esquevin authored and Guillaume Esquevin committed Jun 28, 2023
1 parent df8d7a1 commit 8dc72ee
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/graphiql-toolkit/src/graphql-helpers/merge-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function mergeAst(
}
}

const visitors: ASTVisitor = {
const flattenVisitors: ASTVisitor = {
SelectionSet(node: any) {
const selectionSetType = typeInfo ? typeInfo.getParentType() : null;
let { selections } = node;
Expand All @@ -135,6 +135,25 @@ export function mergeAst(
selectionSetType,
);

return {
...node,
selections,
};
},
FragmentDefinition() {
return null;
},
};

const flattenedAST = visit(
documentAST,
typeInfo ? visitWithTypeInfo(typeInfo, flattenVisitors) : flattenVisitors,
);

const deduplicateVisitors: ASTVisitor = {
SelectionSet(node: any) {
let { selections } = node;

selections = uniqueBy(selections, selection =>
selection.alias ? selection.alias.value : selection.name.value,
);
Expand All @@ -149,8 +168,5 @@ export function mergeAst(
},
};

return visit(
documentAST,
typeInfo ? visitWithTypeInfo(typeInfo, visitors) : visitors,
);
return visit(flattenedAST, deduplicateVisitors);
}

0 comments on commit 8dc72ee

Please sign in to comment.