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

useLazyQuery cannot be prefetched on the server #1495

Closed
Csszabi98 opened this issue Aug 7, 2023 · 1 comment · Fixed by Shoutzor/frontend#141
Closed

useLazyQuery cannot be prefetched on the server #1495

Csszabi98 opened this issue Aug 7, 2023 · 1 comment · Fixed by Shoutzor/frontend#141

Comments

@Csszabi98
Copy link
Contributor

Describe the bug
As of now, useLazyQuery cannot be prefetched during ssr, as the "forceDisabled" value passed to the underlying implementation is always set to "true".

Expected behavior
useLazyQuery should accept a new parameter allowing consumers to decide if the query should be disabled out of the box.

Workaround
Call useQuery or useLazyQuery conditionally based on if the query should or should not be eager:

import { UseQueryOptions, useLazyQuery, useQuery } from '@vue/apollo-composable';
import type {
  DocumentParameter,
  OptionsParameter,
  UseQueryReturn,
  VariablesParameter,
} from '@vue/apollo-composable/dist/useQuery.js';
import { DocumentNode } from 'graphql';

type UseLazyQueryReturn<TResult, TVariables extends Record<string, unknown>> = UseQueryReturn<
  TResult,
  TVariables
> & {
  load: (
    document?: DocumentNode | null,
    variables?: TVariables | null,
    options?: UseQueryOptions | null,
  ) => boolean;
};

export function useLazyQuerySsrSafe<
  TResult = unknown,
  TVariables extends Record<string, unknown> = Record<string, unknown>,
>(
  document: DocumentParameter<TResult, TVariables>,
  variables?: VariablesParameter<TVariables>,
  options?: OptionsParameter<TResult, TVariables>,
  eager = false, // This line does the trick, enables prefetching for lazy queries
): UseLazyQueryReturn<TResult, TVariables> {
  if (eager) {
    const query = useQuery(
      document,
      variables as VariablesParameter<TVariables>,
      options as OptionsParameter<TResult, TVariables>,
    );

    return {
      ...query,
      // set to have a compatible api
      load() {
        return query.forceDisabled.value;
      },
    };
  }

  return useLazyQuery(document, variables, options);
}
@jarkt
Copy link

jarkt commented Jan 19, 2024

It's an architectural issue, because the functionality relies on nextTick, which of course is never called during SSR.

d1d8426#r137461373

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