Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serverSidePostQuery hook to access query data + NextContext during SSR #74

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/wired/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'react-relay/hooks';
import {
Environment,
GraphQLResponse,
GraphQLTaggedNode,
OperationType,
RelayFeatureFlags,
Expand Down Expand Up @@ -60,6 +61,10 @@ export interface WiredOptions<Props extends WiredProps, ServerSideProps = {}> {
ctx: NextPageContext
) => Promise<OrRedirect<ServerSideProps>>;
fetchPolicy?: PreloadFetchPolicy;
serverSidePostQuery?: (
queryResult: GraphQLResponse | undefined,
ctx: NextPageContext
) => Promise<unknown> | unknown;
}

function defaultVariablesFromContext(
Expand Down Expand Up @@ -145,7 +150,10 @@ async function getServerInitialProps<Props extends WiredProps, ServerSideProps>(
query: GraphQLTaggedNode,
opts: WiredOptions<Props, ServerSideProps>
) {
const { variablesFromContext = defaultVariablesFromContext } = opts;
const {
variablesFromContext = defaultVariablesFromContext,
serverSidePostQuery,
} = opts;
const serverSideProps = opts.serverSideProps
? await opts.serverSideProps(ctx)
: ({} as ServerSideProps);
Expand Down Expand Up @@ -175,6 +183,11 @@ async function getServerInitialProps<Props extends WiredProps, ServerSideProps>(

await ensureQueryFlushed(preloadedQuery);

if (serverSidePostQuery) {
const queryResult = await preloadedQuery.source?.toPromise();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause a double network request? I'm not sure if the observables returned from Relay are hot or cold.

Copy link
Contributor Author

@FINDarkside FINDarkside Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, call to preloadedQuery.source?.toPromise() here results in a already resolved promise. If you have better name suggestion for serverSidePostQuery let me know 😄

await serverSidePostQuery(queryResult, ctx);
}

const context = createWiredServerContext({
variables,
query,
Expand Down
4 changes: 4 additions & 0 deletions website/docs/page-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const options: RelayOptions<{ token: string }> = {
- `variablesFromContext?`: Function that extracts GraphQL query variables from
`NextPageContext`. Run on both the client and server. If omitted query
variables are set to `ctx.query`.
- `serverSidePostQuery?`: Function that is called during server side rendering after
fetching the query is finished. First parameter gives you access to the data returned by your
query and the second parameter gives access to `NextPageContext`. This function can
be used for example to set your response status to 404 if your query didn't return data.

## `getRelaySerializedState`

Expand Down