Skip to content

Commit

Permalink
Rework resolvercontext based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Markionium committed Jul 31, 2024
1 parent eefbfbc commit 62a5c7f
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 219 deletions.
51 changes: 0 additions & 51 deletions packages/react-relay/__tests__/ClientOnlyQueries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,57 +252,6 @@ test('hello-world query', () => {
expect(renderer?.toJSON()).toBe('Hello, World!');
});

test('hello-world context query', () => {
const environment = new Environment({
network: Network.create(() => {
const error = new Error('Unexpected Network Error');
throw error;
}),
store: new Store(RecordSource.create(), {
resolverContext: {world: 'World'},
}),
});

function InnerTestComponent() {
const data = useClientQuery(
graphql`
query ClientOnlyQueriesTestContextQuery {
hello_context
}
`,
{},
);
return data.hello_context ?? 'MISSING';
}

function TestComponent({
environment: relayEnvironment,
...rest
}: {
environment: IEnvironment,
fetchPolicy?: FetchPolicy,
}) {
return (
<RelayEnvironmentProvider environment={relayEnvironment}>
<React.Suspense fallback="Loading...">
<InnerTestComponent {...rest} />
</React.Suspense>
</RelayEnvironmentProvider>
);
}
let renderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<TestComponent
environment={environment}
fetchPolicy="store-or-network"
/>,
);
});

expect(renderer?.toJSON()).toBe('Hello, World!');
});

