Skip to content

Commit

Permalink
Generate inline fragments with different __typenames
Browse files Browse the repository at this point in the history
  • Loading branch information
richardguerre committed Oct 23, 2021
1 parent a4324bf commit 9b89ce4
Show file tree
Hide file tree
Showing 2 changed files with 2,017 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/relay-test-utils/RelayMockPayloadGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type MockPayloadGeneratorOptions = {
minRandomArrayLength?: number;
arrayLength?: number;
maxRandomArrayLength?: number;
sequencialInlineFragmentTypename?: boolean;
};

function createIdGenerator() {
Expand Down Expand Up @@ -200,6 +201,7 @@ class RelayMockPayloadGenerator {
_mockResolvers: MockResolvers;
_selectionMetadata: SelectionMetadata;
_options: MockPayloadGeneratorOptions | null;
_inlineFragmentStore: { [path: string]: number };

constructor(options: {
readonly variables: Variables;
Expand All @@ -222,6 +224,7 @@ class RelayMockPayloadGenerator {
...options.options,
useLimitArguments: true,
};
this._inlineFragmentStore = {};
}

generate(
Expand Down Expand Up @@ -410,8 +413,31 @@ class RelayMockPayloadGenerator {
(mockData[TYPENAME_KEY] == null ||
mockData[TYPENAME_KEY] === DEFAULT_MOCK_TYPENAME)
) {
const allPossibleTypenames = [];
for (const selection of selections) {
if (
selection.kind === 'InlineFragment' &&
selection.type !== 'Node'
) {
allPossibleTypenames.push(selection.type);
}
}
const joinedPath = path.join('.');
let typenameIndex = 0;
if (this._options?.sequencialInlineFragmentTypename) {
const currentIndex = this._inlineFragmentStore[joinedPath] ?? 0;
typenameIndex = currentIndex;
this._inlineFragmentStore[joinedPath] =
(currentIndex + 1) % allPossibleTypenames.length;
} else {
typenameIndex = Math.round(
Math.random() * (allPossibleTypenames.length - 1)
);
}
mockData[TYPENAME_KEY] =
defaultValues?.[TYPENAME_KEY] ?? selection.type;
defaultValues?.[TYPENAME_KEY] ??
allPossibleTypenames[typenameIndex] ??
selection.type;
}

// Now, we need to make sure that we don't select abstract type
Expand Down
Loading

0 comments on commit 9b89ce4

Please sign in to comment.