@drizzle-graphql-suite/client 0.6.0 → 0.7.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 (3) hide show
  1. package/README.md +2 -2
  2. package/index.js +24 -439
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -46,9 +46,9 @@ const client = createDrizzleClient({
46
46
  })
47
47
  ```
48
48
 
49
- ### From Schema Descriptor
49
+ ### From Schema Descriptor (separate-repo setups)
50
50
 
51
- 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).
51
+ Use `createClient` with a codegen-generated schema descriptor when the client is in a separate repository that can't import the Drizzle schema directly.
52
52
 
53
53
  ```ts
54
54
  import { createClient } from 'drizzle-graphql-suite/client'
package/index.js CHANGED
@@ -1,439 +1,24 @@
1
- // src/query-builder.ts
2
- var capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
3
- function buildSelectionSet(select, schema, entityDef, indent = 4) {
4
- const pad = " ".repeat(indent);
5
- const lines = [];
6
- for (const [key, value] of Object.entries(select)) {
7
- if (value === true) {
8
- lines.push(`${pad}${key}`);
9
- continue;
10
- }
11
- if (typeof value === "object" && value !== null) {
12
- const rel = entityDef.relations[key];
13
- if (!rel)
14
- continue;
15
- const targetDef = schema[rel.entity];
16
- if (!targetDef)
17
- continue;
18
- const inner = buildSelectionSet(value, schema, targetDef, indent + 2);
19
- lines.push(`${pad}${key} {`);
20
- lines.push(inner);
21
- lines.push(`${pad}}`);
22
- }
23
- }
24
- return lines.join(`
25
- `);
26
- }
27
- function getFilterTypeName(entityName) {
28
- return `${capitalize(entityName)}Filters`;
29
- }
30
- function getOrderByTypeName(entityName) {
31
- return `${capitalize(entityName)}OrderBy`;
32
- }
33
- function getInsertInputTypeName(entityName) {
34
- return `${capitalize(entityName)}InsertInput`;
35
- }
36
- function getUpdateInputTypeName(entityName) {
37
- return `${capitalize(entityName)}UpdateInput`;
38
- }
39
- function buildListQuery(entityName, entityDef, schema, select, hasWhere, hasOrderBy, hasLimit, hasOffset) {
40
- const opName = entityDef.queryListName;
41
- if (!opName)
42
- throw new Error(`Entity '${entityName}' has no list query`);
43
- const filterType = getFilterTypeName(entityName);
44
- const orderByType = getOrderByTypeName(entityName);
45
- const operationName = `${capitalize(opName)}Query`;
46
- const varDefs = [];
47
- const argPairs = [];
48
- if (hasWhere) {
49
- varDefs.push(`$where: ${filterType}`);
50
- argPairs.push("where: $where");
51
- }
52
- if (hasOrderBy) {
53
- varDefs.push(`$orderBy: ${orderByType}`);
54
- argPairs.push("orderBy: $orderBy");
55
- }
56
- if (hasLimit) {
57
- varDefs.push("$limit: Int");
58
- argPairs.push("limit: $limit");
59
- }
60
- if (hasOffset) {
61
- varDefs.push("$offset: Int");
62
- argPairs.push("offset: $offset");
63
- }
64
- const varDefsStr = varDefs.length ? `(${varDefs.join(", ")})` : "";
65
- const argsStr = argPairs.length ? `(${argPairs.join(", ")})` : "";
66
- const selection = buildSelectionSet(select, schema, entityDef);
67
- const query = `query ${operationName}${varDefsStr} {
68
- ${opName}${argsStr} {
69
- ${selection}
70
- }
71
- }`;
72
- return { query, variables: {}, operationName };
73
- }
74
- function buildSingleQuery(entityName, entityDef, schema, select, hasWhere, hasOrderBy, hasOffset) {
75
- const opName = entityDef.queryName;
76
- if (!opName)
77
- throw new Error(`Entity '${entityName}' has no single query`);
78
- const filterType = getFilterTypeName(entityName);
79
- const orderByType = getOrderByTypeName(entityName);
80
- const operationName = `${capitalize(opName)}SingleQuery`;
81
- const varDefs = [];
82
- const argPairs = [];
83
- if (hasWhere) {
84
- varDefs.push(`$where: ${filterType}`);
85
- argPairs.push("where: $where");
86
- }
87
- if (hasOrderBy) {
88
- varDefs.push(`$orderBy: ${orderByType}`);
89
- argPairs.push("orderBy: $orderBy");
90
- }
91
- if (hasOffset) {
92
- varDefs.push("$offset: Int");
93
- argPairs.push("offset: $offset");
94
- }
95
- const varDefsStr = varDefs.length ? `(${varDefs.join(", ")})` : "";
96
- const argsStr = argPairs.length ? `(${argPairs.join(", ")})` : "";
97
- const selection = buildSelectionSet(select, schema, entityDef);
98
- const query = `query ${operationName}${varDefsStr} {
99
- ${opName}${argsStr} {
100
- ${selection}
101
- }
102
- }`;
103
- return { query, variables: {}, operationName };
104
- }
105
- function buildCountQuery(entityName, entityDef, hasWhere) {
106
- const opName = entityDef.countName;
107
- if (!opName)
108
- throw new Error(`Entity '${entityName}' has no count query`);
109
- const filterType = getFilterTypeName(entityName);
110
- const operationName = `${capitalize(opName)}Query`;
111
- const varDefs = [];
112
- const argPairs = [];
113
- if (hasWhere) {
114
- varDefs.push(`$where: ${filterType}`);
115
- argPairs.push("where: $where");
116
- }
117
- const varDefsStr = varDefs.length ? `(${varDefs.join(", ")})` : "";
118
- const argsStr = argPairs.length ? `(${argPairs.join(", ")})` : "";
119
- const query = `query ${operationName}${varDefsStr} {
120
- ${opName}${argsStr}
121
- }`;
122
- return { query, variables: {}, operationName };
123
- }
124
- function buildInsertMutation(entityName, entityDef, schema, returning, isSingle) {
125
- const opName = isSingle ? entityDef.insertSingleName : entityDef.insertName;
126
- if (!opName)
127
- throw new Error(`Entity '${entityName}' has no ${isSingle ? "insertSingle" : "insert"} mutation`);
128
- const inputType = getInsertInputTypeName(entityName);
129
- const operationName = `${capitalize(opName)}Mutation`;
130
- const valuesType = isSingle ? `${inputType}!` : `[${inputType}!]!`;
131
- const varDefs = `($values: ${valuesType})`;
132
- const argsStr = "(values: $values)";
133
- let selectionBlock = "";
134
- if (returning) {
135
- const selection = buildSelectionSet(returning, schema, entityDef);
136
- selectionBlock = ` {
137
- ${selection}
138
- }`;
139
- }
140
- const query = `mutation ${operationName}${varDefs} {
141
- ${opName}${argsStr}${selectionBlock}
142
- }`;
143
- return { query, variables: {}, operationName };
144
- }
145
- function buildUpdateMutation(entityName, entityDef, schema, returning, hasWhere) {
146
- const opName = entityDef.updateName;
147
- if (!opName)
148
- throw new Error(`Entity '${entityName}' has no update mutation`);
149
- const updateType = getUpdateInputTypeName(entityName);
150
- const filterType = getFilterTypeName(entityName);
151
- const operationName = `${capitalize(opName)}Mutation`;
152
- const varDefs = [`$set: ${updateType}!`];
153
- const argPairs = ["set: $set"];
154
- if (hasWhere) {
155
- varDefs.push(`$where: ${filterType}`);
156
- argPairs.push("where: $where");
157
- }
158
- const varDefsStr = `(${varDefs.join(", ")})`;
159
- const argsStr = `(${argPairs.join(", ")})`;
160
- let selectionBlock = "";
161
- if (returning) {
162
- const selection = buildSelectionSet(returning, schema, entityDef);
163
- selectionBlock = ` {
164
- ${selection}
165
- }`;
166
- }
167
- const query = `mutation ${operationName}${varDefsStr} {
168
- ${opName}${argsStr}${selectionBlock}
169
- }`;
170
- return { query, variables: {}, operationName };
171
- }
172
- function buildDeleteMutation(entityName, entityDef, schema, returning, hasWhere) {
173
- const opName = entityDef.deleteName;
174
- if (!opName)
175
- throw new Error(`Entity '${entityName}' has no delete mutation`);
176
- const filterType = getFilterTypeName(entityName);
177
- const operationName = `${capitalize(opName)}Mutation`;
178
- const varDefs = [];
179
- const argPairs = [];
180
- if (hasWhere) {
181
- varDefs.push(`$where: ${filterType}`);
182
- argPairs.push("where: $where");
183
- }
184
- const varDefsStr = varDefs.length ? `(${varDefs.join(", ")})` : "";
185
- const argsStr = argPairs.length ? `(${argPairs.join(", ")})` : "";
186
- let selectionBlock = "";
187
- if (returning) {
188
- const selection = buildSelectionSet(returning, schema, entityDef);
189
- selectionBlock = ` {
190
- ${selection}
191
- }`;
192
- }
193
- const query = `mutation ${operationName}${varDefsStr} {
194
- ${opName}${argsStr}${selectionBlock}
195
- }`;
196
- return { query, variables: {}, operationName };
197
- }
198
-
199
- // src/entity.ts
200
- function createEntityClient(entityName, entityDef, schema, executeGraphQL) {
201
- return {
202
- async query(params) {
203
- const { select, where, limit, offset, orderBy } = params;
204
- const built = buildListQuery(entityName, entityDef, schema, select, where != null, orderBy != null, limit != null, offset != null);
205
- const variables = {};
206
- if (where != null)
207
- variables.where = where;
208
- if (orderBy != null)
209
- variables.orderBy = orderBy;
210
- if (limit != null)
211
- variables.limit = limit;
212
- if (offset != null)
213
- variables.offset = offset;
214
- const data = await executeGraphQL(built.query, variables);
215
- return data[entityDef.queryListName];
216
- },
217
- async querySingle(params) {
218
- const { select, where, offset, orderBy } = params;
219
- const built = buildSingleQuery(entityName, entityDef, schema, select, where != null, orderBy != null, offset != null);
220
- const variables = {};
221
- if (where != null)
222
- variables.where = where;
223
- if (orderBy != null)
224
- variables.orderBy = orderBy;
225
- if (offset != null)
226
- variables.offset = offset;
227
- const data = await executeGraphQL(built.query, variables);
228
- return data[entityDef.queryName] ?? null;
229
- },
230
- async count(params) {
231
- const where = params?.where;
232
- const built = buildCountQuery(entityName, entityDef, where != null);
233
- const variables = {};
234
- if (where != null)
235
- variables.where = where;
236
- const data = await executeGraphQL(built.query, variables);
237
- return data[entityDef.countName];
238
- },
239
- async insert(params) {
240
- const { values, returning } = params;
241
- const built = buildInsertMutation(entityName, entityDef, schema, returning, false);
242
- const variables = { values };
243
- const data = await executeGraphQL(built.query, variables);
244
- return data[entityDef.insertName];
245
- },
246
- async insertSingle(params) {
247
- const { values, returning } = params;
248
- const built = buildInsertMutation(entityName, entityDef, schema, returning, true);
249
- const variables = { values };
250
- const data = await executeGraphQL(built.query, variables);
251
- return data[entityDef.insertSingleName] ?? null;
252
- },
253
- async update(params) {
254
- const { set, where, returning } = params;
255
- const built = buildUpdateMutation(entityName, entityDef, schema, returning, where != null);
256
- const variables = { set };
257
- if (where != null)
258
- variables.where = where;
259
- const data = await executeGraphQL(built.query, variables);
260
- return data[entityDef.updateName];
261
- },
262
- async delete(params) {
263
- const { where, returning } = params;
264
- const built = buildDeleteMutation(entityName, entityDef, schema, returning, where != null);
265
- const variables = {};
266
- if (where != null)
267
- variables.where = where;
268
- const data = await executeGraphQL(built.query, variables);
269
- return data[entityDef.deleteName];
270
- }
271
- };
272
- }
273
-
274
- // src/errors.ts
275
- class GraphQLClientError extends Error {
276
- errors;
277
- status;
278
- constructor(errors, status = 200) {
279
- const message = errors.map((e) => e.message).join("; ");
280
- super(message);
281
- this.name = "GraphQLClientError";
282
- this.errors = errors;
283
- this.status = status;
284
- }
285
- }
286
-
287
- class NetworkError extends Error {
288
- status;
289
- constructor(message, status) {
290
- super(message);
291
- this.name = "NetworkError";
292
- this.status = status;
293
- }
294
- }
295
-
296
- // src/schema-builder.ts
297
- import { getTableColumns, getTableName, is, Many, One, Relations, Table } from "drizzle-orm";
298
- var capitalize2 = (s) => s.charAt(0).toUpperCase() + s.slice(1);
299
- function buildSchemaDescriptor(schema, config = {}) {
300
- const excludeSet = new Set(config.tables?.exclude ?? []);
301
- const listSuffix = config.suffixes?.list ?? "s";
302
- const tableMap = new Map;
303
- const dbNameToKey = new Map;
304
- for (const [key, value] of Object.entries(schema)) {
305
- if (is(value, Table)) {
306
- if (excludeSet.has(key))
307
- continue;
308
- const dbName = getTableName(value);
309
- const cols = Object.keys(getTableColumns(value));
310
- tableMap.set(key, { table: value, dbName, columns: cols });
311
- dbNameToKey.set(dbName, key);
312
- }
313
- }
314
- const relationsMap = new Map;
315
- for (const value of Object.values(schema)) {
316
- if (!is(value, Relations))
317
- continue;
318
- const sourceDbName = getTableName(value.table);
319
- const sourceKey = dbNameToKey.get(sourceDbName);
320
- if (!sourceKey || !tableMap.has(sourceKey))
321
- continue;
322
- const helpers = {
323
- one: (referencedTable, cfg) => {
324
- return new One(value.table, referencedTable, cfg, false);
325
- },
326
- many: (referencedTable, cfg) => {
327
- return new Many(value.table, referencedTable, cfg);
328
- }
329
- };
330
- const relConfig = value.config(helpers);
331
- const rels = {};
332
- for (const [relName, relValue] of Object.entries(relConfig)) {
333
- const rel = relValue;
334
- const targetDbName = rel.referencedTableName;
335
- if (!targetDbName)
336
- continue;
337
- const targetKey = dbNameToKey.get(targetDbName);
338
- if (!targetKey)
339
- continue;
340
- const type = is(rel, One) ? "one" : "many";
341
- rels[relName] = { entity: targetKey, type };
342
- }
343
- relationsMap.set(sourceKey, rels);
344
- }
345
- const pruneRelations = config.pruneRelations ?? {};
346
- for (const [sourceKey, rels] of relationsMap) {
347
- for (const relName of Object.keys(rels)) {
348
- const pruneKey = `${sourceKey}.${relName}`;
349
- const pruneValue = pruneRelations[pruneKey];
350
- if (pruneValue === false) {
351
- delete rels[relName];
352
- }
353
- }
354
- }
355
- const descriptor = {};
356
- for (const [key, { columns }] of tableMap) {
357
- const rels = relationsMap.get(key) ?? {};
358
- descriptor[key] = {
359
- queryName: key,
360
- queryListName: `${key}${listSuffix}`,
361
- countName: `${key}Count`,
362
- insertName: `insertInto${capitalize2(key)}`,
363
- insertSingleName: `insertInto${capitalize2(key)}Single`,
364
- updateName: `update${capitalize2(key)}`,
365
- deleteName: `deleteFrom${capitalize2(key)}`,
366
- fields: columns,
367
- relations: rels
368
- };
369
- }
370
- return descriptor;
371
- }
372
-
373
- // src/client.ts
374
- class GraphQLClient {
375
- url;
376
- schema;
377
- headers;
378
- constructor(config) {
379
- this.url = config.url;
380
- this.schema = config.schema;
381
- this.headers = config.headers;
382
- }
383
- entity(entityName) {
384
- const entityDef = this.schema[entityName];
385
- if (!entityDef) {
386
- throw new Error(`Entity '${entityName}' not found in schema`);
387
- }
388
- return createEntityClient(entityName, entityDef, this.schema, (query, variables) => this.execute(query, variables));
389
- }
390
- async execute(query, variables = {}) {
391
- const url = typeof this.url === "function" ? this.url() : this.url;
392
- const headers = {
393
- "Content-Type": "application/json",
394
- ...typeof this.headers === "function" ? await this.headers() : this.headers ?? {}
395
- };
396
- let response;
397
- try {
398
- response = await fetch(url, {
399
- method: "POST",
400
- headers,
401
- body: JSON.stringify({ query, variables })
402
- });
403
- } catch (e) {
404
- throw new NetworkError(e instanceof Error ? e.message : "Network request failed", 0);
405
- }
406
- if (!response.ok) {
407
- throw new NetworkError(`HTTP ${response.status}: ${response.statusText}`, response.status);
408
- }
409
- const json = await response.json();
410
- if (json.errors?.length) {
411
- throw new GraphQLClientError(json.errors, response.status);
412
- }
413
- if (!json.data) {
414
- throw new GraphQLClientError([{ message: "No data in response" }], response.status);
415
- }
416
- return json.data;
417
- }
418
- }
419
- function createDrizzleClient(options) {
420
- const schema = buildSchemaDescriptor(options.schema, options.config);
421
- return new GraphQLClient({
422
- url: options.url,
423
- schema,
424
- headers: options.headers
425
- });
426
- }
427
-
428
- // src/index.ts
429
- function createClient(config) {
430
- return new GraphQLClient(config);
431
- }
432
- export {
433
- createDrizzleClient,
434
- createClient,
435
- buildSchemaDescriptor,
436
- NetworkError,
437
- GraphQLClientError,
438
- GraphQLClient
439
- };
1
+ var j=(E)=>E.charAt(0).toUpperCase()+E.slice(1);function G(E,x,$,Y=4){let J=" ".repeat(Y),C=[];for(let[H,R]of Object.entries(E)){if(R===!0){C.push(`${J}${H}`);continue}if(typeof R==="object"&&R!==null){let X=$.relations[H];if(!X)continue;let A=x[X.entity];if(!A)continue;let Z=G(R,x,A,Y+2);C.push(`${J}${H} {`),C.push(Z),C.push(`${J}}`)}}return C.join(`
2
+ `)}function L(E){return`${j(E)}Filters`}function S(E){return`${j(E)}OrderBy`}function p(E){return`${j(E)}InsertInput`}function c(E){return`${j(E)}UpdateInput`}function T(E,x,$,Y,J,C,H,R){let X=x.queryListName;if(!X)throw Error(`Entity '${E}' has no list query`);let A=L(E),Z=S(E),_=`${j(X)}Query`,V=[],I=[];if(J)V.push(`$where: ${A}`),I.push("where: $where");if(C)V.push(`$orderBy: ${Z}`),I.push("orderBy: $orderBy");if(H)V.push("$limit: Int"),I.push("limit: $limit");if(R)V.push("$offset: Int"),I.push("offset: $offset");let q=V.length?`(${V.join(", ")})`:"",U=I.length?`(${I.join(", ")})`:"",F=G(Y,$,x);return{query:`query ${_}${q} {
3
+ ${X}${U} {
4
+ ${F}
5
+ }
6
+ }`,variables:{},operationName:_}}function d(E,x,$,Y,J,C,H){let R=x.queryName;if(!R)throw Error(`Entity '${E}' has no single query`);let X=L(E),A=S(E),Z=`${j(R)}SingleQuery`,_=[],V=[];if(J)_.push(`$where: ${X}`),V.push("where: $where");if(C)_.push(`$orderBy: ${A}`),V.push("orderBy: $orderBy");if(H)_.push("$offset: Int"),V.push("offset: $offset");let I=_.length?`(${_.join(", ")})`:"",q=V.length?`(${V.join(", ")})`:"",U=G(Y,$,x);return{query:`query ${Z}${I} {
7
+ ${R}${q} {
8
+ ${U}
9
+ }
10
+ }`,variables:{},operationName:Z}}function b(E,x,$){let Y=x.countName;if(!Y)throw Error(`Entity '${E}' has no count query`);let J=L(E),C=`${j(Y)}Query`,H=[],R=[];if($)H.push(`$where: ${J}`),R.push("where: $where");let X=H.length?`(${H.join(", ")})`:"",A=R.length?`(${R.join(", ")})`:"";return{query:`query ${C}${X} {
11
+ ${Y}${A}
12
+ }`,variables:{},operationName:C}}function w(E,x,$,Y,J){let C=J?x.insertSingleName:x.insertName;if(!C)throw Error(`Entity '${E}' has no ${J?"insertSingle":"insert"} mutation`);let H=p(E),R=`${j(C)}Mutation`,A=`($values: ${J?`${H}!`:`[${H}!]!`})`,Z="(values: $values)",_="";if(Y)_=` {
13
+ ${G(Y,$,x)}
14
+ }`;return{query:`mutation ${R}${A} {
15
+ ${C}${Z}${_}
16
+ }`,variables:{},operationName:R}}function v(E,x,$,Y,J){let C=x.updateName;if(!C)throw Error(`Entity '${E}' has no update mutation`);let H=c(E),R=L(E),X=`${j(C)}Mutation`,A=[`$set: ${H}!`],Z=["set: $set"];if(J)A.push(`$where: ${R}`),Z.push("where: $where");let _=`(${A.join(", ")})`,V=`(${Z.join(", ")})`,I="";if(Y)I=` {
17
+ ${G(Y,$,x)}
18
+ }`;return{query:`mutation ${X}${_} {
19
+ ${C}${V}${I}
20
+ }`,variables:{},operationName:X}}function g(E,x,$,Y,J){let C=x.deleteName;if(!C)throw Error(`Entity '${E}' has no delete mutation`);let H=L(E),R=`${j(C)}Mutation`,X=[],A=[];if(J)X.push(`$where: ${H}`),A.push("where: $where");let Z=X.length?`(${X.join(", ")})`:"",_=A.length?`(${A.join(", ")})`:"",V="";if(Y)V=` {
21
+ ${G(Y,$,x)}
22
+ }`;return{query:`mutation ${R}${Z} {
23
+ ${C}${_}${V}
24
+ }`,variables:{},operationName:R}}function D(E,x,$,Y){return{async query(J){let{select:C,where:H,limit:R,offset:X,orderBy:A}=J,Z=T(E,x,$,C,H!=null,A!=null,R!=null,X!=null),_={};if(H!=null)_.where=H;if(A!=null)_.orderBy=A;if(R!=null)_.limit=R;if(X!=null)_.offset=X;return(await Y(Z.query,_))[x.queryListName]},async querySingle(J){let{select:C,where:H,offset:R,orderBy:X}=J,A=d(E,x,$,C,H!=null,X!=null,R!=null),Z={};if(H!=null)Z.where=H;if(X!=null)Z.orderBy=X;if(R!=null)Z.offset=R;return(await Y(A.query,Z))[x.queryName]??null},async count(J){let C=J?.where,H=b(E,x,C!=null),R={};if(C!=null)R.where=C;return(await Y(H.query,R))[x.countName]},async insert(J){let{values:C,returning:H}=J,R=w(E,x,$,H,!1),X={values:C};return(await Y(R.query,X))[x.insertName]},async insertSingle(J){let{values:C,returning:H}=J,R=w(E,x,$,H,!0),X={values:C};return(await Y(R.query,X))[x.insertSingleName]??null},async update(J){let{set:C,where:H,returning:R}=J,X=v(E,x,$,R,H!=null),A={set:C};if(H!=null)A.where=H;return(await Y(X.query,A))[x.updateName]},async delete(J){let{where:C,returning:H}=J,R=g(E,x,$,H,C!=null),X={};if(C!=null)X.where=C;return(await Y(R.query,X))[x.deleteName]}}}class K extends Error{errors;status;constructor(E,x=200){let $=E.map((Y)=>Y.message).join("; ");super($);this.name="GraphQLClientError",this.errors=E,this.status=x}}class W extends Error{status;constructor(E,x){super(E);this.name="NetworkError",this.status=x}}import{getTableColumns as f,getTableName as h,is as z,Many as N,One as u,Relations as m,Table as l}from"drizzle-orm";var O=(E)=>E.charAt(0).toUpperCase()+E.slice(1);function k(E,x={}){let $=new Set(x.tables?.exclude??[]),Y=x.suffixes?.list??"s",J=new Map,C=new Map;for(let[A,Z]of Object.entries(E))if(z(Z,l)){if($.has(A))continue;let _=h(Z),V=Object.keys(f(Z));J.set(A,{table:Z,dbName:_,columns:V}),C.set(_,A)}let H=new Map;for(let A of Object.values(E)){if(!z(A,m))continue;let Z=h(A.table),_=C.get(Z);if(!_||!J.has(_))continue;let V={one:(U,F)=>{return new u(A.table,U,F,!1)},many:(U,F)=>{return new N(A.table,U,F)}},I=A.config(V),q={};for(let[U,F]of Object.entries(I)){let Q=F,P=Q.referencedTableName;if(!P)continue;let B=C.get(P);if(!B)continue;let o=z(Q,u)?"one":"many";q[U]={entity:B,type:o}}H.set(_,q)}let R=x.pruneRelations??{};for(let[A,Z]of H)for(let _ of Object.keys(Z)){let V=`${A}.${_}`;if(R[V]===!1)delete Z[_]}let X={};for(let[A,{columns:Z}]of J){let _=H.get(A)??{};X[A]={queryName:A,queryListName:`${A}${Y}`,countName:`${A}Count`,insertName:`insertInto${O(A)}`,insertSingleName:`insertInto${O(A)}Single`,updateName:`update${O(A)}`,deleteName:`deleteFrom${O(A)}`,fields:Z,relations:_}}return X}class M{url;schema;headers;constructor(E){this.url=E.url,this.schema=E.schema,this.headers=E.headers}entity(E){let x=this.schema[E];if(!x)throw Error(`Entity '${E}' not found in schema`);return D(E,x,this.schema,($,Y)=>this.execute($,Y))}async execute(E,x={}){let $=typeof this.url==="function"?this.url():this.url,Y={"Content-Type":"application/json",...typeof this.headers==="function"?await this.headers():this.headers??{}},J;try{J=await fetch($,{method:"POST",headers:Y,body:JSON.stringify({query:E,variables:x})})}catch(H){throw new W(H instanceof Error?H.message:"Network request failed",0)}if(!J.ok)throw new W(`HTTP ${J.status}: ${J.statusText}`,J.status);let C=await J.json();if(C.errors?.length)throw new K(C.errors,J.status);if(!C.data)throw new K([{message:"No data in response"}],J.status);return C.data}}function y(E){let x=k(E.schema,E.config);return new M({url:E.url,schema:x,headers:E.headers})}function HE(E){return new M(E)}export{y as createDrizzleClient,HE as createClient,k as buildSchemaDescriptor,W as NetworkError,K as GraphQLClientError,M as GraphQLClient};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drizzle-graphql-suite/client",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Type-safe GraphQL client with entity-based API and full Drizzle type inference",
5
5
  "license": "MIT",
6
6
  "author": "https://github.com/dmythro",