Skip to content

Commit

Permalink
fix executing standby queries in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
atarek12 committed Feb 4, 2022
1 parent 108d652 commit 922ac0e
Showing 1 changed file with 49 additions and 30 deletions.
79 changes: 49 additions & 30 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { equal } from '@wry/equality';
import { OperationVariables, mergeOptions } from '../../core';
import { getApolloContext } from '../context';
import { ApolloContextValue, getApolloContext } from '../context';
import { ApolloError } from '../../errors';
import {
ApolloQueryResult,
Expand Down Expand Up @@ -53,37 +53,13 @@ export function useQuery<

if (
context.renderPromises &&
options?.fetchPolicy !== "standby" &&
options?.ssr !== false &&
!options?.skip &&
obsQuery.getCurrentResult().loading
) {
// TODO: This is a legacy API which could probably be cleaned up
context.renderPromises.addQueryPromise(
{
// The only options which seem to actually be used by the
// RenderPromises class are query and variables.
getOptions: () => createWatchQueryOptions(query, options, defaultWatchQueryOptions),
fetchData: () => new Promise<void>((resolve) => {
const sub = obsQuery!.subscribe({
next(result) {
if (!result.loading) {
resolve()
sub.unsubscribe();
}
},
error() {
resolve();
sub.unsubscribe();
},
complete() {
resolve();
},
});
}),
},
// This callback never seemed to do anything
() => null,
);
addSSRQueryPromise(context, watchQueryOptions, obsQuery)
}

return obsQuery;
Expand Down Expand Up @@ -244,15 +220,19 @@ export function useQuery<
obsQuery.refetch();
}

// TODO: This is a hack to make sure useLazyQuery executions update the
// obsevable query options for ssr.
// TODO: This is a hack to make sure useLazyQuery executions update the
// observable query options for ssr.
if (
context.renderPromises &&
options?.ssr !== false &&
!options?.skip &&
result.loading
) {
obsQuery.setOptions(createWatchQueryOptions(query, options, defaultWatchQueryOptions)).catch(() => {});
const watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);
if (!equal(ref.current.watchQueryOptions, watchQueryOptions)) {
obsQuery.setOptions(watchQueryOptions).catch(() => {});
addSSRQueryPromise(context, watchQueryOptions, obsQuery)
}
}

// We assign options during rendering as a guard to make sure that
Expand Down Expand Up @@ -370,3 +350,42 @@ function createWatchQueryOptions<TData, TVariables>(

return watchQueryOptions;
}

/**
* Function to add query promise in ssr
*/

function addSSRQueryPromise<TData, TVariables>(
context: ApolloContextValue,
options: QueryHookOptions<TData, TVariables> = {},
obsQuery: ObservableQuery<TData, TVariables>
): void {
if(!context.renderPromises) return;
context.renderPromises.addQueryPromise(
{
// The only options which seem to actually be used by the
// RenderPromises class are query and variables.
getOptions: () => options,
fetchData: () =>
new Promise<void>((resolve) => {
const sub = obsQuery!.subscribe({
next(result) {
if (!result.loading) {
resolve();
sub.unsubscribe();
}
},
error() {
resolve();
sub.unsubscribe();
},
complete() {
resolve();
},
});
}),
},
// This callback never seemed to do anything
() => null
);
}

0 comments on commit 922ac0e

Please sign in to comment.