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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +126 -0
  3. package/package.json +76 -0
  4. package/packages/client/README.md +236 -0
  5. package/packages/client/dist/LICENSE +21 -0
  6. package/packages/client/dist/README.md +236 -0
  7. package/packages/client/dist/client.d.ts +19 -0
  8. package/packages/client/dist/entity.d.ts +57 -0
  9. package/packages/client/dist/errors.d.ts +19 -0
  10. package/packages/client/dist/index.d.ts +11 -0
  11. package/packages/client/dist/index.js +439 -0
  12. package/packages/client/dist/infer.d.ts +86 -0
  13. package/packages/client/dist/package.json +37 -0
  14. package/packages/client/dist/query-builder.d.ts +12 -0
  15. package/packages/client/dist/schema-builder.d.ts +15 -0
  16. package/packages/client/dist/types.d.ts +45 -0
  17. package/packages/query/README.md +276 -0
  18. package/packages/query/dist/LICENSE +21 -0
  19. package/packages/query/dist/README.md +276 -0
  20. package/packages/query/dist/index.d.ts +7 -0
  21. package/packages/query/dist/index.js +172 -0
  22. package/packages/query/dist/package.json +39 -0
  23. package/packages/query/dist/provider.d.ts +7 -0
  24. package/packages/query/dist/test-setup.d.ts +1 -0
  25. package/packages/query/dist/test-utils.d.ts +40 -0
  26. package/packages/query/dist/types.d.ts +4 -0
  27. package/packages/query/dist/useEntity.d.ts +2 -0
  28. package/packages/query/dist/useEntityInfiniteQuery.d.ts +26 -0
  29. package/packages/query/dist/useEntityList.d.ts +22 -0
  30. package/packages/query/dist/useEntityMutation.d.ts +41 -0
  31. package/packages/query/dist/useEntityQuery.d.ts +21 -0
  32. package/packages/schema/README.md +348 -0
  33. package/packages/schema/dist/LICENSE +21 -0
  34. package/packages/schema/dist/README.md +348 -0
  35. package/packages/schema/dist/adapters/pg.d.ts +10 -0
  36. package/packages/schema/dist/adapters/types.d.ts +11 -0
  37. package/packages/schema/dist/case-ops.d.ts +2 -0
  38. package/packages/schema/dist/codegen.d.ts +12 -0
  39. package/packages/schema/dist/data-mappers.d.ts +12 -0
  40. package/packages/schema/dist/graphql/scalars.d.ts +2 -0
  41. package/packages/schema/dist/graphql/type-builder.d.ts +7 -0
  42. package/packages/schema/dist/index.d.ts +26 -0
  43. package/packages/schema/dist/index.js +1812 -0
  44. package/packages/schema/dist/package.json +41 -0
  45. package/packages/schema/dist/schema-builder.d.ts +65 -0
  46. package/packages/schema/dist/types.d.ts +117 -0
