Skip to content

Commit

Permalink
Refactor metadata selection into helper function
Browse files Browse the repository at this point in the history
Reviewed By: monicatang

Differential Revision: D57464333

fbshipit-source-id: e558e13a70148ddf1b5314ae1fe4b96504377834
  • Loading branch information
Fernando Gorodscy authored and facebook-github-bot committed May 17, 2024
1 parent f87e77d commit ed6b934
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/relay-test-utils/RelayMockPayloadGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,7 @@ class RelayMockPayloadGenerator {
// We will pass this data down to selection, so _mockScalar(...) can use
// values from `defaults`
const selectionPath = [...path, applicationName];
const typeFromSelection = this._selectionMetadata[
// When selecting metadata, skip the number on plural fields so that every field in the array
// gets the same metadata.
selectionPath.filter(field => isNaN(parseInt(field, 10))).join('.')
] ?? {
const typeFromSelection = this._getTypeDetailsForPath(selectionPath) ?? {
type: DEFAULT_MOCK_TYPENAME,
};

Expand Down Expand Up @@ -913,18 +909,29 @@ class RelayMockPayloadGenerator {
+nullable: boolean,
} {
return (
this._selectionMetadata[
// When selecting metadata, skip the number on plural fields so that every field in the array
// gets the same metadata.
selectionPath.filter(field => isNaN(parseInt(field, 10))).join('.')
] ?? {
this._getTypeDetailsForPath(selectionPath) ?? {
type: field.name === 'id' ? 'ID' : 'String',
plural: false,
enumValues: null,
nullable: false,
}
);
}

/**
* When selecting metadata, skip the number on plural fields so that every field in the array
* gets the same metadata.
* @private
*/
_getTypeDetailsForPath(
path: $ReadOnlyArray<string>,
): $Values<SelectionMetadata> {
return this._selectionMetadata[
// When selecting metadata, skip the number on plural fields so that every field in the array
// gets the same metadata.
path.filter(field => isNaN(parseInt(field, 10))).join('.')
];
}
}

/**
Expand Down

0 comments on commit ed6b934

Please sign in to comment.