Skip to content

Commit

Permalink
limit depth to 3, excluding connections, edges and pageinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Mar 9, 2023
1 parent aa183e2 commit 2bc9b10
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions exchanges/populate/src/populateExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface FieldUsage {
}

type FragmentMap<T extends string = string> = Record<T, FragmentDefinitionNode>;
const SKIP_COUNT_TYPE = /^PageInfo|(Connection|Edge)$/;

/** An exchange for auto-populating mutations with a required response body. */
export const populateExchange = ({
Expand Down Expand Up @@ -100,7 +101,10 @@ export const populateExchange = ({
const visited = new Set();
const populateSelections = (
type: GraphQLFlatType,
selections: Array<FieldNode | InlineFragmentNode | FragmentSpreadNode>
selections: Array<
FieldNode | InlineFragmentNode | FragmentSpreadNode
>,
depth: number
) => {
let possibleTypes: readonly string[] = [];
let isAbstract = false;
Expand Down Expand Up @@ -195,12 +199,17 @@ export const populateExchange = ({
typeSelections.push(field);
} else if (
value.type instanceof GraphQLObjectType &&
!visited.has(value.type.name)
!visited.has(value.type.name) &&
depth <= 3
) {
visited.add(value.type.name);
const fieldSelections: Array<FieldNode> = [];

populateSelections(value.type, fieldSelections);
populateSelections(
value.type,
fieldSelections,
SKIP_COUNT_TYPE.test(value.type.name) ? depth : depth + 1
);

const args = value.args
? Object.keys(value.args).map(k => {
Expand Down Expand Up @@ -242,7 +251,7 @@ export const populateExchange = ({
const selections: Array<
FieldNode | InlineFragmentNode | FragmentSpreadNode
> = node.selectionSet ? [...node.selectionSet.selections] : [];
populateSelections(type, selections);
populateSelections(type, selections, 0);

return {
...node,
Expand Down

0 comments on commit 2bc9b10

Please sign in to comment.