Skip to content

Instantly share code, notes, and snippets.

@robrichard
Last active May 19, 2023 14:25
Show Gist options
  • Save robrichard/a402759733366ead3854a01aef8685b1 to your computer and use it in GitHub Desktop.
Save robrichard/a402759733366ead3854a01aef8685b1 to your computer and use it in GitHub Desktop.
React Server Component Relay Example
import { renderToPipeableStream } from 'react-server-dom-webpack/server.node';
import { runWithAsyncLocalStorageRelay } from './runWithAsyncLocalStorageRelay';
function handler(req, res, next) {
const relayEnvironment = createRelayEnvironment(req, res);
const { Component, props } = getComponent(req);
runWithAsyncLocalStorageRelay(relayEnvironment, () => {
const { pipe } = renderToPipeableStream(
<Component {...props} />,
clientManifest
);
pipe(res);
});
}
import { Environment } from 'relay-runtime';
import { AsyncLocalStorage } from 'node:async_hooks';
export const relayLocalStorage = new AsyncLocalStorage<Environment>();
import { fetchQuery, OperationType, GraphQLTaggedNode } from 'relay-runtime';
import { relayLocalStorage } from './relayAsyncLocalStorage';
export function rscFetchQuery<T extends OperationType>(
taggedNode: GraphQLTaggedNode,
variables: T['variables']
): Promise<T['response']> {
const environment = relayLocalStorage.getStore();
if (!environment) {
throw new Error('Must be run inside `runWithAsyncLocalStorageRelay` from `dibs-rsc-relay`');
}
return fetchQuery(environment, taggedNode, variables).toPromise();
}
import {
getSelector,
getFragment,
GraphQLTaggedNode,
PluralReaderSelector,
ReaderSelector,
} from 'relay-runtime';
import { relayLocalStorage } from './relayAsyncLocalStorage';
import type {
KeyType,
KeyTypeData,
ArrayKeyType,
ArrayKeyTypeData,
} from 'react-relay/relay-hooks/helpers';
function readerSelectorIsPlural(selector: ReaderSelector): selector is PluralReaderSelector {
return selector.kind === 'PluralReaderSelector';
}
function rscReadFragment<TKey extends KeyType>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey
): KeyTypeData<TKey>;
function rscReadFragment<TKey extends KeyType>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey | null
): KeyTypeData<TKey> | null;
function rscReadFragment<TKey extends ArrayKeyType>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey
): ArrayKeyTypeData<TKey>;
function rscReadFragment<TKey extends ArrayKeyType>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey | null
): ArrayKeyTypeData<TKey> | null;
function rscReadFragment<TKey extends ArrayKeyType>(
fragmentInput: GraphQLTaggedNode,
fragmentRef: TKey | null
): ArrayKeyTypeData<TKey> | null {
if (!fragmentRef) {
return null;
}
const environment = relayLocalStorage.getStore();
if (!environment) {
throw new Error('Must be run inside `runWithAsyncLocalStorageRelay` from `dibs-rsc-relay`');
}
const fragmentSelector = getSelector(getFragment(fragmentInput), fragmentRef);
if (readerSelectorIsPlural(fragmentSelector)) {
return fragmentSelector.selectors.map(s => environment.lookup(s));
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return environment.lookup(fragmentSelector as any).data as any;
}
}
export { rscReadFragment };
import { Environment } from 'relay-runtime';
import { relayLocalStorage } from './relayAsyncLocalStorage';
export function runWithAsyncLocalStorageRelay(environment: Environment, cb: () => void): void {
relayLocalStorage.run(environment, cb);
}
@robrichard
Copy link
Author

This allows fetching data and reading fragments with Relay in React Server Components. It does not attempt to allow passing fragment references between server and client components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment