diff --git a/.changeset/tame-lions-smile.md b/.changeset/tame-lions-smile.md new file mode 100644 index 0000000000000..afd8668e8e1c9 --- /dev/null +++ b/.changeset/tame-lions-smile.md @@ -0,0 +1,20 @@ +--- +"@refinedev/core": minor +--- + +Converted `MetaQuery` and `GraphQLQueryOptions` from type aliases to interfaces. + +This is structurally identical and fully backward compatible, but it unlocks TypeScript's [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) for `MetaQuery`. Consumers can now extend it in their own apps to get autocompletion and type-checking for custom `meta` fields (e.g. `meta.queryParams` used by custom data providers) across every hook that accepts `meta`: + +```ts +declare module "@refinedev/core" { + interface MetaQuery { + queryParams?: { + _pull?: string; + [key: string]: unknown; + }; + } +} +``` + +Previously this was not possible because declaration merging only applies to interfaces, not type aliases. diff --git a/documentation/docs/core/interface-references/index.md b/documentation/docs/core/interface-references/index.md index a721156d60b71..c9b1d22669348 100644 --- a/documentation/docs/core/interface-references/index.md +++ b/documentation/docs/core/interface-references/index.md @@ -203,11 +203,23 @@ type OpenNotificationParams = { ### MetaQuery ```tsx -type MetaQuery = { +interface MetaQuery extends QueryBuilderOptions, GraphQLQueryOptions { queryContext?: Omit; [key: string]: any; -} & QueryBuilderOptions & - GraphQLQueryOptions; +} +``` + +Since `MetaQuery` is an interface, you can use [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) to add typed fields to it (e.g. the `queryParams` your data provider reads) and get autocompletion/type-checking across every hook that accepts `meta`: + +```tsx +declare module "@refinedev/core" { + interface MetaQuery { + queryParams?: { + _pull?: string; + [key: string]: unknown; + }; + } +} ``` ### GraphQLQueryOptions @@ -215,13 +227,13 @@ type MetaQuery = { ```tsx import type { DocumentNode } from "graphql"; -type GraphQLQueryOptions = { +interface GraphQLQueryOptions { gqlQuery?: DocumentNode; gqlMutation?: DocumentNode; gqlVariables?: { [key: string]: any; }; -}; +} ``` ### QueryFunctionContext diff --git a/packages/core/src/contexts/data/types.ts b/packages/core/src/contexts/data/types.ts index 8b63bb716ba3f..cf61e1e211ee8 100644 --- a/packages/core/src/contexts/data/types.ts +++ b/packages/core/src/contexts/data/types.ts @@ -39,7 +39,7 @@ export interface QueryBuilderOptions { variables?: VariableOptions; } -export type GraphQLQueryOptions = { +export interface GraphQLQueryOptions { /** * @description GraphQL query to be used by data providers. * @optional @@ -161,13 +161,12 @@ export type GraphQLQueryOptions = { gqlVariables?: { [key: string]: any; }; -}; +} -export type MetaQuery = { +export interface MetaQuery extends QueryBuilderOptions, GraphQLQueryOptions { [k: string]: any; queryContext?: Omit; -} & QueryBuilderOptions & - GraphQLQueryOptions; +} export interface Pagination { /**