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
@@ -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,19 @@
1
+ import { type EntityClient } from './entity';
2
+ import type { InferEntityDefs } from './infer';
3
+ import { type ClientSchemaConfig } from './schema-builder';
4
+ import type { AnyEntityDefs, ClientConfig, SchemaDescriptor } from './types';
5
+ export declare class GraphQLClient<TSchema extends SchemaDescriptor, TDefs extends AnyEntityDefs = AnyEntityDefs> {
6
+ private url;
7
+ private schema;
8
+ private headers?;
9
+ constructor(config: ClientConfig<TSchema>);
10
+ entity<TEntityName extends string & keyof TSchema & keyof TDefs>(entityName: TEntityName): EntityClient<TDefs, TDefs[TEntityName]>;
11
+ execute(query: string, variables?: Record<string, unknown>): Promise<Record<string, unknown>>;
12
+ }
13
+ export type DrizzleClientConfig<TSchema extends Record<string, unknown>, TConfig extends ClientSchemaConfig> = {
14
+ schema: TSchema;
15
+ config: TConfig;
16
+ url: string | (() => string);
17
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
18
+ };
19
+ export declare function createDrizzleClient<TSchema extends Record<string, unknown>, const TConfig extends ClientSchemaConfig>(options: DrizzleClientConfig<TSchema, TConfig>): GraphQLClient<SchemaDescriptor, InferEntityDefs<TSchema, TConfig>>;
@@ -0,0 +1,57 @@
1
+ import type { AnyEntityDefs, EntityDef, EntityDescriptor, InferResult, SchemaDescriptor } from './types';
2
+ export type EntityClient<TDefs extends AnyEntityDefs, TEntity extends EntityDef> = {
3
+ query<S extends Record<string, unknown>>(params: {
4
+ select: S;
5
+ where?: TEntity extends {
6
+ filters: infer F;
7
+ } ? F : never;
8
+ limit?: number;
9
+ offset?: number;
10
+ orderBy?: TEntity extends {
11
+ orderBy: infer O;
12
+ } ? O : never;
13
+ }): Promise<InferResult<TDefs, TEntity, S>[]>;
14
+ querySingle<S extends Record<string, unknown>>(params: {
15
+ select: S;
16
+ where?: TEntity extends {
17
+ filters: infer F;
18
+ } ? F : never;
19
+ offset?: number;
20
+ orderBy?: TEntity extends {
21
+ orderBy: infer O;
22
+ } ? O : never;
23
+ }): Promise<InferResult<TDefs, TEntity, S> | null>;
24
+ count(params?: {
25
+ where?: TEntity extends {
26
+ filters: infer F;
27
+ } ? F : never;
28
+ }): Promise<number>;
29
+ insert<S extends Record<string, unknown>>(params: {
30
+ values: TEntity extends {
31
+ insertInput: infer I;
32
+ } ? I[] : never;
33
+ returning?: S;
34
+ }): Promise<InferResult<TDefs, TEntity, S>[]>;
35
+ insertSingle<S extends Record<string, unknown>>(params: {
36
+ values: TEntity extends {
37
+ insertInput: infer I;
38
+ } ? I : never;
39
+ returning?: S;
40
+ }): Promise<InferResult<TDefs, TEntity, S> | null>;
41
+ update<S extends Record<string, unknown>>(params: {
42
+ set: TEntity extends {
43
+ updateInput: infer U;
44
+ } ? U : never;
45
+ where?: TEntity extends {
46
+ filters: infer F;
47
+ } ? F : never;
48
+ returning?: S;
49
+ }): Promise<InferResult<TDefs, TEntity, S>[]>;
50
+ delete<S extends Record<string, unknown>>(params: {
51
+ where?: TEntity extends {
52
+ filters: infer F;
53
+ } ? F : never;
54
+ returning?: S;
55
+ }): Promise<InferResult<TDefs, TEntity, S>[]>;
56
+ };
57
+ export declare function createEntityClient<TDefs extends AnyEntityDefs, TEntity extends EntityDef>(entityName: string, entityDef: EntityDescriptor, schema: SchemaDescriptor, executeGraphQL: (query: string, variables: Record<string, unknown>) => Promise<Record<string, unknown>>): EntityClient<TDefs, TEntity>;
@@ -0,0 +1,19 @@
1
+ export type GraphQLErrorLocation = {
2
+ line: number;
3
+ column: number;
4
+ };
5
+ export type GraphQLErrorEntry = {
6
+ message: string;
7
+ locations?: GraphQLErrorLocation[];
8
+ path?: (string | number)[];
9
+ extensions?: Record<string, unknown>;
10
+ };
11
+ export declare class GraphQLClientError extends Error {
12
+ readonly errors: GraphQLErrorEntry[];
13
+ readonly status: number;
14
+ constructor(errors: GraphQLErrorEntry[], status?: number);
15
+ }
16
+ export declare class NetworkError extends Error {
17
+ readonly status: number;
18
+ constructor(message: string, status: number);
19
+ }
@@ -0,0 +1,11 @@
1
+ import { GraphQLClient } from './client';
2
+ import type { AnyEntityDefs, ClientConfig, SchemaDescriptor } from './types';
3
+ export declare function createClient<TSchema extends SchemaDescriptor, TDefs extends AnyEntityDefs = AnyEntityDefs>(config: ClientConfig<TSchema>): GraphQLClient<TSchema, TDefs>;
4
+ export type { DrizzleClientConfig } from './client';
5
+ export { createDrizzleClient, GraphQLClient } from './client';
6
+ export type { EntityClient } from './entity';
7
+ export { GraphQLClientError, NetworkError } from './errors';
8
+ export type { InferEntityDefs } from './infer';
9
+ export type { ClientSchemaConfig } from './schema-builder';
10
+ export { buildSchemaDescriptor } from './schema-builder';
11
+ export type { AnyEntityDefs, ClientConfig, EntityDef, EntityDescriptor, InferResult, SchemaDescriptor, SelectInput, } from './types';