Skip to content
Open
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
20 changes: 20 additions & 0 deletions .changeset/tame-lions-smile.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 17 additions & 5 deletions documentation/docs/core/interface-references/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,37 @@ type OpenNotificationParams = {
### MetaQuery

```tsx
type MetaQuery = {
interface MetaQuery extends QueryBuilderOptions, GraphQLQueryOptions {
queryContext?: Omit<QueryFunctionContext, "meta">;
[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

```tsx
import type { DocumentNode } from "graphql";

type GraphQLQueryOptions = {
interface GraphQLQueryOptions {
gqlQuery?: DocumentNode;
gqlMutation?: DocumentNode;
gqlVariables?: {
[key: string]: any;
};
};
}
```

### QueryFunctionContext
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/contexts/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<QueryFunctionContext, "meta">;
} & QueryBuilderOptions &
GraphQLQueryOptions;
}

export interface Pagination {
/**
Expand Down