@izumisy-tailor/tailor-data-viewer 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -10,8 +10,7 @@ import type {
|
|
|
10
10
|
SortState,
|
|
11
11
|
UseCollectionParamsOptions,
|
|
12
12
|
UseCollectionParamsReturn,
|
|
13
|
-
|
|
14
|
-
MatchingTableName,
|
|
13
|
+
ExtractQueryVariables,
|
|
15
14
|
} from "../types";
|
|
16
15
|
import { fieldTypeToFilterConfig } from "../types";
|
|
17
16
|
import type { FieldName } from "../types";
|
|
@@ -24,29 +23,46 @@ import type { FieldName } from "../types";
|
|
|
24
23
|
* Hook for managing collection query parameters (filters, sort, pagination)
|
|
25
24
|
* with metadata-based field name typing and automatic `fieldType` detection.
|
|
26
25
|
*
|
|
26
|
+
* When `query` is provided, the output `variables` is typed to match
|
|
27
|
+
* the GraphQL query's expected variables (e.g. `VariablesOf<typeof QUERY>`).
|
|
28
|
+
* The `query` value is only used for type inference and ignored at runtime.
|
|
29
|
+
*
|
|
27
30
|
* @example
|
|
28
31
|
* ```tsx
|
|
29
32
|
* import { tableMetadata } from "./generated/data-viewer-metadata.generated";
|
|
30
33
|
*
|
|
34
|
+
* // Without query — variables typed as QueryVariables<FieldName>
|
|
31
35
|
* const params = useCollectionParams({
|
|
32
36
|
* metadata: tableMetadata,
|
|
33
37
|
* tableName: "task",
|
|
34
38
|
* pageSize: 20,
|
|
35
39
|
* });
|
|
36
40
|
*
|
|
37
|
-
*
|
|
38
|
-
* params
|
|
41
|
+
* // With query — variables typed as VariablesOf<typeof GET_TASKS>
|
|
42
|
+
* const params = useCollectionParams({
|
|
43
|
+
* metadata: tableMetadata,
|
|
44
|
+
* tableName: "task",
|
|
45
|
+
* query: GET_TASKS,
|
|
46
|
+
* pageSize: 20,
|
|
47
|
+
* });
|
|
39
48
|
* ```
|
|
40
49
|
*/
|
|
41
50
|
export function useCollectionParams<
|
|
42
51
|
const TMetadata extends TableMetadataMap,
|
|
43
52
|
TTableName extends string & keyof TMetadata,
|
|
53
|
+
TQuery = never,
|
|
44
54
|
>(
|
|
45
55
|
options: UseCollectionParamsOptions<FieldName<TMetadata, TTableName>> & {
|
|
46
56
|
metadata: TMetadata;
|
|
47
57
|
tableName: TTableName;
|
|
58
|
+
query?: TQuery;
|
|
48
59
|
},
|
|
49
|
-
): UseCollectionParamsReturn<
|
|
60
|
+
): UseCollectionParamsReturn<
|
|
61
|
+
FieldName<TMetadata, TTableName>,
|
|
62
|
+
[TQuery] extends [never]
|
|
63
|
+
? QueryVariables<FieldName<TMetadata, TTableName>>
|
|
64
|
+
: ExtractQueryVariables<TQuery>
|
|
65
|
+
>;
|
|
50
66
|
|
|
51
67
|
/**
|
|
52
68
|
* Hook for managing collection query parameters (filters, sort, pagination).
|
|
@@ -54,50 +70,28 @@ export function useCollectionParams<
|
|
|
54
70
|
* Produces `variables` in Tailor Platform format that can be passed directly
|
|
55
71
|
* to a GraphQL query (e.g. urql's `useQuery`).
|
|
56
72
|
*
|
|
73
|
+
* When `query` is provided, the output `variables` is typed to match
|
|
74
|
+
* the GraphQL query's expected variables.
|
|
75
|
+
*
|
|
57
76
|
* @example
|
|
58
77
|
* ```tsx
|
|
59
78
|
* const params = useCollectionParams({ pageSize: 20 });
|
|
60
79
|
* const [result] = useQuery({ query: GET_ORDERS, variables: params.variables });
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export function useCollectionParams(
|
|
64
|
-
options?: UseCollectionParamsOptions,
|
|
65
|
-
): UseCollectionParamsReturn;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Hook for managing collection query parameters (filters, sort, pagination)
|
|
69
|
-
* with an explicit `TVariables` type for the output `variables`.
|
|
70
|
-
*
|
|
71
|
-
* The `order[].field` union is extracted from `TVariables` and used as
|
|
72
|
-
* `TFieldName` for `addFilter`, `setSort`, `initialSort`, etc.
|
|
73
|
-
* When `metadata` is provided, `tableName` is constrained to tables
|
|
74
|
-
* whose fields are a superset of the extracted field names.
|
|
75
|
-
*
|
|
76
|
-
* @typeParam TVariables - The exact type for the output `variables` property.
|
|
77
|
-
* Typically `VariablesOf<typeof YOUR_QUERY>` from gql-tada.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```tsx
|
|
81
|
-
* import type { VariablesOf } from "gql.tada";
|
|
82
80
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* tableName: "order", // ← constrained to tables matching order[].field
|
|
86
|
-
* pageSize: 20,
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* params.addFilter("status", "ACTIVE"); // ← field name auto-completed
|
|
90
|
-
* const [result] = useQuery({ query: GET_ORDERS, variables: params.variables });
|
|
81
|
+
* // With query — variables auto-typed
|
|
82
|
+
* const params = useCollectionParams({ query: GET_ORDERS, pageSize: 20 });
|
|
91
83
|
* ```
|
|
92
84
|
*/
|
|
93
|
-
export function useCollectionParams<
|
|
94
|
-
options?: UseCollectionParamsOptions
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
: MatchingTableName<TableMetadataMap, ExtractOrderField<TVariables>>;
|
|
85
|
+
export function useCollectionParams<TQuery = never>(
|
|
86
|
+
options?: UseCollectionParamsOptions & {
|
|
87
|
+
query?: TQuery;
|
|
88
|
+
metadata?: never;
|
|
89
|
+
tableName?: never;
|
|
99
90
|
},
|
|
100
|
-
): UseCollectionParamsReturn<
|
|
91
|
+
): UseCollectionParamsReturn<
|
|
92
|
+
string,
|
|
93
|
+
[TQuery] extends [never] ? QueryVariables : ExtractQueryVariables<TQuery>
|
|
94
|
+
>;
|
|
101
95
|
|
|
102
96
|
// -----------------------------------------------------------------------------
|
|
103
97
|
// Implementation
|
package/src/component/index.ts
CHANGED
package/src/component/types.ts
CHANGED
|
@@ -506,6 +506,25 @@ export type ExtractOrderField<T> = T extends {
|
|
|
506
506
|
: string
|
|
507
507
|
: string;
|
|
508
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Extract the variables type from a GraphQL query object.
|
|
511
|
+
*
|
|
512
|
+
* Works with `TypedDocumentNode` (used by gql-tada, graphql-codegen, etc.)
|
|
513
|
+
* which stores the variables type in the `__variablesType` brand property.
|
|
514
|
+
*
|
|
515
|
+
* @example
|
|
516
|
+
* ```ts
|
|
517
|
+
* const GET_ORDERS = graphql(`query Orders($first: Int, ...) { ... }`);
|
|
518
|
+
* type Vars = ExtractQueryVariables<typeof GET_ORDERS>;
|
|
519
|
+
* // → { first?: number | null; order?: OrderInput[] | null; ... }
|
|
520
|
+
* ```
|
|
521
|
+
*/
|
|
522
|
+
export type ExtractQueryVariables<T> = T extends {
|
|
523
|
+
__variablesType?: infer V;
|
|
524
|
+
}
|
|
525
|
+
? NonNullable<V>
|
|
526
|
+
: never;
|
|
527
|
+
|
|
509
528
|
/**
|
|
510
529
|
* Find table names in metadata whose fields are a superset of `TFieldName`.
|
|
511
530
|
*
|