test('hello user query with client-edge query', () => {
const environment = createEnvironment(
RecordSource.create({
Expand Down
6 changes: 3 additions & 3 deletions packages/react-relay/__tests__/LiveResolvers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ test('allows dependencies to be provided through the store', () => {
network: RelayNetwork.create(jest.fn()),
store: new LiveResolverStore(RelayRecordSource.create(), {
gcReleaseBufferSize: 0,
resolverContext: {
liveResolverContext: {
greeting: 'Hello Allemaal!',
},
}),
Expand All @@ -1750,7 +1750,7 @@ test('allows objects to be provided to be provided through the store', () => {
network: RelayNetwork.create(jest.fn()),
store: new LiveResolverStore(RelayRecordSource.create(), {
gcReleaseBufferSize: 0,
resolverContext: {
liveResolverContext: {
greeting: {
myHello: 'Hello Allemaal!',
},
Expand Down Expand Up @@ -1791,7 +1791,7 @@ test('ResolverContext can contain observable values', async () => {
const operation = createOperationDescriptor(FooQuery, {});
const store = new LiveResolverStore(source, {
gcReleaseBufferSize: 0,
resolverContext: {
liveResolverContext: {
counter: Observable.create<number>(observer => {
next = (value: number) => observer.next(value);
}),
Expand Down

This file was deleted.

32 changes: 22 additions & 10 deletions packages/relay-runtime/store/RelayReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
ClientEdgeTraversalInfo,
DataIDSet,
ErrorResponseFields,
LiveResolverContext,
MissingClientEdgeRequestInfo,
MissingLiveResolverField,
MissingRequiredFields,
Expand All @@ -47,7 +48,6 @@ import type {
} from './RelayStoreTypes';
import type {Arguments} from './RelayStoreUtils';
import type {EvaluationResult, ResolverCache} from './ResolverCache';
import type {ResolverContext} from './ResolverFragments';

const {
ACTOR_CHANGE,
Expand Down Expand Up @@ -102,13 +102,13 @@ function read(
recordSource: RecordSource,
selector: SingularReaderSelector,
resolverCache?: ResolverCache,
resolverContext?: mixed,
liveResolverContext?: LiveResolverContext,
): Snapshot {
const reader = new RelayReader(
recordSource,
selector,
resolverCache ?? new NoopResolverCache(),
resolverContext,
liveResolverContext,
);
return reader.read();
}
Expand All @@ -133,13 +133,13 @@ class RelayReader {
_resolverCache: ResolverCache;
_resolverErrors: RelayResolverErrors;
_fragmentName: string;
_resolverContext: mixed;
_liveResolverContext: ?LiveResolverContext;

constructor(
recordSource: RecordSource,
selector: SingularReaderSelector,
resolverCache: ResolverCache,
resolverContext: mixed,
liveResolverContext: ?LiveResolverContext,
) {
this._clientEdgeTraversalPath = selector.clientEdgeTraversalPath?.length
? [...selector.clientEdgeTraversalPath]
Expand All @@ -159,7 +159,7 @@ class RelayReader {
this._resolverErrors = [];
this._fragmentName = selector.node.name;
this._updatedDataIDs = new Set();
this._resolverContext = resolverContext;
this._liveResolverContext = liveResolverContext;
}

read(): Snapshot {
Expand Down Expand Up @@ -712,7 +712,7 @@ class RelayReader {
field,
this._variables,
key,
this._resolverContext,
this._liveResolverContext,
);
return {resolverResult, snapshot, error: resolverError};
});
Expand All @@ -721,7 +721,7 @@ class RelayReader {
field,
this._variables,
null,
this._resolverContext,
this._liveResolverContext,
);
return {resolverResult, snapshot: undefined, error: resolverError};
}
Expand Down Expand Up @@ -1414,7 +1414,7 @@ function getResolverValue(
field: ReaderRelayResolver | ReaderRelayLiveResolver,
variables: Variables,
fragmentKey: mixed,
resolverContext: mixed,
liveResolverContext: ?LiveResolverContext,
): [mixed, ?Error] {
// Support for languages that work (best) with ES6 modules, such as TypeScript.
const resolverFunction =
Expand All @@ -1424,17 +1424,29 @@ function getResolverValue(

let resolverResult = null;
let resolverError = null;

const shouldPassContextToLiveResolver =
liveResolverContext && field.kind === 'RelayLiveResolver';
try {
const resolverFunctionArgs = [];
if (field.fragment != null) {
resolverFunctionArgs.push(fragmentKey);
} else {
// Set first argument to `null` in case we have resolver context
// to make sure the context object is always the 3rd argument.
if (shouldPassContextToLiveResolver) {
resolverFunctionArgs.push(null);
}
}
const args = field.args
? getArgumentValues(field.args, variables)
: undefined;

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

if (shouldPassContextToLiveResolver) {
resolverFunctionArgs.push(liveResolverContext);
}

resolverResult = resolverFunction.apply(null, resolverFunctionArgs);
} catch (e) {
Expand Down
12 changes: 6 additions & 6 deletions packages/relay-runtime/store/RelayStoreSubscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import type {Disposable} from '../util/RelayRuntimeTypes';
import type {
DataIDSet,
LiveResolverContext,
LogFunction,
OperationDescriptor,
RecordSource,
RequestDescriptor,
ResolverContext,
Snapshot,
StoreSubscriptions,
} from './RelayStoreTypes';
Expand All @@ -42,17 +42,17 @@ class RelayStoreSubscriptions implements StoreSubscriptions {
_subscriptions: Set<Subscription>;
__log: ?LogFunction;
_resolverCache: ResolverCache;
_resolverContext: ?ResolverContext;
_liveResolverContext: ?LiveResolverContext;

constructor(
log?: ?LogFunction,
resolverCache: ResolverCache,
resolverContext?: ResolverContext,
liveResolverContext?: LiveResolverContext,
) {
this._subscriptions = new Set();
this.__log = log;
this._resolverCache = resolverCache;
this._resolverContext = resolverContext;
this._liveResolverContext = liveResolverContext;
}

subscribe(
Expand Down Expand Up @@ -90,7 +90,7 @@ class RelayStoreSubscriptions implements StoreSubscriptions {
source,
snapshot.selector,
this._resolverCache,
this._resolverContext,
this._liveResolverContext,
);
const nextData = recycleNodesInto(snapshot.data, backup.data);
(backup: $FlowFixMe).data = nextData; // backup owns the snapshot and can safely mutate
Expand Down Expand Up @@ -176,7 +176,7 @@ class RelayStoreSubscriptions implements StoreSubscriptions {
source,
snapshot.selector,
this._resolverCache,
this._resolverContext,
this._liveResolverContext,
)
: backup;
const nextData = recycleNodesInto(snapshot.data, nextSnapshot.data);
Expand Down
7 changes: 6 additions & 1 deletion packages/relay-runtime/store/RelayStoreTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,11 @@ export type LiveState<+T> = {
subscribe(cb: () => void): () => void,
};

export type ResolverContext = {
/**
* Context that will be provided to live resolvers if
* `liveResolverContext` is set on the Relay Store.
* This context will be passed as the third argument to the live resolver
*/
export type LiveResolverContext = {
[key: string]: mixed,
};

This file was deleted.

Loading

0 comments on commit 62a5c7f

Please sign in to comment.