From eefbfbc1728a45cf1b63e1cbb7f2c19777e51f20 Mon Sep 17 00:00:00 2001 From: Mark Polak Date: Mon, 22 Jul 2024 19:11:18 +0200 Subject: [PATCH] start fixing some stuff --- .../relay-runtime/store/RelayModernStore.js | 11 +------- packages/relay-runtime/store/RelayReader.js | 22 ++++----------- .../relay-runtime/store/ResolverFragments.js | 28 ++----------------- 3 files changed, 9 insertions(+), 52 deletions(-) diff --git a/packages/relay-runtime/store/RelayModernStore.js b/packages/relay-runtime/store/RelayModernStore.js index 6dc6cacb15880..dde64ef45e17b 100644 --- a/packages/relay-runtime/store/RelayModernStore.js +++ b/packages/relay-runtime/store/RelayModernStore.js @@ -24,7 +24,6 @@ import type { OperationLoader, RecordSource, RequestDescriptor, - ResolverContext, Scheduler, SingularReaderSelector, Snapshot, @@ -106,7 +105,6 @@ class RelayModernStore implements Store { _storeSubscriptions: StoreSubscriptions; _updatedRecordIDs: DataIDSet; _shouldProcessClientComponents: ?boolean; - _resolverContext: ?ResolverContext; constructor( source: MutableRecordSource, @@ -118,7 +116,6 @@ class RelayModernStore implements Store { gcReleaseBufferSize?: ?number, queryCacheExpirationTime?: ?number, shouldProcessClientComponents?: ?boolean, - resolverContext?: ResolverContext, }, ) { // Prevent mutation of a record from outside the store. @@ -159,7 +156,6 @@ class RelayModernStore implements Store { this._updatedRecordIDs = new Set(); this._shouldProcessClientComponents = options?.shouldProcessClientComponents; - this._resolverContext = options?.resolverContext; initializeRecordSource(this._recordSource); } @@ -300,12 +296,7 @@ class RelayModernStore implements Store { lookup(selector: SingularReaderSelector): Snapshot { const source = this.getSource(); - const snapshot = RelayReader.read( - source, - selector, - this._resolverCache, - this._resolverContext, - ); + const snapshot = RelayReader.read(source, selector, this._resolverCache); if (__DEV__) { deepFreeze(snapshot); } diff --git a/packages/relay-runtime/store/RelayReader.js b/packages/relay-runtime/store/RelayReader.js index 99122693fb843..9bc4975898596 100644 --- a/packages/relay-runtime/store/RelayReader.js +++ b/packages/relay-runtime/store/RelayReader.js @@ -706,36 +706,22 @@ class RelayReader { : {}, }, }; - const resolverContext: ResolverContext = { - getDataForResolverFragment, - resolverContext: this._resolverContext, - }; + const resolverContext = {getDataForResolverFragment}; return withResolverContext(resolverContext, () => { const [resolverResult, resolverError] = getResolverValue( field, this._variables, key, + this._resolverContext, ); return {resolverResult, snapshot, error: resolverError}; }); - } else if (this._resolverContext !== undefined) { - const resolverContext: ResolverContext = { - resolverContext: this._resolverContext, - getDataForResolverFragment, - }; - return withResolverContext(resolverContext, () => { - const [resolverResult, resolverError] = getResolverValue( - field, - this._variables, - null, - ); - return {resolverResult, snapshot: undefined, error: resolverError}; - }); } else { const [resolverResult, resolverError] = getResolverValue( field, this._variables, null, + this._resolverContext, ); return {resolverResult, snapshot: undefined, error: resolverError}; } @@ -1428,6 +1414,7 @@ function getResolverValue( field: ReaderRelayResolver | ReaderRelayLiveResolver, variables: Variables, fragmentKey: mixed, + resolverContext: mixed, ): [mixed, ?Error] { // Support for languages that work (best) with ES6 modules, such as TypeScript. const resolverFunction = @@ -1447,6 +1434,7 @@ function getResolverValue( : undefined; resolverFunctionArgs.push(args); + resolverFunctionArgs.push(resolverContext); resolverResult = resolverFunction.apply(null, resolverFunctionArgs); } catch (e) { diff --git a/packages/relay-runtime/store/ResolverFragments.js b/packages/relay-runtime/store/ResolverFragments.js index f16b7200db8a3..78833ac240a12 100644 --- a/packages/relay-runtime/store/ResolverFragments.js +++ b/packages/relay-runtime/store/ResolverFragments.js @@ -24,20 +24,15 @@ const invariant = require('invariant'); // `readFragment`, but that's a global function -- it needs information // about what resolver is being executed, which is supplied by putting the // info on this stack before we call the resolver function. -export type ResolverContext = { +type ResolverContext = { getDataForResolverFragment: ( SingularReaderSelector, FragmentType, ) => ResolverFragmentResult, - resolverContext: T, }; -// $FlowFixMe[unclear-type] -const contextStack: Array> = []; +const contextStack: Array = []; -function withResolverContext( - context: ResolverContext, - cb: () => T, -): T { +function withResolverContext(context: ResolverContext, cb: () => T): T { contextStack.push(context); try { return cb(); @@ -128,27 +123,10 @@ function readFragment( return data; } -function resolverContext(): T { - if (!contextStack.length) { - throw new Error( - 'resolverContext should be called only from within a Relay Resolver function.', - ); - } - const context = contextStack[contextStack.length - 1]; - - invariant( - context.resolverContext !== undefined, - `Expected resolverContext to be defined, but it was not. Make sure to provide the resolverContext when creating the Store`, - ); - - return context.resolverContext; -} - const RESOLVER_FRAGMENT_MISSING_DATA_SENTINEL: mixed = {}; module.exports = { readFragment, - resolverContext, withResolverContext, RESOLVER_FRAGMENT_MISSING_DATA_SENTINEL, };