Skip to content

Commit

Permalink
Add type param to SelectorStoreUpdater, with a default type of any
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D30730783

fbshipit-source-id: 16f468c479095c3365aecb5462983e519c0f1197
  • Loading branch information
rbalicki2 authored and facebook-github-bot committed Oct 26, 2021
1 parent 2f04533 commit 1b8b188
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 45 deletions.
6 changes: 4 additions & 2 deletions packages/react-relay/relay-hooks/useMutation.js
Expand Up @@ -49,8 +49,10 @@ export type UseMutationConfig<TMutation: MutationParameters> = {|
},
'rawResponse',
>,
optimisticUpdater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<
$ElementType<TMutation, 'response'>,
>,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
uploadables?: UploadableMap,
variables: $ElementType<TMutation, 'variables'>,
|};
Expand Down
Expand Up @@ -195,7 +195,7 @@ class ActorSpecificEnvironment implements IActorEnvironment {

executeSubscription<TMutation: MutationParameters>(config: {
operation: OperationDescriptor,
updater: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
}): RelayObservable<GraphQLResponse> {
return this.multiActorEnvironment.executeSubscription(this, config);
}
Expand Down
Expand Up @@ -355,7 +355,7 @@ class MultiActorEnvironment implements IMultiActorEnvironment {
updater,
}: {
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
},
): RelayObservable<GraphQLResponse> {
return this._execute(actorEnvironment, {
Expand Down Expand Up @@ -452,7 +452,7 @@ class MultiActorEnvironment implements IMultiActorEnvironment {
isClientPayload: boolean,
operation: OperationDescriptor,
optimisticConfig: ?OptimisticResponseConfig<TMutation>,
updater: ?SelectorStoreUpdater,
updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|},
): RelayObservable<GraphQLResponse> {
return RelayObservable.create(sink => {
Expand Down
Expand Up @@ -206,7 +206,7 @@ export interface IMultiActorEnvironment {
actorEnvironment: IActorEnvironment,
config: {
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
},
): RelayObservable<GraphQLResponse>;

Expand Down
26 changes: 16 additions & 10 deletions packages/relay-runtime/mutations/RelayDeclarativeMutationConfig.js
Expand Up @@ -87,11 +87,13 @@ export type DeclarativeMutationConfig =
function convert<TMutation: MutationParameters>(
configs: Array<DeclarativeMutationConfig>,
request: ConcreteRequest,
optimisticUpdater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<
$ElementType<TMutation, 'response'>,
>,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
): {
optimisticUpdater: SelectorStoreUpdater,
updater: SelectorStoreUpdater,
optimisticUpdater: SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
updater: SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
...
} {
const configOptimisticUpdates = optimisticUpdater ? [optimisticUpdater] : [];
Expand Down Expand Up @@ -141,13 +143,13 @@ function convert<TMutation: MutationParameters>(
function nodeDelete(
config: NodeDeleteConfig,
request: ConcreteRequest,
): ?SelectorStoreUpdater {
): ?SelectorStoreUpdater<mixed> {
const {deletedIDFieldName} = config;
const rootField = getRootField(request);
if (!rootField) {
return null;
}
return (store: RecordSourceSelectorProxy, data: ?SelectorData) => {
return (store: RecordSourceSelectorProxy, data: ?mixed) => {
const payload = store.getRootField(rootField);
if (!payload) {
return;
Expand All @@ -165,7 +167,7 @@ function nodeDelete(
function rangeAdd(
config: RangeAddConfig,
request: ConcreteRequest,
): ?SelectorStoreUpdater {
): ?SelectorStoreUpdater<mixed> {
const {parentID, connectionInfo, edgeName} = config;
if (!parentID) {
warning(
Expand All @@ -179,7 +181,7 @@ function rangeAdd(
if (!connectionInfo || !rootField) {
return null;
}
return (store: RecordSourceSelectorProxy, data: ?SelectorData) => {
return (store: RecordSourceSelectorProxy, data: ?mixed) => {
const parent = store.get(parentID);
if (!parent) {
return;
Expand Down Expand Up @@ -233,7 +235,7 @@ function rangeAdd(
function rangeDelete(
config: RangeDeleteConfig,
request: ConcreteRequest,
): ?SelectorStoreUpdater {
): ?SelectorStoreUpdater<mixed> {
const {
parentID,
connectionKeys,
Expand All @@ -252,12 +254,16 @@ function rangeDelete(
if (!rootField) {
return null;
}
return (store: RecordSourceSelectorProxy, data: ?SelectorData) => {
return (store: RecordSourceSelectorProxy, data: ?mixed) => {
if (!data) {
return;
}
const deleteIDs = [];
// the type of data should come from a type parameter associated with ConcreteRequest,
// but ConcreteRequest does not contain a type parameter. Hence, we use a FlowFixMe.
// $FlowFixMe[incompatible-use] see above
let deletedIDField = data[rootField];

if (deletedIDField && Array.isArray(deletedIDFieldName)) {
for (const eachField of deletedIDFieldName) {
if (deletedIDField && typeof deletedIDField === 'object') {
Expand Down
4 changes: 3 additions & 1 deletion packages/relay-runtime/mutations/applyOptimisticMutation.js
Expand Up @@ -33,7 +33,9 @@ export type OptimisticMutationConfig<TMutation: MutationParameters> = {|
configs?: ?Array<DeclarativeMutationConfig>,
mutation: GraphQLTaggedNode,
variables: Variables,
optimisticUpdater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<
$ElementType<TMutation, 'response'>,
>,
optimisticResponse?: Object,
|};

Expand Down
29 changes: 17 additions & 12 deletions packages/relay-runtime/mutations/commitMutation.js
Expand Up @@ -37,52 +37,57 @@ const validateMutation = require('./validateMutation');
const invariant = require('invariant');
const warning = require('warning');

export type DEPRECATED_MutationConfig<T> = {|
export type DEPRECATED_MutationConfig<TMutationResponse> = {|
configs?: Array<DeclarativeMutationConfig>,
cacheConfig?: CacheConfig,
mutation: GraphQLTaggedNode,
variables: Variables,
uploadables?: UploadableMap,
onCompleted?: ?(response: T, errors: ?Array<PayloadError>) => void,
onCompleted?: ?(
response: TMutationResponse,
errors: ?Array<PayloadError>,
) => void,
onError?: ?(error: Error) => void,
onUnsubscribe?: ?() => void,
optimisticUpdater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<TMutationResponse>,
optimisticResponse?: Object,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<TMutationResponse>,
|};

export type MutationConfig<T: MutationParameters> = {|
export type MutationConfig<TMutation: MutationParameters> = {|
configs?: Array<DeclarativeMutationConfig>,
cacheConfig?: CacheConfig,
mutation: GraphQLTaggedNode,
onError?: ?(error: Error) => void,
onCompleted?: ?(
response: $ElementType<T, 'response'>,
response: $ElementType<TMutation, 'response'>,
errors: ?Array<PayloadError>,
) => void,
onNext?: ?() => void,
onUnsubscribe?: ?() => void,
optimisticResponse?: $ElementType<
{
+rawResponse?: {...},
...T,
...TMutation,
...
},
'rawResponse',
>,
optimisticUpdater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<
$ElementType<TMutation, 'response'>,
>,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
uploadables?: UploadableMap,
variables: $ElementType<T, 'variables'>,
variables: $ElementType<TMutation, 'variables'>,
|};

/**
* Higher-level helper function to execute a mutation against a specific
* environment.
*/
function commitMutation<T: MutationParameters>(
function commitMutation<TMutation: MutationParameters>(
environment: IEnvironment,
config: MutationConfig<T>,
config: MutationConfig<TMutation>,
): Disposable {
invariant(
isRelayModernEnvironment(environment),
Expand Down
6 changes: 3 additions & 3 deletions packages/relay-runtime/store/OperationExecutor.js
Expand Up @@ -95,7 +95,7 @@ export type ExecuteConfig<TMutation: MutationParameters> = {|
+sink: Sink<GraphQLResponse>,
+source: RelayObservable<GraphQLResponse>,
+treatMissingFieldsAsNull: boolean,
+updater?: ?SelectorStoreUpdater,
+updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
+log: LogFunction,
|};

Expand Down Expand Up @@ -164,7 +164,7 @@ class Executor<TMutation: MutationParameters> {
_state: 'started' | 'loading_incremental' | 'loading_final' | 'completed';
+_getStore: (actorIdentifier: ActorIdentifier) => Store;
_subscriptions: Map<number, Subscription>;
_updater: ?SelectorStoreUpdater;
_updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>;
_asyncStoreUpdateDisposable: ?Disposable;
_completeFns: Array<() => void>;
+_retainDisposables: Map<ActorIdentifier, Disposable>;
Expand Down Expand Up @@ -601,7 +601,7 @@ class Executor<TMutation: MutationParameters> {

_processOptimisticResponse(
response: ?GraphQLResponseWithData,
updater: ?SelectorStoreUpdater,
updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
treatMissingFieldsAsNull: boolean,
): void {
invariant(
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-runtime/store/RelayModernEnvironment.js
Expand Up @@ -368,7 +368,7 @@ class RelayModernEnvironment implements IEnvironment {
updater,
}: {|
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|}): RelayObservable<GraphQLResponse> {
return this._execute({
createSource: () =>
Expand Down Expand Up @@ -468,7 +468,7 @@ class RelayModernEnvironment implements IEnvironment {
isClientPayload: boolean,
operation: OperationDescriptor,
optimisticConfig: ?OptimisticResponseConfig<TMutation>,
updater: ?SelectorStoreUpdater,
updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|}): RelayObservable<GraphQLResponse> {
const publishQueue = this._publishQueue;
const store = this._store;
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-runtime/store/RelayPublishQueue.js
Expand Up @@ -46,7 +46,7 @@ type PendingRelayPayload<TMutation: MutationParameters> = {|
+kind: 'payload',
+operation: OperationDescriptor,
+payload: RelayResponsePayload,
+updater: ?SelectorStoreUpdater,
+updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|};
type PendingRecordSource = {|
+kind: 'source',
Expand Down Expand Up @@ -160,7 +160,7 @@ class RelayPublishQueue implements PublishQueue {
commitPayload<TMutation: MutationParameters>(
operation: OperationDescriptor,
payload: RelayResponsePayload,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
): void {
this._pendingBackupRebase = true;
this._pendingData.add({
Expand Down
16 changes: 9 additions & 7 deletions packages/relay-runtime/store/RelayStoreTypes.js
Expand Up @@ -744,7 +744,7 @@ export interface IEnvironment {
*/
executeSubscription<TMutation: MutationParameters>(config: {|
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|}): RelayObservable<GraphQLResponse>;

/**
Expand Down Expand Up @@ -974,7 +974,7 @@ export type StoreUpdater = (store: RecordSourceProxy) => void;
* order to easily access the root fields of a query/mutation as well as a
* second argument of the response object of the mutation.
*/
export type SelectorStoreUpdater = (
export type SelectorStoreUpdater<-TMutationResponse = any> = (
store: RecordSourceSelectorProxy,
// Actually SelectorData, but mixed is inconvenient to access deeply in
// product code.
Expand All @@ -996,13 +996,13 @@ export type OptimisticUpdateFunction = {|
export type OptimisticUpdateRelayPayload<TMutation: MutationParameters> = {|
+operation: OperationDescriptor,
+payload: RelayResponsePayload,
+updater: ?SelectorStoreUpdater,
+updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|};

export type OptimisticResponseConfig<TMutation: MutationParameters> = {|
+operation: OperationDescriptor,
+response: ?PayloadData,
+updater: ?SelectorStoreUpdater,
+updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
|};

/**
Expand Down Expand Up @@ -1072,9 +1072,11 @@ export type RelayResponsePayload = {|
*/
export type ExecuteMutationConfig<TMutation: MutationParameters> = {|
operation: OperationDescriptor,
optimisticUpdater?: ?SelectorStoreUpdater,
optimisticUpdater?: ?SelectorStoreUpdater<
$ElementType<TMutation, 'response'>,
>,
optimisticResponse?: ?Object,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
uploadables?: ?UploadableMap,
|};
Expand Down Expand Up @@ -1107,7 +1109,7 @@ export interface PublishQueue {
commitPayload<TMutation: MutationParameters>(
operation: OperationDescriptor,
payload: RelayResponsePayload,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
): void;
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/relay-runtime/subscription/requestSubscription.js
Expand Up @@ -47,7 +47,7 @@ export type GraphQLSubscriptionConfig<T: SubscriptionParameters> = {|
onCompleted?: ?() => void,
onError?: ?(error: Error) => void,
onNext?: ?(response: ?$ElementType<T, 'response'>) => void,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<$ElementType<T, 'response'>>,
|};

export type DEPRECATED_GraphQLSubscriptionConfig<TSubscriptionPayload> = {|
Expand All @@ -58,7 +58,7 @@ export type DEPRECATED_GraphQLSubscriptionConfig<TSubscriptionPayload> = {|
onCompleted?: ?() => void,
onError?: ?(error: Error) => void,
onNext?: ?(response: ?TSubscriptionPayload) => void,
updater?: ?SelectorStoreUpdater,
updater?: ?SelectorStoreUpdater<TSubscriptionPayload>,
|};

function requestSubscription<TSubscriptionPayload>(
Expand Down

0 comments on commit 1b8b188

Please sign in to comment.