Skip to content

Commit

Permalink
Update types of commitMutation to use types from graphql tags
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D48457658

fbshipit-source-id: 80f40602a4aab2d2d44a3d18165809cb26307391
  • Loading branch information
alunyov authored and facebook-github-bot committed Aug 21, 2023
1 parent 90c1aae commit 603ce54
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
14 changes: 2 additions & 12 deletions packages/react-relay/relay-hooks/useMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
'use strict';

import type {
CommitMutationConfig,
DeclarativeMutationConfig,
Disposable,
IEnvironment,
Mutation,
MutationConfig,
MutationParameters,
PayloadError,
SelectorStoreUpdater,
Expand Down Expand Up @@ -68,17 +68,7 @@ function useMutation<TVariables: Variables, TData, TRawResponse = {...}>(
mutation: Mutation<TVariables, TData, TRawResponse>,
commitMutationFn?: (
environment: IEnvironment,
config: MutationConfig<{
variables: TVariables,
/* $FlowFixMe[incompatible-type-arg] error exposed when improving flow
* typing of useMutation */
response: TData,
/* $FlowFixMe[incompatible-type-arg] error exposed when improving flow
* typing of useMutation */
rawResponse?: TRawResponse,
}>,
/* $FlowFixMe[incompatible-type-arg] error exposed when improving flow typing
* of useMutation */
config: CommitMutationConfig<TVariables, TData, TRawResponse>,
) => Disposable = defaultCommitMutation,
): [
(UseMutationConfigInternal<TVariables, TData, TRawResponse>) => Disposable,
Expand Down
5 changes: 4 additions & 1 deletion packages/relay-runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export type {
RangeOperation,
} from './mutations/RelayDeclarativeMutationConfig';
export type {OptimisticMutationConfig} from './mutations/applyOptimisticMutation';
export type {MutationConfig} from './mutations/commitMutation';
export type {
MutationConfig,
CommitMutationConfig,
} from './mutations/commitMutation';
export type {
ExecuteFunction,
FetchFunction,
Expand Down
30 changes: 30 additions & 0 deletions packages/relay-runtime/mutations/__tests__/commitMutation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ describe('Configs: NODE_DELETE', () => {
store.subscribe(snapshot, callback);
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
mutation,
optimisticResponse,
optimisticUpdater,
Expand Down Expand Up @@ -595,6 +597,10 @@ describe('Configs: RANGE_ADD', () => {
store.subscribe(snapshot, callback);
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
optimisticResponse,
optimisticUpdater,
Expand Down Expand Up @@ -651,6 +657,10 @@ describe('Configs: RANGE_ADD', () => {
// send mutation
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
variables,
});
Expand Down Expand Up @@ -736,6 +746,10 @@ describe('Configs: RANGE_ADD', () => {
// send the same mutation again
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
variables,
});
Expand Down Expand Up @@ -816,6 +830,10 @@ describe('Configs: RANGE_ADD', () => {
store.subscribe(snapshot, callback);
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
optimisticResponse,
optimisticUpdater,
Expand Down Expand Up @@ -881,6 +899,10 @@ describe('Configs: RANGE_ADD', () => {
store.subscribe(snapshot, callback);
commitMutation(environment, {
configs,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
optimisticResponse,
optimisticUpdater,
Expand Down Expand Up @@ -936,6 +958,10 @@ describe('Configs: RANGE_ADD', () => {
// send mutation
commitMutation(environment, {
updater,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
variables,
});
Expand Down Expand Up @@ -1027,6 +1053,10 @@ describe('Configs: RANGE_ADD', () => {
// send the same mutation again
commitMutation(environment, {
updater,
/* $FlowFixMe[prop-missing] error exposed when improving flow typing of
* commitMutation */
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
mutation,
variables,
});
Expand Down
47 changes: 37 additions & 10 deletions packages/relay-runtime/mutations/commitMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import type {
MutationParameters,
SelectorStoreUpdater,
} from '../store/RelayStoreTypes';
import type {CacheConfig, Disposable} from '../util/RelayRuntimeTypes';
import type {
CacheConfig,
Disposable,
Mutation,
Variables,
} from '../util/RelayRuntimeTypes';
import type {DeclarativeMutationConfig} from './RelayDeclarativeMutationConfig';

const {getRequest} = require('../query/GraphQLTag');
Expand Down Expand Up @@ -54,13 +59,28 @@ export type MutationConfig<TMutation: MutationParameters> = {
variables: TMutation['variables'],
};

export type CommitMutationConfig<TVariables, TData, TRawResponse> = {
cacheConfig?: CacheConfig,
configs?: Array<DeclarativeMutationConfig>,
mutation: Mutation<TVariables, TData, TRawResponse>,
onCompleted?: ?(response: TData, errors: ?Array<PayloadError>) => void,
onError?: ?(error: Error) => void,
onNext?: ?() => void,
onUnsubscribe?: ?() => void,
optimisticResponse?: TRawResponse,
optimisticUpdater?: ?SelectorStoreUpdater<TData>,
updater?: ?SelectorStoreUpdater<TData>,
uploadables?: UploadableMap,
variables: TVariables,
};

/**
* Higher-level helper function to execute a mutation against a specific
* environment.
*/
function commitMutation<TMutation: MutationParameters>(
function commitMutation<TVariables: Variables, TData, TRawResponse = {...}>(
environment: IEnvironment,
config: MutationConfig<TMutation>,
config: CommitMutationConfig<TVariables, TData, TRawResponse>,
): Disposable {
invariant(
isRelayModernEnvironment(environment),
Expand All @@ -85,6 +105,8 @@ function commitMutation<TMutation: MutationParameters>(
);
// TODO: remove this check after we fix flow.
if (typeof optimisticResponse === 'function') {
/* $FlowFixMe[incompatible-use] error exposed when improving flow typing of
* commitMutation */
optimisticResponse = optimisticResponse();
warning(
false,
Expand All @@ -98,16 +120,21 @@ function commitMutation<TMutation: MutationParameters>(
}
}
if (configs) {
({optimisticUpdater, updater} = RelayDeclarativeMutationConfig.convert(
configs,
mutation,
optimisticUpdater,
updater,
));
({optimisticUpdater, updater} = RelayDeclarativeMutationConfig.convert<{
variables: TVariables,
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
response: TData,
}>(configs, mutation, optimisticUpdater, updater));
}
const errors: Array<PayloadError> = [];
const subscription = environment
.executeMutation({
.executeMutation<{
variables: TVariables,
/* $FlowFixMe[incompatible-call] error exposed when improving flow typing
* of commitMutation */
response: TData,
}>({
operation,
optimisticResponse,
optimisticUpdater,
Expand Down

0 comments on commit 603ce54

Please sign in to comment.