@constructive-io/graphql-react 2.14.10

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.
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useTableRowsPaginated = useTableRowsPaginated;
4
+ exports.useTableRowsInfinite = useTableRowsInfinite;
5
+ exports.useCreateTableRow = useCreateTableRow;
6
+ exports.useDeleteTableRow = useDeleteTableRow;
7
+ // @ts-nocheck
8
+ const react_1 = require("react");
9
+ const react_query_1 = require("react-query");
10
+ const use_graphql_client_1 = require("./use-graphql-client");
11
+ const use_constructive_client_1 = require("./use-constructive-client");
12
+ const noop = () => { };
13
+ function useTableRowsPaginated(options = {}) {
14
+ const { tableName, first, after, last, before, condition, filter, orderBy, onSuccess = noop, onError, skip = false, select, ...restOptions } = options;
15
+ const graphqlClient = (0, use_graphql_client_1.useGraphqlClient)();
16
+ const queryBuilder = (0, use_constructive_client_1.useConstructiveQuery)();
17
+ const graphqlQuery = (0, react_1.useMemo)(() => {
18
+ if (!queryBuilder)
19
+ return null;
20
+ const result = queryBuilder.query(tableName).getMany({ select }).print();
21
+ return {
22
+ hash: result._hash,
23
+ key: result._key
24
+ };
25
+ }, [queryBuilder, select, tableName]);
26
+ const params = (0, react_1.useMemo)(() => ({
27
+ skip,
28
+ first,
29
+ after,
30
+ last,
31
+ before,
32
+ condition,
33
+ filter,
34
+ orderBy,
35
+ graphqlQuery
36
+ }), [after, before, condition, filter, first, graphqlQuery, last, orderBy, skip]);
37
+ const query = (0, react_query_1.useQuery)([tableName, params], async ({ queryKey }) => {
38
+ const [, params] = queryKey;
39
+ const { skip, first, last, after, before, condition, filter, orderBy, graphqlQuery } = normalize(params);
40
+ if (skip || !graphqlQuery)
41
+ return null;
42
+ const result = await graphqlClient.request(graphqlQuery.hash, {
43
+ first,
44
+ last,
45
+ after,
46
+ before,
47
+ condition,
48
+ filter,
49
+ orderBy
50
+ });
51
+ return result[graphqlQuery.key];
52
+ }, {
53
+ onError,
54
+ onSuccess,
55
+ enabled: !!graphqlClient && !!graphqlQuery,
56
+ ...restOptions
57
+ // Read more what this does at:
58
+ // https://react-query.tanstack.com/guides/paginated-queries#better-paginated-queries-with-keeppreviousdata
59
+ // keepPreviousData: true
60
+ });
61
+ return query;
62
+ }
63
+ function useTableRowsInfinite(options = {}) {
64
+ const { tableName, condition, filter, orderBy, onSuccess = noop, onError, select, ...restOptions } = options;
65
+ const graphqlClient = (0, use_graphql_client_1.useGraphqlClient)();
66
+ const queryBuilder = (0, use_constructive_client_1.useConstructiveQuery)();
67
+ const graphqlQuery = (0, react_1.useMemo)(() => {
68
+ if (!queryBuilder)
69
+ return null;
70
+ const result = queryBuilder.query(tableName).getMany({ select }).print();
71
+ return {
72
+ hash: result._hash,
73
+ key: result._key
74
+ };
75
+ }, [queryBuilder, select, tableName]);
76
+ const params = (0, react_1.useMemo)(() => ({
77
+ graphqlQuery,
78
+ condition,
79
+ filter,
80
+ orderBy
81
+ }), [condition, filter, graphqlQuery, orderBy]);
82
+ const infiniteQuery = (0, react_query_1.useInfiniteQuery)([tableName, params], async ({ queryKey, pageParam = null }) => {
83
+ const [, params] = queryKey;
84
+ const { condition, filter, orderBy, graphqlQuery } = normalize(params);
85
+ if (!graphqlQuery)
86
+ return null;
87
+ const result = await graphqlClient.request(graphqlQuery.hash, {
88
+ first: 50,
89
+ after: pageParam,
90
+ condition,
91
+ filter,
92
+ orderBy
93
+ });
94
+ return result[graphqlQuery.key];
95
+ }, {
96
+ onError,
97
+ onSuccess,
98
+ enabled: !!graphqlClient && !!graphqlQuery,
99
+ getNextPageParam: (lastPage) => lastPage.pageInfo.endCursor,
100
+ ...restOptions
101
+ });
102
+ return infiniteQuery;
103
+ }
104
+ function useCreateTableRow(options = {}) {
105
+ const { tableName, onSuccess, onError } = options;
106
+ const queryClient = (0, react_query_1.useQueryClient)();
107
+ const graphqlClient = (0, use_graphql_client_1.useGraphqlClient)();
108
+ const queryBuilder = (0, use_constructive_client_1.useConstructiveQuery)();
109
+ const graphqlMutation = (0, react_1.useMemo)(() => {
110
+ if (!queryBuilder)
111
+ return null;
112
+ const result = queryBuilder.query(tableName).create().print();
113
+ return {
114
+ hash: result._hash,
115
+ key: result._key
116
+ };
117
+ }, [queryBuilder, tableName]);
118
+ const mutation = (0, react_query_1.useMutation)(async (variables) => {
119
+ const result = await graphqlClient.request(graphqlMutation.hash, variables);
120
+ return result[graphqlMutation.key];
121
+ }, {
122
+ onSuccess: (...args) => {
123
+ // Will invalidate every query that has query key starting with tableName
124
+ // ex: ['Action'] and ['Action', params] will both be invalidated
125
+ queryClient.invalidateQueries(tableName);
126
+ if (typeof onSuccess === 'function')
127
+ onSuccess(args);
128
+ },
129
+ onError
130
+ });
131
+ return mutation;
132
+ }
133
+ function useDeleteTableRow(options = {}) {
134
+ const { tableName, onSuccess, onError } = options;
135
+ const queryClient = (0, react_query_1.useQueryClient)();
136
+ const graphqlClient = (0, use_graphql_client_1.useGraphqlClient)();
137
+ const queryBuilder = (0, use_constructive_client_1.useConstructiveQuery)();
138
+ const graphqlMutation = (0, react_1.useMemo)(() => {
139
+ if (!queryBuilder)
140
+ return null;
141
+ const result = queryBuilder.query(tableName).delete().print();
142
+ return {
143
+ hash: result._hash,
144
+ key: result._key
145
+ };
146
+ }, [queryBuilder, tableName]);
147
+ const mutation = (0, react_query_1.useMutation)(async (variables) => {
148
+ const result = await graphqlClient.request(graphqlMutation.hash, variables);
149
+ return result[graphqlMutation.key];
150
+ }, {
151
+ onSuccess: (...args) => {
152
+ // Will invalidate every query that has query key starting with tableName
153
+ // ex: ['Action'] and ['Action', params] will both be invalidated
154
+ queryClient.invalidateQueries(tableName);
155
+ if (typeof onSuccess === 'function')
156
+ onSuccess(args);
157
+ },
158
+ onError
159
+ });
160
+ return mutation;
161
+ }
162
+ function normalize(params) {
163
+ return {
164
+ ...params,
165
+ after: params.after ? String(params.after) : null,
166
+ before: params.before ? String(params.before) : null,
167
+ first: isValidPageSize(params.first) ? undefined : Number(params.first),
168
+ last: isValidPageSize(params.last) ? undefined : Number(params.last)
169
+ };
170
+ }
171
+ function isValidPageSize(size) {
172
+ return isNaN(size) || size == null;
173
+ }