Skip to content

Commit

Permalink
start fixing some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Markionium committed Jul 23, 2024
1 parent 8c31211 commit eefbfbc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 52 deletions.
11 changes: 1 addition & 10 deletions packages/relay-runtime/store/RelayModernStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
OperationLoader,
RecordSource,
RequestDescriptor,
ResolverContext,
Scheduler,
SingularReaderSelector,
Snapshot,
Expand Down Expand Up @@ -106,7 +105,6 @@ class RelayModernStore implements Store {
_storeSubscriptions: StoreSubscriptions;
_updatedRecordIDs: DataIDSet;
_shouldProcessClientComponents: ?boolean;
_resolverContext: ?ResolverContext;

constructor(
source: MutableRecordSource,
Expand All @@ -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.
Expand Down Expand Up @@ -159,7 +156,6 @@ class RelayModernStore implements Store {
this._updatedRecordIDs = new Set();
this._shouldProcessClientComponents =
options?.shouldProcessClientComponents;
this._resolverContext = options?.resolverContext;

initializeRecordSource(this._recordSource);
}
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 5 additions & 17 deletions packages/relay-runtime/store/RelayReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,36 +706,22 @@ class RelayReader {
: {},
},
};
const resolverContext: ResolverContext<mixed> = {
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<mixed> = {
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};
}
Expand Down Expand Up @@ -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 =
Expand All @@ -1447,6 +1434,7 @@ function getResolverValue(
: undefined;

resolverFunctionArgs.push(args);
resolverFunctionArgs.push(resolverContext);

resolverResult = resolverFunction.apply(null, resolverFunctionArgs);
} catch (e) {
Expand Down
28 changes: 3 additions & 25 deletions packages/relay-runtime/store/ResolverFragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
type ResolverContext = {
getDataForResolverFragment: (
SingularReaderSelector,
FragmentType,
) => ResolverFragmentResult,
resolverContext: T,
};
// $FlowFixMe[unclear-type]
const contextStack: Array<ResolverContext<any>> = [];
const contextStack: Array<ResolverContext> = [];

function withResolverContext<T, D = mixed>(
context: ResolverContext<D>,
cb: () => T,
): T {
function withResolverContext<T>(context: ResolverContext, cb: () => T): T {
contextStack.push(context);
try {
return cb();
Expand Down Expand Up @@ -128,27 +123,10 @@ function readFragment(
return data;
}

function resolverContext<T = mixed>(): 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,
};

0 comments on commit eefbfbc

Please sign in to comment.