Skip to content

useLazyQuery's exec function return type doesn't thread errorPolicy through #13371

Description

@mx-bernhard

Issue Description

Using modern signatures ApolloClient.QueryResult<TData, TErrorPolicy> correctly narrows based on error policy — e.g. errorPolicy: 'none' maps to { data: TData; error?: never } since GraphQL errors reject the promise instead of resolving with partial data.

useLazyQuery's ExecFunction type hardcodes the default policy and never looks at the errorPolicy passed to the hook:

    type ExecFunction<TData, TVariables extends OperationVariables> = (...args: {} extends TVariables ? [
        options?: useLazyQuery.ExecOptions<TVariables>
    ] : [options: useLazyQuery.ExecOptions<TVariables>]) => ObservableQuery.ResultPromise<ApolloClient.QueryResult<TData /*, missing TErrorPolicy here */>>;

Repro

import type { TypedDocumentNode } from '@apollo/client';
import { useLazyQuery } from '@apollo/client/react';

const [loadOperation] = useLazyQuery({} as TypedDocumentNode<{ someField: boolean }, {}>, { errorPolicy: 'none' });

loadOperation({}).then(({ data }) => {
  data.someField; // TS18048: 'data' is possibly 'undefined'
});

Even though at runtime, with errorPolicy: 'none', data is guaranteed defined on promise resolution (a GraphQL error rejects the promise instead), the type says otherwise. Expected: ExecFunction/ResultTuple should carry the resolved TErrorPolicy (from the hook's Options), so the exec function's promise resolves to the correctly narrowed QueryResult<TData, TErrorPolicy>.

Link to Reproduction

This is a Typescript-only issue

Reproduction Steps

No response

@apollo/client version

4.2.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions