Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab3b117

Browse files
tyao1facebook-github-bot
authored andcommitted
Pass operation availability to the network layer for loadQuery
Reviewed By: lynnshaoyuDifferential Revision: D70144365fbshipit-source-id: d97370b3087987baff2a59183dca613cc223ba59
1 parent687e996 commitab3b117

File tree

4 files changed

+120
-12
lines changed

4 files changed

+120
-12
lines changed

‎packages/react-relay/relay-hooks/__tests__/loadQuery-test.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
}from'./__generated__/loadQueryTestQuery.graphql';
1919
importtype{
2020
CacheConfig,
21+
INetwork,
2122
LogRequestInfoFunction,
2223
Query,
2324
RequestParameters,
@@ -96,6 +97,7 @@ describe('loadQuery', () => {
9697
let mockAvailability:{fetchTime?:number,status:string};
9798
let disposeOnloadCallback;
9899
let executeOnloadCallback;
100+
let checkOperation;
99101

100102
beforeEach(() =>{
101103
fetch=jest.fn(
@@ -122,7 +124,17 @@ describe('loadQuery', () => {
122124
returnobservable;
123125
},
124126
);
125-
environment=createMockEnvironment({network:Network.create(fetch)});
127+
functionwrapNetworkExecute(network:INetwork):INetwork{
128+
return{
129+
execute:(_1,_2,_3,_4,_5,_6,_7,_checkOperation)=>{
130+
checkOperation=_checkOperation;
131+
returnnetwork.execute(_1,_2,_3,_4,_5,_6,_7,_checkOperation);
132+
},
133+
};
134+
}
135+
environment=createMockEnvironment({
136+
network:wrapNetworkExecute(Network.create(fetch)),
137+
});
126138

127139
jest.clearAllTimers();
128140
jest.useFakeTimers();
@@ -397,6 +409,48 @@ describe('loadQuery', () => {
397409
expect(disposeEnvironmentRetain).toHaveBeenCalledTimes(1);
398410
});
399411
});
412+
413+
describe("with fetchPolicy === 'store-and-network'",()=>{
414+
it('should call fetch if the query can be fulfilled by the store',()=>{
415+
const{source}=loadQuery(
416+
environment,
417+
preloadableConcreteRequest,
418+
variables,
419+
{
420+
fetchPolicy:'store-and-network',
421+
},
422+
);
423+
expect(fetch).toHaveBeenCalled();
424+
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
425+
expect(environment.executeWithSource).toHaveBeenCalled();
426+
expect(source).toBeDefined();
427+
// Query should still be retained even if we don't fetch
428+
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
429+
expect(environment.retain).toHaveBeenCalled();
430+
});
431+
432+
it('returns the correct operation availability (available)',()=>{
433+
loadQuery(environment,preloadableConcreteRequest,variables,{
434+
fetchPolicy:'store-and-network',
435+
});
436+
expect(fetch).toHaveBeenCalled();
437+
expect(checkOperation!=null&&checkOperation().status).toEqual(
438+
'available',
439+
);
440+
});
441+
442+
it('returns the correct operation availability (missing)',()=>{
443+
mockAvailability={status:'missing'};
444+
445+
loadQuery(environment,preloadableConcreteRequest,variables,{
446+
fetchPolicy:'store-and-network',
447+
});
448+
expect(fetch).toHaveBeenCalled();
449+
expect(checkOperation!=null&&checkOperation().status).toEqual(
450+
'missing',
451+
);
452+
});
453+
});
400454
});
401455

402456
describe('when the query AST is unavailable synchronously',()=>{

‎packages/react-relay/relay-hooks/__tests__/preloadQuery_DEPRECATED-test.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
'use strict';
1313

14-
importtype{GraphQLResponse}from'relay-runtime/network/RelayNetworkTypes';
14+
importtype{
15+
GraphQLResponse,
16+
INetwork,
17+
}from'relay-runtime/network/RelayNetworkTypes';
1518

1619
constpreloadQuery_DEPRECATED=require('../preloadQuery_DEPRECATED');
1720
const{
@@ -76,19 +79,37 @@ describe.each(['RelayModernEnvironment', 'MultiActorEnvironment'])(
7679
letsink;
7780
letvariables;
7881
letoperation;
82+
letcheckOperation;
7983

8084
beforeEach(()=>{
8185
// $FlowFixMe[missing-local-annot] error found when enabling Flow LTI mode
82-
fetch=jest.fn((_query,_variables,_cacheConfig)=>{
86+
fetch=jest.fn((_query,_variables,_cacheConfig,_4,_5)=>{
8387
// $FlowFixMe[missing-local-annot] error found when enabling Flow LTI mode
8488
returnObservable.create(_sink=>{
8589
sink=_sink;
8690
});
8791
});
88-
92+
functionwrapNetworkExecute(network:INetwork):INetwork{
93+
return{
94+
execute:(_1,_2,_3,_4,_5,_6,_7,_checkOperation)=>{
95+
checkOperation=_checkOperation;
96+
returnnetwork.execute(
97+
_1,
98+
_2,
99+
_3,
100+
_4,
101+
_5,
102+
_6,
103+
_7,
104+
_checkOperation,
105+
);
106+
},
107+
};
108+
}
89109
constmultiActorEnvironment=newMultiActorEnvironment({
90110
// $FlowFixMe[invalid-tuple-arity] Error found while enabling LTI on this file
91-
createNetworkForActor:_actorID=>Network.create(fetch),
111+
createNetworkForActor:_actorID=>
112+
wrapNetworkExecute(Network.create(fetch)),
92113
createStoreForActor:_actorID=>
93114
newStore(newRecordSource(),{
94115
gcReleaseBufferSize:1,
@@ -99,7 +120,7 @@ describe.each(['RelayModernEnvironment', 'MultiActorEnvironment'])(
99120
?multiActorEnvironment.forActor(getActorIdentifier('actor:1234'))
100121
:newEnvironment({
101122
// $FlowFixMe[invalid-tuple-arity] Error found while enabling LTI on this file
102-
network:Network.create(fetch),
123+
network:wrapNetworkExecute(Network.create(fetch)),
103124
store:newStore(newRecordSource(),{
104125
gcReleaseBufferSize:1,
105126
}),
@@ -535,6 +556,10 @@ describe.each(['RelayModernEnvironment', 'MultiActorEnvironment'])(
535556
expect(fetch.mock.calls[0][0]).toBe(query.params);
536557
expect(fetch.mock.calls[0][1]).toEqual(variables);
537558
expect(fetch.mock.calls[0][2]).toEqual({force:true});
559+
expect(checkOperation&&checkOperation()).toEqual({
560+
status:'available',
561+
fetchTime,
562+
});
538563

539564
const[events,observer]=createObserver();
540565
if(preloaded.source){

‎packages/react-relay/relay-hooks/loadQuery.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
RequestIdentifier,
2929
RequestParameters,
3030
}from'relay-runtime';
31+
importtype{OperationAvailability}from'relay-runtime/store/RelayStoreTypes';
3132

3233
constinvariant=require('invariant');
3334
const{
@@ -130,6 +131,7 @@ function loadQuery<
130131
letdidMakeNetworkRequest=false;
131132
constmakeNetworkRequest=(
132133
params:RequestParameters,
134+
checkOperation?:()=>OperationAvailability,
133135
):Observable<GraphQLResponse>=>{
134136
// N.B. this function is called synchronously or not at all
135137
// didMakeNetworkRequest is safe to rely on in the returned value
@@ -160,7 +162,16 @@ function loadQuery<
160162
'raw-network-request-' + getRequestIdentifier(params, variables);
161163
constobservable=fetchQueryDeduped(environment,identifier,()=>{
162164
constnetwork=environment.getNetwork();
163-
returnnetwork.execute(params,variables,networkCacheConfig);
165+
returnnetwork.execute(
166+
params,
167+
variables,
168+
networkCacheConfig,
169+
undefined,
170+
undefined,
171+
undefined,
172+
undefined,
173+
checkOperation,
174+
);
164175
});
165176

166177
const{unsubscribe}=observable.subscribe({
@@ -245,15 +256,19 @@ function loadQuery<
245256
// N.B. If the fetch policy allows fulfillment from the store but the
246257
// environment already has the data for that operation cached in the store,
247258
// then we do nothing.
259+
constoperationAvailability=environment.check(operation);
248260
constshouldFetch=
249261
fetchPolicy!=='store-or-network'||
250-
environment.check(operation).status!=='available';
262+
operationAvailability.status!=='available';
251263

252264
if(shouldFetch){
253265
executeDeduped(operation,()=>{
254266
// N.B. Since we have the operation synchronously available here,
255267
// we can immediately fetch and execute the operation.
256-
constnetworkObservable=makeNetworkRequest(concreteRequest.params);
268+
constnetworkObservable=makeNetworkRequest(
269+
concreteRequest.params,
270+
()=>operationAvailability,
271+
);
257272
constexecuteObservable=executeWithNetworkSource(
258273
operation,
259274
networkObservable,

‎packages/react-relay/relay-hooks/preloadQuery_DEPRECATED.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,17 @@ function preloadQueryDeduped<TQuery: OperationType>(
167167
}`;
168168
const prevQueryEntry = pendingQueries.get(cacheKey);
169169

170-
const availability =
171-
fetchPolicy === STORE_OR_NETWORK_DEFAULT&&query!=null&&query!=null
170+
function checkOperation(){
171+
returnquery!=null
172172
?environment.check(
173173
createOperationDescriptor(query,variables,networkCacheConfig),
174174
)
175175
:{status:'missing'};
176+
}
177+
const availability =
178+
fetchPolicy === STORE_OR_NETWORK_DEFAULT
179+
? checkOperation()
180+
:{status:'missing'};
176181

177182
let nextQueryEntry: ?PendingQueryEntry;
178183
if (availability.status === 'available'&&query!=null){
@@ -203,7 +208,16 @@ function preloadQueryDeduped<TQuery: OperationType>(
203208
}
204209
}elseif(prevQueryEntry==null||prevQueryEntry.kind!== 'network'){
205210
// Should fetch but we're not already fetching: fetch!
206-
constsource=network.execute(params,variables,networkCacheConfig,null);
211+
constsource=network.execute(
212+
params,
213+
variables,
214+
networkCacheConfig,
215+
null,
216+
undefined,
217+
undefined,
218+
undefined,
219+
checkOperation,
220+
);
207221
constsubject=newReplaySubject<GraphQLResponse>();
208222
nextQueryEntry={
209223
cacheKey,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp