drizzle-graphql-suite 0.5.0
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/LICENSE +21 -0
- package/README.md +126 -0
- package/package.json +76 -0
- package/packages/client/README.md +236 -0
- package/packages/client/dist/LICENSE +21 -0
- package/packages/client/dist/README.md +236 -0
- package/packages/client/dist/client.d.ts +19 -0
- package/packages/client/dist/entity.d.ts +57 -0
- package/packages/client/dist/errors.d.ts +19 -0
- package/packages/client/dist/index.d.ts +11 -0
- package/packages/client/dist/index.js +439 -0
- package/packages/client/dist/infer.d.ts +86 -0
- package/packages/client/dist/package.json +37 -0
- package/packages/client/dist/query-builder.d.ts +12 -0
- package/packages/client/dist/schema-builder.d.ts +15 -0
- package/packages/client/dist/types.d.ts +45 -0
- package/packages/query/README.md +276 -0
- package/packages/query/dist/LICENSE +21 -0
- package/packages/query/dist/README.md +276 -0
- package/packages/query/dist/index.d.ts +7 -0
- package/packages/query/dist/index.js +172 -0
- package/packages/query/dist/package.json +39 -0
- package/packages/query/dist/provider.d.ts +7 -0
- package/packages/query/dist/test-setup.d.ts +1 -0
- package/packages/query/dist/test-utils.d.ts +40 -0
- package/packages/query/dist/types.d.ts +4 -0
- package/packages/query/dist/useEntity.d.ts +2 -0
- package/packages/query/dist/useEntityInfiniteQuery.d.ts +26 -0
- package/packages/query/dist/useEntityList.d.ts +22 -0
- package/packages/query/dist/useEntityMutation.d.ts +41 -0
- package/packages/query/dist/useEntityQuery.d.ts +21 -0
- package/packages/schema/README.md +348 -0
- package/packages/schema/dist/LICENSE +21 -0
- package/packages/schema/dist/README.md +348 -0
- package/packages/schema/dist/adapters/pg.d.ts +10 -0
- package/packages/schema/dist/adapters/types.d.ts +11 -0
- package/packages/schema/dist/case-ops.d.ts +2 -0
- package/packages/schema/dist/codegen.d.ts +12 -0
- package/packages/schema/dist/data-mappers.d.ts +12 -0
- package/packages/schema/dist/graphql/scalars.d.ts +2 -0
- package/packages/schema/dist/graphql/type-builder.d.ts +7 -0
- package/packages/schema/dist/index.d.ts +26 -0
- package/packages/schema/dist/index.js +1812 -0
- package/packages/schema/dist/package.json +41 -0
- package/packages/schema/dist/schema-builder.d.ts +65 -0
- package/packages/schema/dist/types.d.ts +117 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# @drizzle-graphql-suite/query
|
|
2
|
+
|
|
3
|
+
TanStack React Query hooks for `drizzle-graphql-suite/client` — type-safe data fetching with caching, pagination, and mutations.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Provider
|
|
8
|
+
|
|
9
|
+
Wrap your app with `<GraphQLProvider>` inside a `<QueryClientProvider>`:
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
13
|
+
import { GraphQLProvider } from 'drizzle-graphql-suite/query'
|
|
14
|
+
import { client } from './graphql-client'
|
|
15
|
+
|
|
16
|
+
const queryClient = new QueryClient()
|
|
17
|
+
|
|
18
|
+
function App() {
|
|
19
|
+
return (
|
|
20
|
+
<QueryClientProvider client={queryClient}>
|
|
21
|
+
<GraphQLProvider client={client}>
|
|
22
|
+
{/* your app */}
|
|
23
|
+
</GraphQLProvider>
|
|
24
|
+
</QueryClientProvider>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### `useGraphQLClient()`
|
|
30
|
+
|
|
31
|
+
Access the raw `GraphQLClient` instance from context:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { useGraphQLClient } from 'drizzle-graphql-suite/query'
|
|
35
|
+
|
|
36
|
+
function MyComponent() {
|
|
37
|
+
const client = useGraphQLClient()
|
|
38
|
+
// client.entity('user'), client.execute(query, variables), etc.
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### `useEntity(entityName)`
|
|
43
|
+
|
|
44
|
+
Get a typed `EntityClient` for use with query and mutation hooks:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { useEntity } from 'drizzle-graphql-suite/query'
|
|
48
|
+
|
|
49
|
+
function UserList() {
|
|
50
|
+
const user = useEntity('user')
|
|
51
|
+
// Pass `user` to useEntityList, useEntityQuery, etc.
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Query Hooks
|
|
56
|
+
|
|
57
|
+
### `useEntityList(entity, params, options?)`
|
|
58
|
+
|
|
59
|
+
Fetch a list of records. Returns `UseQueryResult<T[]>`.
|
|
60
|
+
|
|
61
|
+
**Params**: `select`, `where`, `limit`, `offset`, `orderBy`
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { useEntity, useEntityList } from 'drizzle-graphql-suite/query'
|
|
65
|
+
|
|
66
|
+
function UserList() {
|
|
67
|
+
const user = useEntity('user')
|
|
68
|
+
const { data, isLoading, error } = useEntityList(user, {
|
|
69
|
+
select: { id: true, name: true, email: true },
|
|
70
|
+
where: { role: { eq: 'admin' } },
|
|
71
|
+
orderBy: { name: { direction: 'asc', priority: 1 } },
|
|
72
|
+
limit: 20,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
if (isLoading) return <div>Loading...</div>
|
|
76
|
+
if (error) return <div>Error: {error.message}</div>
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<ul>
|
|
80
|
+
{data?.map((u) => <li key={u.id}>{u.name} ({u.email})</li>)}
|
|
81
|
+
</ul>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### `useEntityQuery(entity, params, options?)`
|
|
87
|
+
|
|
88
|
+
Fetch a single record. Returns `UseQueryResult<T | null>`.
|
|
89
|
+
|
|
90
|
+
**Params**: `select`, `where`, `offset`, `orderBy`
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
function UserDetail({ userId }: { userId: string }) {
|
|
94
|
+
const user = useEntity('user')
|
|
95
|
+
const { data } = useEntityQuery(user, {
|
|
96
|
+
select: { id: true, name: true, email: true, role: true },
|
|
97
|
+
where: { id: { eq: userId } },
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
if (!data) return null
|
|
101
|
+
return <div>{data.name} — {data.role}</div>
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `useEntityInfiniteQuery(entity, params, options?)`
|
|
106
|
+
|
|
107
|
+
Infinite scrolling with cursor-based pagination. Returns `UseInfiniteQueryResult` with pages containing `{ items: T[], count: number }`.
|
|
108
|
+
|
|
109
|
+
**Params**: `select`, `where`, `pageSize`, `orderBy`
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
function InfiniteUserList() {
|
|
113
|
+
const user = useEntity('user')
|
|
114
|
+
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
|
|
115
|
+
useEntityInfiniteQuery(user, {
|
|
116
|
+
select: { id: true, name: true },
|
|
117
|
+
pageSize: 20,
|
|
118
|
+
orderBy: { name: { direction: 'asc', priority: 1 } },
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const allUsers = data?.pages.flatMap((page) => page.items) ?? []
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<div>
|
|
125
|
+
<ul>
|
|
126
|
+
{allUsers.map((u) => <li key={u.id}>{u.name}</li>)}
|
|
127
|
+
</ul>
|
|
128
|
+
{hasNextPage && (
|
|
129
|
+
<button onClick={() => fetchNextPage()} disabled={isFetchingNextPage}>
|
|
130
|
+
{isFetchingNextPage ? 'Loading...' : 'Load more'}
|
|
131
|
+
</button>
|
|
132
|
+
)}
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Query Options
|
|
139
|
+
|
|
140
|
+
All query hooks accept an optional `options` parameter:
|
|
141
|
+
|
|
142
|
+
| Option | Type | Description |
|
|
143
|
+
|--------|------|-------------|
|
|
144
|
+
| `enabled` | `boolean` | Disable automatic fetching |
|
|
145
|
+
| `gcTime` | `number` | Garbage collection time (ms) |
|
|
146
|
+
| `staleTime` | `number` | Time until data is considered stale (ms) |
|
|
147
|
+
| `refetchOnWindowFocus` | `boolean` | Refetch when window regains focus |
|
|
148
|
+
| `queryKey` | `unknown[]` | Override the auto-generated query key |
|
|
149
|
+
|
|
150
|
+
## Mutation Hooks
|
|
151
|
+
|
|
152
|
+
### `useEntityInsert(entity, returning?, options?)`
|
|
153
|
+
|
|
154
|
+
Insert records. Call `.mutate({ values })` to execute.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
function CreateUser() {
|
|
158
|
+
const user = useEntity('user')
|
|
159
|
+
const { mutate, isPending } = useEntityInsert(
|
|
160
|
+
user,
|
|
161
|
+
{ id: true, name: true },
|
|
162
|
+
{ onSuccess: (data) => console.log('Created:', data) },
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
return (
|
|
166
|
+
<button
|
|
167
|
+
disabled={isPending}
|
|
168
|
+
onClick={() => mutate({
|
|
169
|
+
values: [{ name: 'Alice', email: 'alice@example.com' }],
|
|
170
|
+
})}
|
|
171
|
+
>
|
|
172
|
+
Create User
|
|
173
|
+
</button>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### `useEntityUpdate(entity, returning?, options?)`
|
|
179
|
+
|
|
180
|
+
Update records. Call `.mutate({ set, where })` to execute.
|
|
181
|
+
|
|
182
|
+
```tsx
|
|
183
|
+
function UpdateRole({ userId }: { userId: string }) {
|
|
184
|
+
const user = useEntity('user')
|
|
185
|
+
const { mutate } = useEntityUpdate(user, { id: true, role: true })
|
|
186
|
+
|
|
187
|
+
return (
|
|
188
|
+
<button onClick={() => mutate({
|
|
189
|
+
set: { role: 'admin' },
|
|
190
|
+
where: { id: { eq: userId } },
|
|
191
|
+
})}>
|
|
192
|
+
Make Admin
|
|
193
|
+
</button>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### `useEntityDelete(entity, returning?, options?)`
|
|
199
|
+
|
|
200
|
+
Delete records. Call `.mutate({ where })` to execute.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
function DeleteUser({ userId }: { userId: string }) {
|
|
204
|
+
const user = useEntity('user')
|
|
205
|
+
const { mutate } = useEntityDelete(user, { id: true })
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<button onClick={() => mutate({
|
|
209
|
+
where: { id: { eq: userId } },
|
|
210
|
+
})}>
|
|
211
|
+
Delete
|
|
212
|
+
</button>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Mutation Options
|
|
218
|
+
|
|
219
|
+
All mutation hooks accept an optional `options` parameter:
|
|
220
|
+
|
|
221
|
+
| Option | Type | Description |
|
|
222
|
+
|--------|------|-------------|
|
|
223
|
+
| `invalidate` | `boolean` | Invalidate queries after mutation (default: `true`) |
|
|
224
|
+
| `invalidateKey` | `unknown[]` | Custom query key prefix to invalidate |
|
|
225
|
+
| `onSuccess` | `(data) => void` | Callback after successful mutation |
|
|
226
|
+
| `onError` | `(error) => void` | Callback after failed mutation |
|
|
227
|
+
|
|
228
|
+
## Cache Invalidation
|
|
229
|
+
|
|
230
|
+
By default, all mutations invalidate queries with the `['gql']` key prefix. Since all query hooks use keys starting with `['gql', ...]`, this means every mutation refreshes all GraphQL queries.
|
|
231
|
+
|
|
232
|
+
### Custom Invalidation Key
|
|
233
|
+
|
|
234
|
+
Narrow invalidation to specific queries:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
const { mutate } = useEntityUpdate(user, { id: true }, {
|
|
238
|
+
invalidateKey: ['gql', 'list'], // only invalidate list queries
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Disable Invalidation
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
const { mutate } = useEntityInsert(user, undefined, {
|
|
246
|
+
invalidate: false,
|
|
247
|
+
})
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Query Key Override
|
|
251
|
+
|
|
252
|
+
Override the auto-generated key on query hooks for fine-grained cache control:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
const { data } = useEntityList(user, params, {
|
|
256
|
+
queryKey: ['users', 'admin-list'],
|
|
257
|
+
})
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Type Inference Flow
|
|
261
|
+
|
|
262
|
+
Types flow end-to-end from your Drizzle schema to hook return types:
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
Drizzle Schema (tables + relations)
|
|
266
|
+
↓ InferEntityDefs
|
|
267
|
+
EntityDefs (fields, filters, inputs, orderBy per table)
|
|
268
|
+
↓ createDrizzleClient
|
|
269
|
+
GraphQLClient<SchemaDescriptor, EntityDefs>
|
|
270
|
+
↓ useEntity / client.entity()
|
|
271
|
+
EntityClient<EntityDefs, EntityDef>
|
|
272
|
+
↓ useEntityList / useEntityQuery (select param)
|
|
273
|
+
InferResult<EntityDefs, EntityDef, Select>
|
|
274
|
+
↓
|
|
275
|
+
Fully typed data: only selected fields, relations resolve to T[] or T | null
|
|
276
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Annexare Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# @drizzle-graphql-suite/query
|
|
2
|
+
|
|
3
|
+
TanStack React Query hooks for `drizzle-graphql-suite/client` — type-safe data fetching with caching, pagination, and mutations.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
### Provider
|
|
8
|
+
|
|
9
|
+
Wrap your app with `<GraphQLProvider>` inside a `<QueryClientProvider>`:
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
13
|
+
import { GraphQLProvider } from 'drizzle-graphql-suite/query'
|
|
14
|
+
import { client } from './graphql-client'
|
|
15
|
+
|
|
16
|
+
const queryClient = new QueryClient()
|
|
17
|
+
|
|
18
|
+
function App() {
|
|
19
|
+
return (
|
|
20
|
+
<QueryClientProvider client={queryClient}>
|
|
21
|
+
<GraphQLProvider client={client}>
|
|
22
|
+
{/* your app */}
|
|
23
|
+
</GraphQLProvider>
|
|
24
|
+
</QueryClientProvider>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### `useGraphQLClient()`
|
|
30
|
+
|
|
31
|
+
Access the raw `GraphQLClient` instance from context:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { useGraphQLClient } from 'drizzle-graphql-suite/query'
|
|
35
|
+
|
|
36
|
+
function MyComponent() {
|
|
37
|
+
const client = useGraphQLClient()
|
|
38
|
+
// client.entity('user'), client.execute(query, variables), etc.
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### `useEntity(entityName)`
|
|
43
|
+
|
|
44
|
+
Get a typed `EntityClient` for use with query and mutation hooks:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { useEntity } from 'drizzle-graphql-suite/query'
|
|
48
|
+
|
|
49
|
+
function UserList() {
|
|
50
|
+
const user = useEntity('user')
|
|
51
|
+
// Pass `user` to useEntityList, useEntityQuery, etc.
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Query Hooks
|
|
56
|
+
|
|
57
|
+
### `useEntityList(entity, params, options?)`
|
|
58
|
+
|
|
59
|
+
Fetch a list of records. Returns `UseQueryResult<T[]>`.
|
|
60
|
+
|
|
61
|
+
**Params**: `select`, `where`, `limit`, `offset`, `orderBy`
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { useEntity, useEntityList } from 'drizzle-graphql-suite/query'
|
|
65
|
+
|
|
66
|
+
function UserList() {
|
|
67
|
+
const user = useEntity('user')
|
|
68
|
+
const { data, isLoading, error } = useEntityList(user, {
|
|
69
|
+
select: { id: true, name: true, email: true },
|
|
70
|
+
where: { role: { eq: 'admin' } },
|
|
71
|
+
orderBy: { name: { direction: 'asc', priority: 1 } },
|
|
72
|
+
limit: 20,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
if (isLoading) return <div>Loading...</div>
|
|
76
|
+
if (error) return <div>Error: {error.message}</div>
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<ul>
|
|
80
|
+
{data?.map((u) => <li key={u.id}>{u.name} ({u.email})</li>)}
|
|
81
|
+
</ul>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### `useEntityQuery(entity, params, options?)`
|
|
87
|
+
|
|
88
|
+
Fetch a single record. Returns `UseQueryResult<T | null>`.
|
|
89
|
+
|
|
90
|
+
**Params**: `select`, `where`, `offset`, `orderBy`
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
function UserDetail({ userId }: { userId: string }) {
|
|
94
|
+
const user = useEntity('user')
|
|
95
|
+
const { data } = useEntityQuery(user, {
|
|
96
|
+
select: { id: true, name: true, email: true, role: true },
|
|
97
|
+
where: { id: { eq: userId } },
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
if (!data) return null
|
|
101
|
+
return <div>{data.name} — {data.role}</div>
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### `useEntityInfiniteQuery(entity, params, options?)`
|
|
106
|
+
|
|
107
|
+
Infinite scrolling with cursor-based pagination. Returns `UseInfiniteQueryResult` with pages containing `{ items: T[], count: number }`.
|
|
108
|
+
|
|
109
|
+
**Params**: `select`, `where`, `pageSize`, `orderBy`
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
function InfiniteUserList() {
|
|
113
|
+
const user = useEntity('user')
|
|
114
|
+
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
|
|
115
|
+
useEntityInfiniteQuery(user, {
|
|
116
|
+
select: { id: true, name: true },
|
|
117
|
+
pageSize: 20,
|
|
118
|
+
orderBy: { name: { direction: 'asc', priority: 1 } },
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const allUsers = data?.pages.flatMap((page) => page.items) ?? []
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<div>
|
|
125
|
+
<ul>
|
|
126
|
+
{allUsers.map((u) => <li key={u.id}>{u.name}</li>)}
|
|
127
|
+
</ul>
|
|
128
|
+
{hasNextPage && (
|
|
129
|
+
<button onClick={() => fetchNextPage()} disabled={isFetchingNextPage}>
|
|
130
|
+
{isFetchingNextPage ? 'Loading...' : 'Load more'}
|
|
131
|
+
</button>
|
|
132
|
+
)}
|
|
133
|
+
</div>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Query Options
|
|
139
|
+
|
|
140
|
+
All query hooks accept an optional `options` parameter:
|
|
141
|
+
|
|
142
|
+
| Option | Type | Description |
|
|
143
|
+
|--------|------|-------------|
|
|
144
|
+
| `enabled` | `boolean` | Disable automatic fetching |
|
|
145
|
+
| `gcTime` | `number` | Garbage collection time (ms) |
|
|
146
|
+
| `staleTime` | `number` | Time until data is considered stale (ms) |
|
|
147
|
+
| `refetchOnWindowFocus` | `boolean` | Refetch when window regains focus |
|
|
148
|
+
| `queryKey` | `unknown[]` | Override the auto-generated query key |
|
|
149
|
+
|
|
150
|
+
## Mutation Hooks
|
|
151
|
+
|
|
152
|
+
### `useEntityInsert(entity, returning?, options?)`
|
|
153
|
+
|
|
154
|
+
Insert records. Call `.mutate({ values })` to execute.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
function CreateUser() {
|
|
158
|
+
const user = useEntity('user')
|
|
159
|
+
const { mutate, isPending } = useEntityInsert(
|
|
160
|
+
user,
|
|
161
|
+
{ id: true, name: true },
|
|
162
|
+
{ onSuccess: (data) => console.log('Created:', data) },
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
return (
|
|
166
|
+
<button
|
|
167
|
+
disabled={isPending}
|
|
168
|
+
onClick={() => mutate({
|
|
169
|
+
values: [{ name: 'Alice', email: 'alice@example.com' }],
|
|
170
|
+
})}
|
|
171
|
+
>
|
|
172
|
+
Create User
|
|
173
|
+
</button>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### `useEntityUpdate(entity, returning?, options?)`
|
|
179
|
+
|
|
180
|
+
Update records. Call `.mutate({ set, where })` to execute.
|
|
181
|
+
|
|
182
|
+
```tsx
|
|
183
|
+
function UpdateRole({ userId }: { userId: string }) {
|
|
184
|
+
const user = useEntity('user')
|
|
185
|
+
const { mutate } = useEntityUpdate(user, { id: true, role: true })
|
|
186
|
+
|
|
187
|
+
return (
|
|
188
|
+
<button onClick={() => mutate({
|
|
189
|
+
set: { role: 'admin' },
|
|
190
|
+
where: { id: { eq: userId } },
|
|
191
|
+
})}>
|
|
192
|
+
Make Admin
|
|
193
|
+
</button>
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### `useEntityDelete(entity, returning?, options?)`
|
|
199
|
+
|
|
200
|
+
Delete records. Call `.mutate({ where })` to execute.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
function DeleteUser({ userId }: { userId: string }) {
|
|
204
|
+
const user = useEntity('user')
|
|
205
|
+
const { mutate } = useEntityDelete(user, { id: true })
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<button onClick={() => mutate({
|
|
209
|
+
where: { id: { eq: userId } },
|
|
210
|
+
})}>
|
|
211
|
+
Delete
|
|
212
|
+
</button>
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Mutation Options
|
|
218
|
+
|
|
219
|
+
All mutation hooks accept an optional `options` parameter:
|
|
220
|
+
|
|
221
|
+
| Option | Type | Description |
|
|
222
|
+
|--------|------|-------------|
|
|
223
|
+
| `invalidate` | `boolean` | Invalidate queries after mutation (default: `true`) |
|
|
224
|
+
| `invalidateKey` | `unknown[]` | Custom query key prefix to invalidate |
|
|
225
|
+
| `onSuccess` | `(data) => void` | Callback after successful mutation |
|
|
226
|
+
| `onError` | `(error) => void` | Callback after failed mutation |
|
|
227
|
+
|
|
228
|
+
## Cache Invalidation
|
|
229
|
+
|
|
230
|
+
By default, all mutations invalidate queries with the `['gql']` key prefix. Since all query hooks use keys starting with `['gql', ...]`, this means every mutation refreshes all GraphQL queries.
|
|
231
|
+
|
|
232
|
+
### Custom Invalidation Key
|
|
233
|
+
|
|
234
|
+
Narrow invalidation to specific queries:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
const { mutate } = useEntityUpdate(user, { id: true }, {
|
|
238
|
+
invalidateKey: ['gql', 'list'], // only invalidate list queries
|
|
239
|
+
})
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Disable Invalidation
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
const { mutate } = useEntityInsert(user, undefined, {
|
|
246
|
+
invalidate: false,
|
|
247
|
+
})
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Query Key Override
|
|
251
|
+
|
|
252
|
+
Override the auto-generated key on query hooks for fine-grained cache control:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
const { data } = useEntityList(user, params, {
|
|
256
|
+
queryKey: ['users', 'admin-list'],
|
|
257
|
+
})
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Type Inference Flow
|
|
261
|
+
|
|
262
|
+
Types flow end-to-end from your Drizzle schema to hook return types:
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
Drizzle Schema (tables + relations)
|
|
266
|
+
↓ InferEntityDefs
|
|
267
|
+
EntityDefs (fields, filters, inputs, orderBy per table)
|
|
268
|
+
↓ createDrizzleClient
|
|
269
|
+
GraphQLClient<SchemaDescriptor, EntityDefs>
|
|
270
|
+
↓ useEntity / client.entity()
|
|
271
|
+
EntityClient<EntityDefs, EntityDef>
|
|
272
|
+
↓ useEntityList / useEntityQuery (select param)
|
|
273
|
+
InferResult<EntityDefs, EntityDef, Select>
|
|
274
|
+
↓
|
|
275
|
+
Fully typed data: only selected fields, relations resolve to T[] or T | null
|
|
276
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { GraphQLProvider, useGraphQLClient } from './provider';
|
|
2
|
+
export type { GraphQLClientContext } from './types';
|
|
3
|
+
export { useEntity } from './useEntity';
|
|
4
|
+
export { useEntityInfiniteQuery } from './useEntityInfiniteQuery';
|
|
5
|
+
export { useEntityList } from './useEntityList';
|
|
6
|
+
export { useEntityDelete, useEntityInsert, useEntityUpdate } from './useEntityMutation';
|
|
7
|
+
export { useEntityQuery } from './useEntityQuery';
|