relay-runtime 0.0.0-main-a2087736 → 0.0.0-main-16753a5d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,7 @@ import type {HandlerProvider} from '../handlers/RelayDefaultHandlerProvider';
16
16
  import type {Disposable} from '../util/RelayRuntimeTypes';
17
17
  import type {GetDataID} from './RelayResponseNormalizer';
18
18
  import type {
19
+ MutationParameters,
19
20
  OperationDescriptor,
20
21
  OptimisticUpdate,
21
22
  PublishQueue,
@@ -37,12 +38,15 @@ const RelayRecordSource = require('./RelayRecordSource');
37
38
  const invariant = require('invariant');
38
39
  const warning = require('warning');
39
40
 
40
- type PendingCommit = PendingRelayPayload | PendingRecordSource | PendingUpdater;
41
- type PendingRelayPayload = {|
41
+ type PendingCommit<TMutation: MutationParameters> =
42
+ | PendingRelayPayload<TMutation>
43
+ | PendingRecordSource
44
+ | PendingUpdater;
45
+ type PendingRelayPayload<TMutation: MutationParameters> = {|
42
46
  +kind: 'payload',
43
47
  +operation: OperationDescriptor,
44
48
  +payload: RelayResponsePayload,
45
- +updater: ?SelectorStoreUpdater,
49
+ +updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
46
50
  |};
47
51
  type PendingRecordSource = {|
48
52
  +kind: 'source',
@@ -78,12 +82,19 @@ class RelayPublishQueue implements PublishQueue {
78
82
  // updates performing a rebase.
79
83
  _pendingBackupRebase: boolean;
80
84
  // Payloads to apply or Sources to publish to the store with the next `run()`.
81
- _pendingData: Set<PendingCommit>;
85
+ // $FlowFixMe[unclear-type] See explanation below.
86
+ _pendingData: Set<PendingCommit<any>>;
82
87
  // Optimistic updaters to add with the next `run()`.
83
- _pendingOptimisticUpdates: Set<OptimisticUpdate>;
88
+ // $FlowFixMe[unclear-type] See explanation below.
89
+ _pendingOptimisticUpdates: Set<OptimisticUpdate<any>>;
84
90
  // Optimistic updaters that are already added and might be rerun in order to
85
91
  // rebase them.
86
- _appliedOptimisticUpdates: Set<OptimisticUpdate>;
92
+ // $FlowFixMe[unclear-type] See explanation below.
93
+ _appliedOptimisticUpdates: Set<OptimisticUpdate<any>>;
94
+ // For _pendingOptimisticUpdates, _appliedOptimisticUpdates, and _pendingData,
95
+ // we want to parametrize by "any" since the type is effectively
96
+ // "the union of all T's that PublishQueue's methods were called with".
97
+
87
98
  // Garbage collection hold, should rerun gc on dispose
88
99
  _gcHold: ?Disposable;
89
100
  _isRunning: ?boolean;
@@ -107,7 +118,9 @@ class RelayPublishQueue implements PublishQueue {
107
118
  /**
108
119
  * Schedule applying an optimistic updates on the next `run()`.
109
120
  */
110
- applyUpdate(updater: OptimisticUpdate): void {
121
+ applyUpdate<TMutation: MutationParameters>(
122
+ updater: OptimisticUpdate<TMutation>,
123
+ ): void {
111
124
  invariant(
112
125
  !this._appliedOptimisticUpdates.has(updater) &&
113
126
  !this._pendingOptimisticUpdates.has(updater),
@@ -120,7 +133,9 @@ class RelayPublishQueue implements PublishQueue {
120
133
  /**
121
134
  * Schedule reverting an optimistic updates on the next `run()`.
122
135
  */
123
- revertUpdate(updater: OptimisticUpdate): void {
136
+ revertUpdate<TMutation: MutationParameters>(
137
+ updater: OptimisticUpdate<TMutation>,
138
+ ): void {
124
139
  if (this._pendingOptimisticUpdates.has(updater)) {
125
140
  // Reverted before it was applied
126
141
  this._pendingOptimisticUpdates.delete(updater);
@@ -142,10 +157,10 @@ class RelayPublishQueue implements PublishQueue {
142
157
  /**
143
158
  * Schedule applying a payload to the store on the next `run()`.
144
159
  */
145
- commitPayload(
160
+ commitPayload<TMutation: MutationParameters>(
146
161
  operation: OperationDescriptor,
147
162
  payload: RelayResponsePayload,
148
- updater?: ?SelectorStoreUpdater,
163
+ updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
149
164
  ): void {
150
165
  this._pendingBackupRebase = true;
151
166
  this._pendingData.add({
@@ -252,7 +267,9 @@ class RelayPublishQueue implements PublishQueue {
252
267
  * _publishSourceFromPayload will return a boolean indicating if the
253
268
  * publish caused the store to be globally invalidated.
254
269
  */
255
- _publishSourceFromPayload(pendingPayload: PendingRelayPayload): boolean {
270
+ _publishSourceFromPayload<TMutation: MutationParameters>(
271
+ pendingPayload: PendingRelayPayload<TMutation>,
272
+ ): boolean {
256
273
  const {payload, operation, updater} = pendingPayload;
257
274
  const {source, fieldPayloads} = payload;
258
275
  const mutator = new RelayRecordSourceMutator(
@@ -354,7 +371,8 @@ class RelayPublishQueue implements PublishQueue {
354
371
  this._handlerProvider,
355
372
  );
356
373
 
357
- const processUpdate = optimisticUpdate => {
374
+ // $FlowFixMe[unclear-type] see explanation above.
375
+ const processUpdate = (optimisticUpdate: OptimisticUpdate<any>) => {
358
376
  if (optimisticUpdate.storeUpdater) {
359
377
  const {storeUpdater} = optimisticUpdate;
360
378
  applyWithGuard(
@@ -673,7 +673,9 @@ export interface IEnvironment {
673
673
  * Apply an optimistic mutation response and/or updater. The mutation can be
674
674
  * reverted by calling `dispose()` on the returned value.
675
675
  */
676
- applyMutation(optimisticConfig: OptimisticResponseConfig): Disposable;
676
+ applyMutation<TMutation: MutationParameters>(
677
+ optimisticConfig: OptimisticResponseConfig<TMutation>,
678
+ ): Disposable;
677
679
 
678
680
  /**
679
681
  * Commit an updater to the environment. This mutation cannot be reverted and
@@ -740,9 +742,9 @@ export interface IEnvironment {
740
742
  * Note: Observables are lazy, so calling this method will do nothing until
741
743
  * the result is subscribed to: environment.executeSubscription({...}).subscribe({...}).
742
744
  */
743
- executeSubscription(config: {|
745
+ executeSubscription<TMutation: MutationParameters>(config: {|
744
746
  operation: OperationDescriptor,
745
- updater?: ?SelectorStoreUpdater,
747
+ updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
746
748
  |}): RelayObservable<GraphQLResponse>;
747
749
 
748
750
  /**
@@ -755,8 +757,8 @@ export interface IEnvironment {
755
757
  * the result is subscribed to:
756
758
  * environment.executeMutation({...}).subscribe({...}).
757
759
  */
758
- executeMutation(
759
- config: ExecuteMutationConfig,
760
+ executeMutation<TMutation: MutationParameters>(
761
+ config: ExecuteMutationConfig<TMutation>,
760
762
  ): RelayObservable<GraphQLResponse>;
761
763
 
762
764
  /**
@@ -972,35 +974,33 @@ export type StoreUpdater = (store: RecordSourceProxy) => void;
972
974
  * order to easily access the root fields of a query/mutation as well as a
973
975
  * second argument of the response object of the mutation.
974
976
  */
975
- export type SelectorStoreUpdater = (
977
+ export type SelectorStoreUpdater<-TMutationResponse> = (
976
978
  store: RecordSourceSelectorProxy,
977
- // Actually SelectorData, but mixed is inconvenient to access deeply in
978
- // product code.
979
- data: $FlowFixMe,
979
+ data: ?TMutationResponse,
980
980
  ) => void;
981
981
 
982
982
  /**
983
983
  * A set of configs that can be used to apply an optimistic update into the
984
984
  * store.
985
985
  */
986
- export type OptimisticUpdate =
986
+ export type OptimisticUpdate<TMutation: MutationParameters> =
987
987
  | OptimisticUpdateFunction
988
- | OptimisticUpdateRelayPayload;
988
+ | OptimisticUpdateRelayPayload<TMutation>;
989
989
 
990
990
  export type OptimisticUpdateFunction = {|
991
991
  +storeUpdater: StoreUpdater,
992
992
  |};
993
993
 
994
- export type OptimisticUpdateRelayPayload = {|
994
+ export type OptimisticUpdateRelayPayload<TMutation: MutationParameters> = {|
995
995
  +operation: OperationDescriptor,
996
996
  +payload: RelayResponsePayload,
997
- +updater: ?SelectorStoreUpdater,
997
+ +updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
998
998
  |};
999
999
 
1000
- export type OptimisticResponseConfig = {|
1000
+ export type OptimisticResponseConfig<TMutation: MutationParameters> = {|
1001
1001
  +operation: OperationDescriptor,
1002
1002
  +response: ?PayloadData,
1003
- +updater: ?SelectorStoreUpdater,
1003
+ +updater: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
1004
1004
  |};
1005
1005
 
1006
1006
  /**
@@ -1068,11 +1068,13 @@ export type RelayResponsePayload = {|
1068
1068
  /**
1069
1069
  * Configuration on the executeMutation(...).
1070
1070
  */
1071
- export type ExecuteMutationConfig = {|
1071
+ export type ExecuteMutationConfig<TMutation: MutationParameters> = {|
1072
1072
  operation: OperationDescriptor,
1073
- optimisticUpdater?: ?SelectorStoreUpdater,
1073
+ optimisticUpdater?: ?SelectorStoreUpdater<
1074
+ $ElementType<TMutation, 'response'>,
1075
+ >,
1074
1076
  optimisticResponse?: ?Object,
1075
- updater?: ?SelectorStoreUpdater,
1077
+ updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
1076
1078
  uploadables?: ?UploadableMap,
1077
1079
  |};
1078
1080
 
@@ -1083,12 +1085,16 @@ export interface PublishQueue {
1083
1085
  /**
1084
1086
  * Schedule applying an optimistic updates on the next `run()`.
1085
1087
  */
1086
- applyUpdate(updater: OptimisticUpdate): void;
1088
+ applyUpdate<TMutation: MutationParameters>(
1089
+ updater: OptimisticUpdate<TMutation>,
1090
+ ): void;
1087
1091
 
1088
1092
  /**
1089
1093
  * Schedule reverting an optimistic updates on the next `run()`.
1090
1094
  */
1091
- revertUpdate(updater: OptimisticUpdate): void;
1095
+ revertUpdate<TMutation: MutationParameters>(
1096
+ updater: OptimisticUpdate<TMutation>,
1097
+ ): void;
1092
1098
 
1093
1099
  /**
1094
1100
  * Schedule a revert of all optimistic updates on the next `run()`.
@@ -1098,10 +1104,10 @@ export interface PublishQueue {
1098
1104
  /**
1099
1105
  * Schedule applying a payload to the store on the next `run()`.
1100
1106
  */
1101
- commitPayload(
1107
+ commitPayload<TMutation: MutationParameters>(
1102
1108
  operation: OperationDescriptor,
1103
1109
  payload: RelayResponsePayload,
1104
- updater?: ?SelectorStoreUpdater,
1110
+ updater?: ?SelectorStoreUpdater<$ElementType<TMutation, 'response'>>,
1105
1111
  ): void;
1106
1112
 
1107
1113
  /**
@@ -47,7 +47,7 @@ export type GraphQLSubscriptionConfig<T: SubscriptionParameters> = {|
47
47
  onCompleted?: ?() => void,
48
48
  onError?: ?(error: Error) => void,
49
49
  onNext?: ?(response: ?$ElementType<T, 'response'>) => void,
50
- updater?: ?SelectorStoreUpdater,
50
+ updater?: ?SelectorStoreUpdater<$ElementType<T, 'response'>>,
51
51
  |};
52
52
 
53
53
  export type DEPRECATED_GraphQLSubscriptionConfig<TSubscriptionPayload> = {|
@@ -58,7 +58,7 @@ export type DEPRECATED_GraphQLSubscriptionConfig<TSubscriptionPayload> = {|
58
58
  onCompleted?: ?() => void,
59
59
  onError?: ?(error: Error) => void,
60
60
  onNext?: ?(response: ?TSubscriptionPayload) => void,
61
- updater?: ?SelectorStoreUpdater,
61
+ updater?: ?SelectorStoreUpdater<TSubscriptionPayload>,
62
62
  |};
63
63
 
64
64
  function requestSubscription<TSubscriptionPayload>(