package/LICENSE ADDED
@@ -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.
package/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # drizzle-graphql-suite
2
+
3
+ Auto-generated GraphQL CRUD, type-safe clients, and React Query hooks from Drizzle PostgreSQL schemas.
4
+
5
+ ## Overview
6
+
7
+ `drizzle-graphql-suite` is a three-layer toolkit that turns your Drizzle ORM schema into a fully working GraphQL API with end-to-end type safety:
8
+
9
+ 1. **Schema builder** — generates a complete GraphQL schema with CRUD operations, relation-level filtering, and per-operation hooks from Drizzle table definitions.
10
+ 2. **Client** — provides a type-safe GraphQL client that infers query/mutation types directly from your Drizzle schema, with full TypeScript support for filters, relations, and results.
11
+ 3. **React Query hooks** — wraps the client in TanStack React Query hooks for caching, pagination, and mutations with automatic cache invalidation.
12
+
13
+ Inspired by [`drizzle-graphql`](https://github.com/drizzle-team/drizzle-graphql), rewritten with significant improvements including relation-level filtering, hooks, count queries, configurable schema generation, and code generation.
14
+
15
+ ## Packages
16
+
17
+ | Subpath | Package | Description |
18
+ |---------|---------|-------------|
19
+ | `drizzle-graphql-suite/schema` | [`@drizzle-graphql-suite/schema`](packages/schema/README.md) | GraphQL schema builder with CRUD, filtering, hooks, and codegen |
20
+ | `drizzle-graphql-suite/client` | [`@drizzle-graphql-suite/client`](packages/client/README.md) | Type-safe GraphQL client with full Drizzle type inference |
21
+ | `drizzle-graphql-suite/query` | [`@drizzle-graphql-suite/query`](packages/query/README.md) | TanStack React Query hooks for the client |
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ bun add drizzle-graphql-suite
27
+ ```
28
+
29
+ ```bash
30
+ npm install drizzle-graphql-suite
31
+ ```
32
+
33
+ ## Peer Dependencies
34
+
35
+ Each subpath import has its own peer dependency requirements:
36
+
37
+ | Subpath | Peer Dependencies |
38
+ |---------|-------------------|
39
+ | `./schema` | `drizzle-orm` >=0.44.0, `graphql` >=16.3.0 |
40
+ | `./client` | `drizzle-orm` >=0.44.0 |
41
+ | `./query` | `react` >=18.0.0, `@tanstack/react-query` >=5.0.0 |
42
+
43
+ ## Quick Start
44
+
45
+ ### 1. Server — Build GraphQL Schema
46
+
47
+ ```ts
48
+ import { buildSchema } from 'drizzle-graphql-suite/schema'
49
+ import { createYoga } from 'graphql-yoga'
50
+ import { createServer } from 'node:http'
51
+ import { db } from './db'
52
+
53
+ const { schema } = buildSchema(db, {
54
+ tables: { exclude: ['session', 'verification'] },
55
+ hooks: {
56
+ user: {
57
+ query: {
58
+ before: async ({ context }) => {
59
+ if (!context.user) throw new Error('Unauthorized')
60
+ },
61
+ },
62
+ },
63
+ },
64
+ })
65
+
66
+ const yoga = createYoga({ schema })
67
+ const server = createServer(yoga)
68
+ server.listen(4000)
69
+ ```
70
+
71
+ ### 2. Client — Type-Safe Queries
72
+
73
+ ```ts
74
+ import { createDrizzleClient } from 'drizzle-graphql-suite/client'
75
+ import * as schema from './db/schema'
76
+
77
+ const client = createDrizzleClient({
78
+ schema,
79
+ config: { suffixes: { list: 's' } },
80
+ url: '/api/graphql',
81
+ })
82
+
83
+ const users = await client.entity('user').query({
84
+ select: {
85
+ id: true,
86
+ name: true,
87
+ posts: { id: true, title: true },
88
+ },
89
+ where: { name: { ilike: '%john%' } },
90
+ limit: 10,
91
+ })
92
+ ```
93
+
94
+ ### 3. React — Query Hooks
95
+
96
+ ```tsx
97
+ import { GraphQLProvider, useEntity, useEntityList } from 'drizzle-graphql-suite/query'
98
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
99
+
100
+ const queryClient = new QueryClient()
101
+
102
+ function App() {
103
+ return (
104
+ <QueryClientProvider client={queryClient}>
105
+ <GraphQLProvider client={graphqlClient}>
106
+ <UserList />
107
+ </GraphQLProvider>
108
+ </QueryClientProvider>
109
+ )
110
+ }
111
+
112
+ function UserList() {
113
+ const user = useEntity('user')
114
+ const { data, isLoading } = useEntityList(user, {
115
+ select: { id: true, name: true, email: true },
116
+ limit: 20,
117
+ })
118
+
119
+ if (isLoading) return <div>Loading...</div>
120
+ return <ul>{data?.map((u) => <li key={u.id}>{u.name}</li>)}</ul>
121
+ }
122
+ ```
123
+
124
+ ## License
125
+
126
+ MIT
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "drizzle-graphql-suite",
3
+ "version": "0.5.0",
4
+ "description": "Auto-generated GraphQL CRUD, type-safe clients, and React Query hooks from Drizzle PostgreSQL schemas",
5
+ "author": "https://github.com/dmythro",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "drizzle",
9
+ "graphql",
10
+ "orm",
11
+ "postgresql",
12
+ "postgres",
13
+ "typescript",
14
+ "crud",
15
+ "react",
16
+ "react-query",
17
+ "tanstack",
18
+ "type-safe",
19
+ "schema",
20
+ "api"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/annexare/drizzle-graphql-suite.git"
25
+ },
26
+ "homepage": "https://github.com/annexare/drizzle-graphql-suite#readme",
27
+ "bugs": {
28
+ "url": "https://github.com/annexare/drizzle-graphql-suite/issues"
29
+ },
30
+ "type": "module",
31
+ "files": [
32
+ "packages/schema/dist",
33
+ "packages/client/dist",
34
+ "packages/query/dist",
35
+ "LICENSE",
36
+ "README.md"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "exports": {
42
+ "./schema": {
43
+ "import": "./packages/schema/dist/index.js",
44
+ "types": "./packages/schema/dist/index.d.ts"
45
+ },
46
+ "./client": {
47
+ "import": "./packages/client/dist/index.js",
48
+ "types": "./packages/client/dist/index.d.ts"
49
+ },
50
+ "./query": {
51
+ "import": "./packages/query/dist/index.js",
52
+ "types": "./packages/query/dist/index.d.ts"
53
+ }
54
+ },
55
+ "workspaces": [
56
+ "packages/*"
57
+ ],
58
+ "scripts": {
59
+ "build": "bun run --filter '@drizzle-graphql-suite/schema' --filter '@drizzle-graphql-suite/client' build && bun run --filter '@drizzle-graphql-suite/query' build",
60
+ "check-types": "bun run --filter '*' check-types",
61
+ "test": "bun run --filter '*' test",
62
+ "lint": "bunx @biomejs/biome check --write .",
63
+ "lint:check": "bunx @biomejs/biome check .",
64
+ "check": "bun run check-types && bun run lint:check && bun run test",
65
+ "ci": "bun run check-types && bun run lint:check && bun run test",
66
+ "changeset": "changeset",
67
+ "version-packages": "changeset version",
68
+ "prepare-publish": "bun run scripts/prepare-publish.ts",
69
+ "release": "bun run build && bun run prepare-publish && changeset publish"
70
+ },
71
+ "devDependencies": {
72
+ "@biomejs/biome": "2.4.4",
73
+ "@changesets/cli": "2.29.8",
74
+ "typescript": "5.9.3"
75
+ }
76
+ }
@@ -0,0 +1,236 @@
1
+ # @drizzle-graphql-suite/client
2
+
3
+ Type-safe GraphQL client auto-generated from Drizzle schemas, with full TypeScript inference for queries, mutations, filters, and relations.
4
+
5
+ ## Quick Start
6
+
7
+ ### From Drizzle Schema (recommended)
8
+
9
+ Use `createDrizzleClient` to create a client that infers all types directly from your Drizzle schema module — no code generation needed.
10
+
11
+ ```ts
12
+ import { createDrizzleClient } from 'drizzle-graphql-suite/client'
13
+ import * as schema from './db/schema'
14
+
15
+ const client = createDrizzleClient({
16
+ schema,
17
+ config: {
18
+ suffixes: { list: 's' },
19
+ tables: { exclude: ['session', 'verification'] },
20
+ },
21
+ url: '/api/graphql',
22
+ headers: { Authorization: 'Bearer ...' },
23
+ })
24
+ ```
25
+
26
+ ### From Schema Descriptor
27
+
28
+ Use `createClient` with a codegen-generated schema descriptor when you want to decouple from the Drizzle schema at runtime (e.g., in a frontend bundle that shouldn't import server-side schema files).
29
+
30
+ ```ts
31
+ import { createClient } from 'drizzle-graphql-suite/client'
32
+ import { schema, type EntityDefs } from './generated/entity-defs'
33
+
34
+ const client = createClient<typeof schema, EntityDefs>({
35
+ schema,
36
+ url: '/api/graphql',
37
+ })
38
+ ```
39
+
40
+ ## EntityClient API
41
+
42
+ Access a typed entity client via `client.entity('name')`:
43
+
44
+ ```ts
45
+ const user = client.entity('user')
46
+ ```
47
+
48
+ Each entity client provides the following methods:
49
+
50
+ ### `query(params)`
51
+
52
+ Fetch a list of records with optional filtering, pagination, and ordering. Returns `T[]`.
53
+
54
+ ```ts
55
+ const users = await user.query({
56
+ select: {
57
+ id: true,
58
+ name: true,
59
+ email: true,
60
+ posts: {
61
+ id: true,
62
+ title: true,
63
+ comments: { id: true, body: true },
64
+ },
65
+ },
66
+ where: { email: { ilike: '%@example.com' } },
67
+ orderBy: { name: { direction: 'asc', priority: 1 } },
68
+ limit: 20,
69
+ offset: 0,
70
+ })
71
+ ```
72
+
73
+ ### `querySingle(params)`
74
+
75
+ Fetch a single record. Returns `T | null`.
76
+
77
+ ```ts
78
+ const found = await user.querySingle({
79
+ select: { id: true, name: true, email: true },
80
+ where: { id: { eq: 'some-uuid' } },
81
+ })
82
+ ```
83
+
84
+ ### `count(params?)`
85
+
86
+ Count matching records. Returns `number`.
87
+
88
+ ```ts
89
+ const total = await user.count({
90
+ where: { role: { eq: 'admin' } },
91
+ })
92
+ ```
93
+
94
+ ### `insert(params)`
95
+
96
+ Insert multiple records. Returns `T[]` of inserted rows.
97
+
98
+ ```ts
99
+ const created = await user.insert({
100
+ values: [
101
+ { name: 'Alice', email: 'alice@example.com' },
102
+ { name: 'Bob', email: 'bob@example.com' },
103
+ ],
104
+ returning: { id: true, name: true },
105
+ })
106
+ ```
107
+
108
+ ### `insertSingle(params)`
109
+
110
+ Insert a single record. Returns `T | null`.
111
+
112
+ ```ts
113
+ const created = await user.insertSingle({
114
+ values: { name: 'Alice', email: 'alice@example.com' },
115
+ returning: { id: true, name: true },
116
+ })
117
+ ```
118
+
119
+ ### `update(params)`
120
+
121
+ Update records matching a filter. Returns `T[]` of updated rows.
122
+
123
+ ```ts
124
+ const updated = await user.update({
125
+ set: { role: 'admin' },
126
+ where: { id: { eq: 'some-uuid' } },
127
+ returning: { id: true, role: true },
128
+ })
129
+ ```
130
+
131
+ ### `delete(params)`
132
+
133
+ Delete records matching a filter. Returns `T[]` of deleted rows.
134
+
135
+ ```ts
136
+ const deleted = await user.delete({
137
+ where: { deletedAt: { isNotNull: true } },
138
+ returning: { id: true },
139
+ })
140
+ ```
141
+
142
+ ## Schema Descriptor
143
+
144
+ A `SchemaDescriptor` is a runtime object mapping entity names to their operation names, fields, and relations. It tells the client how to build GraphQL queries.
145
+
146
+ ### `buildSchemaDescriptor(schema, config?)`
147
+
148
+ Builds a `SchemaDescriptor` from a Drizzle schema module. This is called internally by `createDrizzleClient`, but can be used directly if you need the descriptor.
149
+
150
+ ```ts
151
+ import { buildSchemaDescriptor } from 'drizzle-graphql-suite/client'
152
+ import * as schema from './db/schema'
153
+
154
+ const descriptor = buildSchemaDescriptor(schema, {
155
+ suffixes: { list: 's' },
156
+ tables: { exclude: ['session'] },
157
+ pruneRelations: { 'user.sessions': false },
158
+ })
159
+ ```
160
+
161
+ Config options mirror the schema package: `mutations`, `suffixes`, `tables.exclude`, and `pruneRelations`.
162
+
163
+ ## Type Inference
164
+
165
+ The client provides end-to-end type inference from Drizzle schema to query results:
166
+
167
+ ### `InferEntityDefs<TSchema, TConfig>`
168
+
169
+ Infers the complete entity type definitions from a Drizzle schema module, including fields (with Date → string wire conversion), relations, filters, insert inputs, update inputs, and orderBy types.
170
+
171
+ ```ts
172
+ import type { InferEntityDefs } from 'drizzle-graphql-suite/client'
173
+ import type * as schema from './db/schema'
174
+
175
+ type MyEntityDefs = InferEntityDefs<typeof schema, { tables: { exclude: ['session'] } }>
176
+ ```
177
+
178
+ ### `InferResult<TDefs, TEntity, TSelect>`
179
+
180
+ Infers the return type of a query from the `select` object. Only selected scalar fields and relations are included in the result type. Relations resolve to arrays or `T | null` based on their cardinality.
181
+
182
+ ### `SelectInput<TDefs, TEntity>`
183
+
184
+ Describes the valid shape of a `select` parameter — `true` for scalar fields, nested objects for relations.
185
+
186
+ ## Dynamic URL and Headers
187
+
188
+ Both `url` and `headers` support static values or functions (sync or async) that are called per-request:
189
+
190
+ ```ts
191
+ const client = createDrizzleClient({
192
+ schema,
193
+ config: {},
194
+ // Dynamic URL
195
+ url: () => `${getApiBase()}/graphql`,
196
+ // Async headers (e.g., refresh token)
197
+ headers: async () => ({
198
+ Authorization: `Bearer ${await getAccessToken()}`,
199
+ }),
200
+ })
201
+ ```
202
+
203
+ ## Error Handling
204
+
205
+ The client throws two error types:
206
+
207
+ ### `GraphQLClientError`
208
+
209
+ Thrown when the server returns GraphQL errors in the response body.
210
+
211
+ - **`errors`** — `Array<{ message, locations?, path?, extensions? }>` — individual GraphQL errors
212
+ - **`status`** — HTTP status code (usually `200`)
213
+ - **`message`** — concatenated error messages
214
+
215
+ ### `NetworkError`
216
+
217
+ Thrown when the HTTP request fails (network error or non-2xx status).
218
+
219
+ - **`status`** — HTTP status code (`0` for network failures)
220
+ - **`message`** — error description
221
+
222
+ ```ts
223
+ import { GraphQLClientError, NetworkError } from 'drizzle-graphql-suite/client'
224
+
225
+ try {
226
+ const users = await client.entity('user').query({
227
+ select: { id: true, name: true },
228
+ })
229
+ } catch (e) {
230
+ if (e instanceof GraphQLClientError) {
231
+ console.error('GraphQL errors:', e.errors)
232
+ } else if (e instanceof NetworkError) {
233
+ console.error('Network error:', e.status, e.message)
234
+ }
235
+ }
236
+ ```
@@ -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.