<commit_before>import { equal } from '@wry/equality';

import { ApolloError } from '../../errors';

import {
  ApolloClient,
  NetworkStatus,
  FetchMoreQueryOptions,
  SubscribeToMoreOptions,
  ObservableQuery,
  FetchMoreOptions,
  UpdateQueryOptions,
  DocumentNode,
  TypedDocumentNode,
} from '../../core';

import {
  ObservableSubscription
} from '../../utilities';

import { DocumentType } from '../parser';
import {
  QueryResult,
  QueryDataOptions,
  QueryTuple,
  QueryLazyOptions,
  ObservableQueryFields,
} from '../types/types';
import { OperationData } from './OperationData';

type ObservableQueryOptions<TData, TVars> =
  ReturnType<QueryData<TData, TVars>["prepareObservableQueryOptions"]>;

export class QueryData<TData, TVariables> extends OperationData<
  QueryDataOptions<TData, TVariables>
> {
  public onNewData: () => void;
  private currentObservable?: ObservableQuery<TData, TVariables>;
  private currentSubscription?: ObservableSubscription;
  private runLazy: boolean = false;
  private lazyOptions?: QueryLazyOptions<TVariables>;
  private previous: {
    client?: ApolloClient<object>;
    query?: DocumentNode | TypedDocumentNode<TData, TVariables>;
    observableQueryOptions?: ObservableQueryOptions<TData, TVariables>;
    result?: QueryResult<TData, TVariables>;
    loading?: boolean;
    options?: QueryDataOptions<TData, TVariables>;
    error?: ApolloError;
  } = Object.create(null);

  constructor({
    options,
    context,
    onNewData
  }: {
    options: QueryDataOptions<TData, TVariables>;
    context: any;
    onNewData: () => void;
  }) {
    super(options, context);
    this.onNewData = onNewData;
  }

  public execute(): QueryResult<TData, TVariables> {
    this.refreshClient();

    const { skip, query } = this.getOptions();
    if (skip || query !== this.previous.query) {
      this.removeQuerySubscription();
      this.removeObservable(!skip);
      this.previous.query = query;
    }

    this.updateObservableQuery();

    return this.getExecuteSsrResult() || this.getExecuteResult();
  }

  public executeLazy(): QueryTuple<TData, TVariables> {
    return !this.runLazy
      ? [
          this.runLazyQuery,
          {
            loading: false,
            networkStatus: NetworkStatus.ready,
            called: false,
            data: undefined
          }
        ]
      : [this.runLazyQuery, this.execute()];
  }

  // For server-side rendering
  public fetchData(): Promise<void> | boolean {
    const options = this.getOptions();
    if (options.skip || options.ssr === false) return false;
    return new Promise(resolve => this.startQuerySubscription(resolve));
  }

  public afterExecute({ lazy = false }: { lazy?: boolean } = {}) {
    this.isMounted = true;
    const options = this.getOptions();
    const ssrDisabled = options.ssr === false;
    if (
      this.currentObservable &&
      !ssrDisabled &&
      !this.ssrInitiated()
    ) {
      this.startQuerySubscription();
    }

    if (!lazy || this.runLazy) {
      this.handleErrorOrCompleted();
    }

    this.previousOptions = options;
    return this.unmount.bind(this);
  }
<commit_msg>fix ssr: false not starting queries #8571<commit_after>import { equal } from '@wry/equality';

import { ApolloError } from '../../errors';

import {
  ApolloClient,
  NetworkStatus,
  FetchMoreQueryOptions,
  SubscribeToMoreOptions,
  ObservableQuery,
  FetchMoreOptions,
  UpdateQueryOptions,
  DocumentNode,
  TypedDocumentNode,
} from '../../core';

import {
  ObservableSubscription
} from '../../utilities';

import { DocumentType } from '../parser';
import {
  QueryResult,
  QueryDataOptions,
  QueryTuple,
  QueryLazyOptions,
  ObservableQueryFields,
} from '../types/types';
import { OperationData } from './OperationData';

type ObservableQueryOptions<TData, TVars> =
  ReturnType<QueryData<TData, TVars>["prepareObservableQueryOptions"]>;

export class QueryData<TData, TVariables> extends OperationData<
  QueryDataOptions<TData, TVariables>
> {
  public onNewData: () => void;
  private currentObservable?: ObservableQuery<TData, TVariables>;
  private currentSubscription?: ObservableSubscription;
  private runLazy: boolean = false;
  private lazyOptions?: QueryLazyOptions<TVariables>;
  private previous: {
    client?: ApolloClient<object>;
    query?: DocumentNode | TypedDocumentNode<TData, TVariables>;
    observableQueryOptions?: ObservableQueryOptions<TData, TVariables>;
    result?: QueryResult<TData, TVariables>;
    loading?: boolean;
    options?: QueryDataOptions<TData, TVariables>;
    error?: ApolloError;
  } = Object.create(null);

  constructor({
    options,
    context,
    onNewData
  }: {
    options: QueryDataOptions<TData, TVariables>;
    context: any;
    onNewData: () => void;
  }) {
    super(options, context);
    this.onNewData = onNewData;
  }

  public execute(): QueryResult<TData, TVariables> {
    this.refreshClient();

    const { skip, query } = this.getOptions();
    if (skip || query !== this.previous.query) {
      this.removeQuerySubscription();
      this.removeObservable(!skip);
      this.previous.query = query;
    }

    this.updateObservableQuery();

    return this.getExecuteSsrResult() || this.getExecuteResult();
  }

  public executeLazy(): QueryTuple<TData, TVariables> {
    return !this.runLazy
      ? [
          this.runLazyQuery,
          {
            loading: false,
            networkStatus: NetworkStatus.ready,
            called: false,
            data: undefined
          }
        ]
      : [this.runLazyQuery, this.execute()];
  }

  // For server-side rendering
  public fetchData(): Promise<void> | boolean {
    const options = this.getOptions();
    if (options.skip || options.ssr === false) return false;
    return new Promise(resolve => this.startQuerySubscription(resolve));
  }

  public afterExecute({ lazy = false }: { lazy?: boolean } = {}) {
    this.isMounted = true;
    const options = this.getOptions();
    if (
      this.currentObservable &&
      !this.ssrInitiated() &&
      !this.client.disableNetworkFetches
    ) {
      this.startQuerySubscription();
    }

    if (!lazy || this.runLazy) {
      this.handleErrorOrCompleted();
    }

    this.previousOptions = options;
    return this.unmount.bind(this);
  }

{'fraction_non_alphanumeric': 0.1252589641434263, 'fraction_numerical': 0.0006374501992031873, 'mean_word_length': 3.4011220196353436, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1977008', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1852, 'n_words': 388}