diff --git a/packages/react-relay/relay-hooks/useMutation.js b/packages/react-relay/relay-hooks/useMutation.js index d7dfa82820748..0f97bacf39f26 100644 --- a/packages/react-relay/relay-hooks/useMutation.js +++ b/packages/react-relay/relay-hooks/useMutation.js @@ -12,11 +12,11 @@ 'use strict'; import type { + CommitMutationConfig, DeclarativeMutationConfig, Disposable, IEnvironment, Mutation, - MutationConfig, MutationParameters, PayloadError, SelectorStoreUpdater, @@ -68,17 +68,7 @@ function useMutation( mutation: Mutation, 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, ) => Disposable = defaultCommitMutation, ): [ (UseMutationConfigInternal) => Disposable, diff --git a/packages/relay-runtime/index.js b/packages/relay-runtime/index.js index 4638742a4eb29..048525474165b 100644 --- a/packages/relay-runtime/index.js +++ b/packages/relay-runtime/index.js @@ -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, diff --git a/packages/relay-runtime/mutations/__tests__/commitMutation-test.js b/packages/relay-runtime/mutations/__tests__/commitMutation-test.js index baac50bf24153..3d60c2be3f2ae 100644 --- a/packages/relay-runtime/mutations/__tests__/commitMutation-test.js +++ b/packages/relay-runtime/mutations/__tests__/commitMutation-test.js @@ -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, @@ -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, @@ -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, }); @@ -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, }); @@ -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, @@ -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, @@ -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, }); @@ -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, }); diff --git a/packages/relay-runtime/mutations/commitMutation.js b/packages/relay-runtime/mutations/commitMutation.js index c4eed0b6b8558..00754e506b829 100644 --- a/packages/relay-runtime/mutations/commitMutation.js +++ b/packages/relay-runtime/mutations/commitMutation.js @@ -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'); @@ -54,13 +59,28 @@ export type MutationConfig = { variables: TMutation['variables'], }; +export type CommitMutationConfig = { + cacheConfig?: CacheConfig, + configs?: Array, + mutation: Mutation, + onCompleted?: ?(response: TData, errors: ?Array) => void, + onError?: ?(error: Error) => void, + onNext?: ?() => void, + onUnsubscribe?: ?() => void, + optimisticResponse?: TRawResponse, + optimisticUpdater?: ?SelectorStoreUpdater, + updater?: ?SelectorStoreUpdater, + uploadables?: UploadableMap, + variables: TVariables, +}; + /** * Higher-level helper function to execute a mutation against a specific * environment. */ -function commitMutation( +function commitMutation( environment: IEnvironment, - config: MutationConfig, + config: CommitMutationConfig, ): Disposable { invariant( isRelayModernEnvironment(environment), @@ -85,6 +105,8 @@ function commitMutation( ); // 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, @@ -98,16 +120,21 @@ function commitMutation( } } 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 = []; const subscription = environment - .executeMutation({ + .executeMutation<{ + variables: TVariables, + /* $FlowFixMe[incompatible-call] error exposed when improving flow typing + * of commitMutation */ + response: TData, + }>({ operation, optimisticResponse, optimisticUpdater,