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

[TypeScript] Fix useGetOne and useGetMany params type when id param is undefined #9971

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions packages/ra-core/src/dataProvider/useGetMany.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from 'react';
import expect from 'expect';
import { render, waitFor } from '@testing-library/react';
import { QueryClient } from '@tanstack/react-query';

import { CoreAdminContext } from '../core';
import { useGetMany } from './useGetMany';
import { useGetOne } from './useGetOne';
import { testDataProvider } from '../dataProvider';
import { useState } from 'react';
import { QueryClient } from '@tanstack/react-query';

const UseGetMany = ({
resource,
Expand Down Expand Up @@ -39,7 +39,7 @@ const UseCustomGetMany = ({
options?: any;
callback?: Function;
}) => {
const [stateIds, setStateIds] = useState(ids);
const [stateIds, setStateIds] = React.useState(ids);
const hookValue = useGetMany(resource, { ids: stateIds }, options);
if (callback) callback(hookValue);

Expand Down Expand Up @@ -398,4 +398,45 @@ describe('useGetMany', () => {
expect(abort).toHaveBeenCalled();
});
});

describe('TypeScript', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

it('should return the parametric type', () => {
type Foo = { id: number; name: string };
const _Dummy = () => {
const { data, error, isPending } = useGetMany<Foo>('posts', {
ids: [1],
});
if (isPending || error) return null;
return <div>{data[0].name}</div>;
};
// no render needed, only checking types
});
it('should accept empty id param', () => {
const _Dummy = () => {
type Post = {
id: number;
tag_ids: number[];
};
const { data: comment } = useGetOne<Post>('comments', {
id: 1,
});
type Tag = {
id: number;
name: string;
};
const { data, error, isPending } = useGetMany<Tag>('posts', {
ids: comment?.tag_ids,
});
if (isPending || error) return null;
return (
<ul>
{data.map(tag => (
<li key={tag.id}>{tag.name}</li>
))}
</ul>
);
};
// no render needed, only checking types
});
});
});
4 changes: 3 additions & 1 deletion packages/ra-core/src/dataProvider/useGetMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { useEvent } from '../util';
*/
export const useGetMany = <RecordType extends RaRecord = any>(
resource: string,
params: Partial<GetManyParams> = {},
params: Partial<GetManyParams<RecordType>>,
options: UseGetManyOptions<RecordType> = {}
): UseGetManyHookValue<RecordType> => {
const { ids, meta } = params;
Expand All @@ -64,6 +64,7 @@ export const useGetMany = <RecordType extends RaRecord = any>(
onError = noop,
onSuccess = noop,
onSettled = noop,
enabled,
...queryOptions
} = options;
const onSuccessEvent = useEvent(onSuccess);
Expand Down Expand Up @@ -115,6 +116,7 @@ export const useGetMany = <RecordType extends RaRecord = any>(
}
},
retry: false,
enabled: enabled ?? ids != null,
...queryOptions,
});

Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/dataProvider/useGetManyAggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import { useEvent } from '../util';
*/
export const useGetManyAggregate = <RecordType extends RaRecord = any>(
resource: string,
params: GetManyParams,
params: Partial<GetManyParams<RecordType>>,
options: UseGetManyAggregateOptions<RecordType> = {}
): UseGetManyHookValue<RecordType> => {
const dataProvider = useDataProvider();
Expand All @@ -78,6 +78,7 @@ export const useGetManyAggregate = <RecordType extends RaRecord = any>(
onError = noop,
onSuccess = noop,
onSettled = noop,
enabled,
...queryOptions
} = options;
const onSuccessEvent = useEvent(onSuccess);
Expand Down Expand Up @@ -133,6 +134,7 @@ export const useGetManyAggregate = <RecordType extends RaRecord = any>(
});
}),
placeholderData,
enabled: enabled ?? ids != null,
retry: false,
...queryOptions,
});
Expand Down
34 changes: 34 additions & 0 deletions packages/ra-core/src/dataProvider/useGetOne.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,38 @@ describe('useGetOne', () => {
expect(abort).toHaveBeenCalled();
});
});
describe('TypeScript', () => {
it('should return the parametric type', () => {
type Foo = { id: number; name: string };
const _Dummy = () => {
const { data, error, isPending } = useGetOne<Foo>('posts', {
id: 1,
});
if (isPending || error) return null;
return <div>{data.name}</div>;
};
// no render needed, only checking types
});
it('should accept empty id param', () => {
const _Dummy = () => {
type Comment = {
id: number;
post_id: number;
};
const { data: comment } = useGetOne<Comment>('comments', {
id: 1,
});
type Post = {
id: number;
title: string;
};
const { data, error, isPending } = useGetOne<Post>('posts', {
id: comment?.post_id,
});
if (isPending || error) return null;
return <div>{data.title}</div>;
};
// no render needed, only checking types
});
});
Comment on lines +302 to +335
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, as we don't compile our tests, we won't ever see this fail in CI

});
26 changes: 15 additions & 11 deletions packages/ra-core/src/dataProvider/useGetOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ import { useEvent } from '../util';
*/
export const useGetOne = <RecordType extends RaRecord = any>(
resource: string,
{ id, meta }: GetOneParams<RecordType>,
{ id, meta }: Partial<GetOneParams<RecordType>>,
options: UseGetOneOptions<RecordType> = {}
): UseGetOneHookValue<RecordType> => {
const dataProvider = useDataProvider();
const {
onError = noop,
onSuccess = noop,
onSettled = noop,
enabled,
...queryOptions
} = options;
const onSuccessEvent = useEvent(onSuccess);
Expand All @@ -69,16 +70,19 @@ export const useGetOne = <RecordType extends RaRecord = any>(
// As the react-query cache is type-sensitive, we always stringify the identifier to get a match
queryKey: [resource, 'getOne', { id: String(id), meta }],
queryFn: queryParams =>
dataProvider
.getOne<RecordType>(resource, {
id,
meta,
signal:
dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
})
.then(({ data }) => data),
id == null
? new Promise(() => {})
: dataProvider
.getOne<RecordType>(resource, {
id,
meta,
signal:
dataProvider.supportAbortSignal === true
? queryParams.signal
: undefined,
})
.then(({ data }) => data),
enabled: enabled ?? id != null,
...queryOptions,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const withLifecycleCallbacks = (

getMany: async function <RecordType extends RaRecord = any>(
resource: string,
params: GetManyParams
params: GetManyParams<RecordType>
) {
let newParams = params;

Expand Down
6 changes: 3 additions & 3 deletions packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export type DataProvider<ResourceType extends string = string> = {

getMany: <RecordType extends RaRecord = any>(
resource: ResourceType,
params: GetManyParams & QueryFunctionContext
params: GetManyParams<RecordType> & QueryFunctionContext
) => Promise<GetManyResult<RecordType>>;

getManyReference: <RecordType extends RaRecord = any>(
Expand Down Expand Up @@ -172,8 +172,8 @@ export interface GetOneResult<RecordType extends RaRecord = any> {
data: RecordType;
}

export interface GetManyParams {
ids: Identifier[];
export interface GetManyParams<RecordType extends RaRecord = any> {
ids: RecordType['id'][];
meta?: any;
signal?: AbortSignal;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/ra-data-localforage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable import/no-anonymous-default-export */
import fakeRestProvider from 'ra-data-fakerest';

import {
CreateParams,
DataProvider,
Expand Down Expand Up @@ -111,7 +110,7 @@ export default async (
) => baseDataProvider.getOne<RecordType>(resource, params),
getMany: <RecordType extends RaRecord = any>(
resource: string,
params: GetManyParams
params: GetManyParams<RecordType>
) => baseDataProvider.getMany<RecordType>(resource, params),
getManyReference: <RecordType extends RaRecord = any>(
resource: string,
Expand Down
Loading