Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

v21 regression on Typescript inference through PreloadedQuery type/interface #5361

Copy link
Copy link

Description

@xfournet
Issue body actions

Before v21, the following code was valid for typescript

import { graphql, PreloadedQuery, useQueryLoader, usePreloadedQuery } from 'react-relay';
import Button from '@mui/material/Button';
import React from 'react';
import { AppQuery } from './__generated__/AppQuery.graphql';

const appQuery = graphql`
    query AppQuery {
        me {
            name
        }
    }
`;

const QueryFetcherExample = () => {
  const [queryReference, loadQuery, disposeQuery] = useQueryLoader<AppQuery>(appQuery);

  if (queryReference == null) {
    return (
      <Button onClick={() => loadQuery({})}> Click to reveal the name </Button>
    );
  }

  return (
    <>
      <Button onClick={disposeQuery}>
        Click to hide the name and dispose the query.
      </Button>
      <React.Suspense fallback="Loading">
        <NameDisplay queryReference={queryReference} />
      </React.Suspense>
    </>
  );
};

const NameDisplay = ({ queryReference }: { queryReference: PreloadedQuery<AppQuery> }) => {
  const data = usePreloadedQuery(appQuery, queryReference);

  return <h1>{data.me?.name}</h1>;
};

export default QueryFetcherExample;

On line 36 the type of data was inferred though the queryReference type PreloadedQuery<AppQuery> permitting to infer the result of usePreloadedQuery to be AppQuery$data

Image

Since v21 this is not working anymore. I removed the legacy @types/react-relay and @types/relay-runtime dependencies, so i'm based on the new embedded typescript definitions.
The root cause is the PreloadedQuery has been changed from an interface to a type alias:

Before

export interface PreloadedQuery<
    TQuery extends OperationType,
    TEnvironmentProviderOptions = EnvironmentProviderOptions,
> extends
    Readonly<{
        kind: "PreloadedQuery";
        environment: IEnvironment;
        environmentProviderOptions?: TEnvironmentProviderOptions | null | undefined;
        fetchKey: string | number;
        fetchPolicy: PreloadFetchPolicy;
        networkCacheConfig?: CacheConfig | null | undefined;
        id?: string | null | undefined;
        name: string;
        source?: Observable<GraphQLResponse> | null | undefined;
        variables: VariablesOf<TQuery>;
        dispose: DisposeFn;
        isDisposed: boolean;
    }>
{}

Since v21

export type PreloadedQuery<
    TQuery extends OperationType,
    TEnvironmentProviderOptions = EnvironmentProviderOptions,
> = Readonly<{
    kind: 'PreloadedQuery';
    environment: IEnvironment;
    environmentProviderOptions?: TEnvironmentProviderOptions | null | undefined;
    fetchKey: string | number;
    fetchPolicy: PreloadFetchPolicy;
    networkCacheConfig?: CacheConfig | null | undefined;
    id?: string | null | undefined;
    name: string;
    source?: Observable<GraphQLResponse> | null | undefined;
    variables: VariablesOf<TQuery>;
    dispose: DisposeFn;
    isDisposed: boolean;
}>;

With this new definition, the TQuery generic is lost, preventing the type inference on usePreloadedQuery , so result type is now unknown instead if AppQuery$data

Image

The change has happened here 0f00589#diff-ff6ae2b3d1d6c61b5656279464517ab646bfd9931f11408f5374ce1505827e77R137-R140
It should be reverted ?

Reactions are currently unavailable

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

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.