Skip to content

Commit

Permalink
Use the new metadata field on react-cache hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilios1995 committed Sep 2, 2022
1 parent 8496810 commit 5702c6e
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ function useRefetchableFragmentNode<
componentDisplayName: string,
): ReturnType<TQuery, TKey, InternalOptions> {
const parentEnvironment = useRelayEnvironment();
const {refetchableRequest, fragmentRefPathInResponse, identifierField} =
getRefetchMetadata(fragmentNode, componentDisplayName);
const {
refetchableRequest,
fragmentRefPathInResponse,
identifierField,
identifierQueryVariableName,
} = getRefetchMetadata(fragmentNode, componentDisplayName);
const fragmentIdentifier = getFragmentIdentifier(
fragmentNode,
parentFragmentRef,
Expand Down Expand Up @@ -233,6 +237,7 @@ function useRefetchableFragmentNode<
debugPreviousIDAndTypename = debugFunctions.getInitialIDAndType(
refetchQuery.request.variables,
fragmentRefPathInResponse,
identifierQueryVariableName,
environment,
);
}
Expand Down Expand Up @@ -341,6 +346,7 @@ function useRefetchableFragmentNode<
fragmentNode,
fragmentRefPathInResponse,
identifierField,
identifierQueryVariableName,
loadQuery,
parentFragmentRef,
refetchableRequest,
Expand Down Expand Up @@ -375,6 +381,7 @@ function useRefetchFunction<TQuery: OperationType>(
fragmentNode: ReaderFragment,
fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
identifierField: ?string,
identifierQueryVariableName: ?string,
loadQuery: LoaderFn<TQuery>,
parentFragmentRef: mixed,
refetchableRequest: ConcreteRequest,
Expand Down Expand Up @@ -448,8 +455,8 @@ function useRefetchFunction<TQuery: OperationType>(
// If the query needs an identifier value ('id' or similar) and one
// was not explicitly provided, read it from the fragment data.
if (
identifierField != null &&
!providedRefetchVariables.hasOwnProperty('id')
identifierQueryVariableName != null &&
!providedRefetchVariables.hasOwnProperty(identifierQueryVariableName)
) {
// @refetchable fragments are guaranteed to have an `id` selection
// if the type is Node, implements Node, or is @fetchable. Double-check
Expand All @@ -463,7 +470,7 @@ function useRefetchFunction<TQuery: OperationType>(
identifierValue,
);
}
(refetchVariables: $FlowFixMe).id = identifierValue;
(refetchVariables: $FlowFixMe)[identifierQueryVariableName] = identifierValue;
}

const refetchQuery = createOperationDescriptor(
Expand Down Expand Up @@ -512,10 +519,11 @@ if (__DEV__) {
getInitialIDAndType(
memoRefetchVariables: ?Variables,
fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
identifierQueryVariableName: ?string,
environment: IEnvironment,
): ?DebugIDandTypename {
const {Record} = require('relay-runtime');
const id = memoRefetchVariables?.id;
const id = memoRefetchVariables?.[identifierQueryVariableName ?? 'id'];
if (
fragmentRefPathInResponse.length !== 1 ||
fragmentRefPathInResponse[0] !== 'node' ||
Expand Down

0 comments on commit 5702c6e

Please sign in to comment.