Skip to content

Commit 288aebd

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Make store an optional argument when constructing a Relay Environment
Reviewed By: lynnshaoyu Differential Revision: D73151474 fbshipit-source-id: d548f75298d34e20fc598ec0f64f8ed4d2cf6c70
1 parent 63c8437 commit 288aebd

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

packages/relay-runtime/store/RelayModernEnvironment.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const defaultGetDataID = require('./defaultGetDataID');
5858
const defaultRelayFieldLogger = require('./defaultRelayFieldLogger');
5959
const normalizeResponse = require('./normalizeResponse');
6060
const OperationExecutor = require('./OperationExecutor');
61+
const RelayModernStore = require('./RelayModernStore');
6162
const RelayPublishQueue = require('./RelayPublishQueue');
6263
const RelayRecordSource = require('./RelayRecordSource');
6364
const invariant = require('invariant');
@@ -71,7 +72,7 @@ export type EnvironmentConfig = {
7172
+network: INetwork,
7273
+normalizeResponse?: ?NormalizeResponseFunction,
7374
+scheduler?: ?TaskScheduler,
74-
+store: Store,
75+
+store?: Store,
7576
+missingFieldHandlers?: ?$ReadOnlyArray<MissingFieldHandler>,
7677
+operationTracker?: ?OperationTracker,
7778
+getDataID?: ?GetDataID,
@@ -118,6 +119,15 @@ class RelayModernEnvironment implements IEnvironment {
118119
);
119120
}
120121
}
122+
const store =
123+
config.store ??
124+
new RelayModernStore(new RelayRecordSource(), {
125+
log: config.log,
126+
operationLoader: config.operationLoader,
127+
getDataID: config.getDataID,
128+
shouldProcessClientComponents: config.shouldProcessClientComponents,
129+
});
130+
121131
this.__log = config.log ?? emptyFunction;
122132
this.relayFieldLogger = config.relayFieldLogger ?? defaultRelayFieldLogger;
123133
this._defaultRenderPolicy =
@@ -128,14 +138,14 @@ class RelayModernEnvironment implements IEnvironment {
128138
this._getDataID = config.getDataID ?? defaultGetDataID;
129139
this._missingFieldHandlers = config.missingFieldHandlers ?? [];
130140
this._publishQueue = new RelayPublishQueue(
131-
config.store,
141+
store,
132142
config.handlerProvider ?? RelayDefaultHandlerProvider,
133143
this._getDataID,
134144
this._missingFieldHandlers,
135145
this.__log,
136146
);
137147
this._scheduler = config.scheduler ?? null;
138-
this._store = config.store;
148+
this._store = store;
139149
this.options = config.options;
140150
this._isServer = config.isServer ?? false;
141151
this._normalizeResponse = config.normalizeResponse ?? normalizeResponse;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall relay
10+
*/
11+
12+
const RelayNetwork = require('../../network/RelayNetwork');
13+
const RelayModernEnvironment = require('../RelayModernEnvironment');
14+
const RelayModernStore = require('../RelayModernStore');
15+
16+
describe('new Environment()', () => {
17+
it('creates a properly configured Relay Store if one is not provided', () => {
18+
const log = () => {};
19+
const operationLoader = {
20+
get: jest.fn(),
21+
load: jest.fn(),
22+
};
23+
const getDataID = () => 'lol';
24+
const shouldProcessClientComponents = true;
25+
const network = RelayNetwork.create(jest.fn());
26+
const environment = new RelayModernEnvironment({
27+
network,
28+
log,
29+
operationLoader,
30+
getDataID,
31+
shouldProcessClientComponents,
32+
});
33+
34+
const store = environment.getStore();
35+
36+
if (!(store instanceof RelayModernStore)) {
37+
throw new Error('Expected store to be an instance of RelayModernStore');
38+
}
39+
expect(store.__log).toBe(log);
40+
});
41+
});

0 commit comments

Comments
 (0)