@constructive-io/graphql-query 3.2.5 → 3.3.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/README.md +411 -65
- package/ast.d.ts +4 -4
- package/ast.js +24 -9
- package/client/error.d.ts +95 -0
- package/client/error.js +277 -0
- package/client/execute.d.ts +57 -0
- package/client/execute.js +124 -0
- package/client/index.d.ts +8 -0
- package/client/index.js +20 -0
- package/client/typed-document.d.ts +31 -0
- package/client/typed-document.js +44 -0
- package/custom-ast.d.ts +22 -8
- package/custom-ast.js +16 -1
- package/esm/ast.js +22 -7
- package/esm/client/error.js +271 -0
- package/esm/client/execute.js +120 -0
- package/esm/client/index.js +8 -0
- package/esm/client/typed-document.js +40 -0
- package/esm/custom-ast.js +16 -1
- package/esm/generators/field-selector.js +381 -0
- package/esm/generators/index.js +13 -0
- package/esm/generators/mutations.js +200 -0
- package/esm/generators/naming-helpers.js +154 -0
- package/esm/generators/select.js +661 -0
- package/esm/index.js +30 -0
- package/esm/introspect/index.js +9 -0
- package/esm/introspect/infer-tables.js +697 -0
- package/esm/introspect/schema-query.js +120 -0
- package/esm/introspect/transform-schema.js +271 -0
- package/esm/introspect/transform.js +38 -0
- package/esm/meta-object/convert.js +3 -0
- package/esm/meta-object/format.json +11 -41
- package/esm/meta-object/validate.js +20 -4
- package/esm/query-builder.js +14 -18
- package/esm/types/index.js +18 -0
- package/esm/types/introspection.js +54 -0
- package/esm/types/mutation.js +4 -0
- package/esm/types/query.js +4 -0
- package/esm/types/schema.js +5 -0
- package/esm/types/selection.js +4 -0
- package/esm/utils.js +69 -0
- package/generators/field-selector.d.ts +30 -0
- package/generators/field-selector.js +387 -0
- package/generators/index.d.ts +9 -0
- package/generators/index.js +42 -0
- package/generators/mutations.d.ts +30 -0
- package/generators/mutations.js +238 -0
- package/generators/naming-helpers.d.ts +48 -0
- package/generators/naming-helpers.js +169 -0
- package/generators/select.d.ts +39 -0
- package/generators/select.js +705 -0
- package/index.d.ts +19 -0
- package/index.js +34 -1
- package/introspect/index.d.ts +9 -0
- package/introspect/index.js +25 -0
- package/introspect/infer-tables.d.ts +42 -0
- package/introspect/infer-tables.js +700 -0
- package/introspect/schema-query.d.ts +20 -0
- package/introspect/schema-query.js +123 -0
- package/introspect/transform-schema.d.ts +86 -0
- package/introspect/transform-schema.js +281 -0
- package/introspect/transform.d.ts +20 -0
- package/introspect/transform.js +43 -0
- package/meta-object/convert.d.ts +3 -0
- package/meta-object/convert.js +3 -0
- package/meta-object/format.json +11 -41
- package/meta-object/validate.d.ts +8 -3
- package/meta-object/validate.js +20 -4
- package/package.json +4 -3
- package/query-builder.d.ts +11 -12
- package/query-builder.js +25 -29
- package/{types.d.ts → types/core.d.ts} +25 -18
- package/types/index.d.ts +12 -0
- package/types/index.js +34 -0
- package/types/introspection.d.ts +121 -0
- package/types/introspection.js +62 -0
- package/types/mutation.d.ts +45 -0
- package/types/mutation.js +5 -0
- package/types/query.d.ts +91 -0
- package/types/query.js +5 -0
- package/types/schema.d.ts +265 -0
- package/types/schema.js +6 -0
- package/types/selection.d.ts +43 -0
- package/types/selection.js +5 -0
- package/utils.d.ts +17 -0
- package/utils.js +72 -0
- /package/esm/{types.js → types/core.js} +0 -0
- /package/{types.js → types/core.js} +0 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error handling for GraphQL operations
|
|
3
|
+
* Provides consistent error types and parsing for PostGraphile responses
|
|
4
|
+
*/
|
|
5
|
+
export declare const DataErrorType: {
|
|
6
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
7
|
+
readonly TIMEOUT_ERROR: "TIMEOUT_ERROR";
|
|
8
|
+
readonly VALIDATION_FAILED: "VALIDATION_FAILED";
|
|
9
|
+
readonly REQUIRED_FIELD_MISSING: "REQUIRED_FIELD_MISSING";
|
|
10
|
+
readonly INVALID_MUTATION_DATA: "INVALID_MUTATION_DATA";
|
|
11
|
+
readonly QUERY_GENERATION_FAILED: "QUERY_GENERATION_FAILED";
|
|
12
|
+
readonly QUERY_EXECUTION_FAILED: "QUERY_EXECUTION_FAILED";
|
|
13
|
+
readonly UNAUTHORIZED: "UNAUTHORIZED";
|
|
14
|
+
readonly FORBIDDEN: "FORBIDDEN";
|
|
15
|
+
readonly TABLE_NOT_FOUND: "TABLE_NOT_FOUND";
|
|
16
|
+
readonly BAD_REQUEST: "BAD_REQUEST";
|
|
17
|
+
readonly NOT_FOUND: "NOT_FOUND";
|
|
18
|
+
readonly GRAPHQL_ERROR: "GRAPHQL_ERROR";
|
|
19
|
+
readonly UNIQUE_VIOLATION: "UNIQUE_VIOLATION";
|
|
20
|
+
readonly FOREIGN_KEY_VIOLATION: "FOREIGN_KEY_VIOLATION";
|
|
21
|
+
readonly NOT_NULL_VIOLATION: "NOT_NULL_VIOLATION";
|
|
22
|
+
readonly CHECK_VIOLATION: "CHECK_VIOLATION";
|
|
23
|
+
readonly EXCLUSION_VIOLATION: "EXCLUSION_VIOLATION";
|
|
24
|
+
readonly UNKNOWN_ERROR: "UNKNOWN_ERROR";
|
|
25
|
+
};
|
|
26
|
+
export type DataErrorType = (typeof DataErrorType)[keyof typeof DataErrorType];
|
|
27
|
+
export interface DataErrorOptions {
|
|
28
|
+
tableName?: string;
|
|
29
|
+
fieldName?: string;
|
|
30
|
+
constraint?: string;
|
|
31
|
+
originalError?: Error;
|
|
32
|
+
code?: string;
|
|
33
|
+
context?: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Standard error class for data layer operations
|
|
37
|
+
*/
|
|
38
|
+
export declare class DataError extends Error {
|
|
39
|
+
readonly type: DataErrorType;
|
|
40
|
+
readonly code?: string;
|
|
41
|
+
readonly originalError?: Error;
|
|
42
|
+
readonly context?: Record<string, unknown>;
|
|
43
|
+
readonly tableName?: string;
|
|
44
|
+
readonly fieldName?: string;
|
|
45
|
+
readonly constraint?: string;
|
|
46
|
+
constructor(type: DataErrorType, message: string, options?: DataErrorOptions);
|
|
47
|
+
getUserMessage(): string;
|
|
48
|
+
isRetryable(): boolean;
|
|
49
|
+
}
|
|
50
|
+
export declare const PG_ERROR_CODES: {
|
|
51
|
+
readonly UNIQUE_VIOLATION: "23505";
|
|
52
|
+
readonly FOREIGN_KEY_VIOLATION: "23503";
|
|
53
|
+
readonly NOT_NULL_VIOLATION: "23502";
|
|
54
|
+
readonly CHECK_VIOLATION: "23514";
|
|
55
|
+
readonly EXCLUSION_VIOLATION: "23P01";
|
|
56
|
+
readonly NUMERIC_VALUE_OUT_OF_RANGE: "22003";
|
|
57
|
+
readonly STRING_DATA_RIGHT_TRUNCATION: "22001";
|
|
58
|
+
readonly INVALID_TEXT_REPRESENTATION: "22P02";
|
|
59
|
+
readonly DATETIME_FIELD_OVERFLOW: "22008";
|
|
60
|
+
readonly UNDEFINED_TABLE: "42P01";
|
|
61
|
+
readonly UNDEFINED_COLUMN: "42703";
|
|
62
|
+
readonly INSUFFICIENT_PRIVILEGE: "42501";
|
|
63
|
+
};
|
|
64
|
+
export declare const createError: {
|
|
65
|
+
network: (originalError?: Error) => DataError;
|
|
66
|
+
timeout: (originalError?: Error) => DataError;
|
|
67
|
+
unauthorized: (message?: string) => DataError;
|
|
68
|
+
forbidden: (message?: string) => DataError;
|
|
69
|
+
badRequest: (message: string, code?: string) => DataError;
|
|
70
|
+
notFound: (message?: string) => DataError;
|
|
71
|
+
graphql: (message: string, code?: string) => DataError;
|
|
72
|
+
uniqueViolation: (message: string, fieldName?: string, constraint?: string) => DataError;
|
|
73
|
+
foreignKeyViolation: (message: string, fieldName?: string, constraint?: string) => DataError;
|
|
74
|
+
notNullViolation: (message: string, fieldName?: string, constraint?: string) => DataError;
|
|
75
|
+
unknown: (originalError: Error) => DataError;
|
|
76
|
+
};
|
|
77
|
+
export interface GraphQLError {
|
|
78
|
+
message: string;
|
|
79
|
+
extensions?: {
|
|
80
|
+
code?: string;
|
|
81
|
+
} & Record<string, unknown>;
|
|
82
|
+
locations?: Array<{
|
|
83
|
+
line: number;
|
|
84
|
+
column: number;
|
|
85
|
+
}>;
|
|
86
|
+
path?: Array<string | number>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Parse any error into a DataError
|
|
90
|
+
*/
|
|
91
|
+
export declare function parseGraphQLError(error: unknown): DataError;
|
|
92
|
+
/**
|
|
93
|
+
* Check if value is a DataError
|
|
94
|
+
*/
|
|
95
|
+
export declare function isDataError(error: unknown): error is DataError;
|
package/client/error.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error handling for GraphQL operations
|
|
4
|
+
* Provides consistent error types and parsing for PostGraphile responses
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.createError = exports.PG_ERROR_CODES = exports.DataError = exports.DataErrorType = void 0;
|
|
8
|
+
exports.parseGraphQLError = parseGraphQLError;
|
|
9
|
+
exports.isDataError = isDataError;
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// Error Types
|
|
12
|
+
// ============================================================================
|
|
13
|
+
exports.DataErrorType = {
|
|
14
|
+
// Network/Connection errors
|
|
15
|
+
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
16
|
+
TIMEOUT_ERROR: 'TIMEOUT_ERROR',
|
|
17
|
+
// Validation errors
|
|
18
|
+
VALIDATION_FAILED: 'VALIDATION_FAILED',
|
|
19
|
+
REQUIRED_FIELD_MISSING: 'REQUIRED_FIELD_MISSING',
|
|
20
|
+
INVALID_MUTATION_DATA: 'INVALID_MUTATION_DATA',
|
|
21
|
+
// Query errors
|
|
22
|
+
QUERY_GENERATION_FAILED: 'QUERY_GENERATION_FAILED',
|
|
23
|
+
QUERY_EXECUTION_FAILED: 'QUERY_EXECUTION_FAILED',
|
|
24
|
+
// Permission errors
|
|
25
|
+
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
26
|
+
FORBIDDEN: 'FORBIDDEN',
|
|
27
|
+
// Schema errors
|
|
28
|
+
TABLE_NOT_FOUND: 'TABLE_NOT_FOUND',
|
|
29
|
+
// Request errors
|
|
30
|
+
BAD_REQUEST: 'BAD_REQUEST',
|
|
31
|
+
NOT_FOUND: 'NOT_FOUND',
|
|
32
|
+
// GraphQL-specific errors
|
|
33
|
+
GRAPHQL_ERROR: 'GRAPHQL_ERROR',
|
|
34
|
+
// PostgreSQL constraint errors (surfaced via PostGraphile)
|
|
35
|
+
UNIQUE_VIOLATION: 'UNIQUE_VIOLATION',
|
|
36
|
+
FOREIGN_KEY_VIOLATION: 'FOREIGN_KEY_VIOLATION',
|
|
37
|
+
NOT_NULL_VIOLATION: 'NOT_NULL_VIOLATION',
|
|
38
|
+
CHECK_VIOLATION: 'CHECK_VIOLATION',
|
|
39
|
+
EXCLUSION_VIOLATION: 'EXCLUSION_VIOLATION',
|
|
40
|
+
// Generic errors
|
|
41
|
+
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Standard error class for data layer operations
|
|
45
|
+
*/
|
|
46
|
+
class DataError extends Error {
|
|
47
|
+
type;
|
|
48
|
+
code;
|
|
49
|
+
originalError;
|
|
50
|
+
context;
|
|
51
|
+
tableName;
|
|
52
|
+
fieldName;
|
|
53
|
+
constraint;
|
|
54
|
+
constructor(type, message, options = {}) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.name = 'DataError';
|
|
57
|
+
this.type = type;
|
|
58
|
+
this.code = options.code;
|
|
59
|
+
this.originalError = options.originalError;
|
|
60
|
+
this.context = options.context;
|
|
61
|
+
this.tableName = options.tableName;
|
|
62
|
+
this.fieldName = options.fieldName;
|
|
63
|
+
this.constraint = options.constraint;
|
|
64
|
+
if (Error.captureStackTrace) {
|
|
65
|
+
Error.captureStackTrace(this, DataError);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
getUserMessage() {
|
|
69
|
+
switch (this.type) {
|
|
70
|
+
case exports.DataErrorType.NETWORK_ERROR:
|
|
71
|
+
return 'Network error. Please check your connection and try again.';
|
|
72
|
+
case exports.DataErrorType.TIMEOUT_ERROR:
|
|
73
|
+
return 'Request timed out. Please try again.';
|
|
74
|
+
case exports.DataErrorType.UNAUTHORIZED:
|
|
75
|
+
return 'You are not authorized. Please log in and try again.';
|
|
76
|
+
case exports.DataErrorType.FORBIDDEN:
|
|
77
|
+
return 'You do not have permission to access this resource.';
|
|
78
|
+
case exports.DataErrorType.VALIDATION_FAILED:
|
|
79
|
+
return 'Validation failed. Please check your input and try again.';
|
|
80
|
+
case exports.DataErrorType.REQUIRED_FIELD_MISSING:
|
|
81
|
+
return this.fieldName
|
|
82
|
+
? `The field "${this.fieldName}" is required.`
|
|
83
|
+
: 'A required field is missing.';
|
|
84
|
+
case exports.DataErrorType.UNIQUE_VIOLATION:
|
|
85
|
+
return this.fieldName
|
|
86
|
+
? `A record with this ${this.fieldName} already exists.`
|
|
87
|
+
: 'A record with this value already exists.';
|
|
88
|
+
case exports.DataErrorType.FOREIGN_KEY_VIOLATION:
|
|
89
|
+
return 'This record references a record that does not exist.';
|
|
90
|
+
case exports.DataErrorType.NOT_NULL_VIOLATION:
|
|
91
|
+
return this.fieldName
|
|
92
|
+
? `The field "${this.fieldName}" cannot be empty.`
|
|
93
|
+
: 'A required field cannot be empty.';
|
|
94
|
+
case exports.DataErrorType.CHECK_VIOLATION:
|
|
95
|
+
return this.fieldName
|
|
96
|
+
? `The value for "${this.fieldName}" is not valid.`
|
|
97
|
+
: 'The value does not meet the required constraints.';
|
|
98
|
+
default:
|
|
99
|
+
return this.message || 'An unexpected error occurred.';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
isRetryable() {
|
|
103
|
+
return (this.type === exports.DataErrorType.NETWORK_ERROR ||
|
|
104
|
+
this.type === exports.DataErrorType.TIMEOUT_ERROR);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.DataError = DataError;
|
|
108
|
+
// ============================================================================
|
|
109
|
+
// PostgreSQL Error Codes
|
|
110
|
+
// ============================================================================
|
|
111
|
+
exports.PG_ERROR_CODES = {
|
|
112
|
+
UNIQUE_VIOLATION: '23505',
|
|
113
|
+
FOREIGN_KEY_VIOLATION: '23503',
|
|
114
|
+
NOT_NULL_VIOLATION: '23502',
|
|
115
|
+
CHECK_VIOLATION: '23514',
|
|
116
|
+
EXCLUSION_VIOLATION: '23P01',
|
|
117
|
+
NUMERIC_VALUE_OUT_OF_RANGE: '22003',
|
|
118
|
+
STRING_DATA_RIGHT_TRUNCATION: '22001',
|
|
119
|
+
INVALID_TEXT_REPRESENTATION: '22P02',
|
|
120
|
+
DATETIME_FIELD_OVERFLOW: '22008',
|
|
121
|
+
UNDEFINED_TABLE: '42P01',
|
|
122
|
+
UNDEFINED_COLUMN: '42703',
|
|
123
|
+
INSUFFICIENT_PRIVILEGE: '42501',
|
|
124
|
+
};
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// Error Factory
|
|
127
|
+
// ============================================================================
|
|
128
|
+
exports.createError = {
|
|
129
|
+
network: (originalError) => new DataError(exports.DataErrorType.NETWORK_ERROR, 'Network error occurred', {
|
|
130
|
+
originalError,
|
|
131
|
+
}),
|
|
132
|
+
timeout: (originalError) => new DataError(exports.DataErrorType.TIMEOUT_ERROR, 'Request timed out', {
|
|
133
|
+
originalError,
|
|
134
|
+
}),
|
|
135
|
+
unauthorized: (message = 'Authentication required') => new DataError(exports.DataErrorType.UNAUTHORIZED, message),
|
|
136
|
+
forbidden: (message = 'Access forbidden') => new DataError(exports.DataErrorType.FORBIDDEN, message),
|
|
137
|
+
badRequest: (message, code) => new DataError(exports.DataErrorType.BAD_REQUEST, message, { code }),
|
|
138
|
+
notFound: (message = 'Resource not found') => new DataError(exports.DataErrorType.NOT_FOUND, message),
|
|
139
|
+
graphql: (message, code) => new DataError(exports.DataErrorType.GRAPHQL_ERROR, message, { code }),
|
|
140
|
+
uniqueViolation: (message, fieldName, constraint) => new DataError(exports.DataErrorType.UNIQUE_VIOLATION, message, {
|
|
141
|
+
fieldName,
|
|
142
|
+
constraint,
|
|
143
|
+
code: '23505',
|
|
144
|
+
}),
|
|
145
|
+
foreignKeyViolation: (message, fieldName, constraint) => new DataError(exports.DataErrorType.FOREIGN_KEY_VIOLATION, message, {
|
|
146
|
+
fieldName,
|
|
147
|
+
constraint,
|
|
148
|
+
code: '23503',
|
|
149
|
+
}),
|
|
150
|
+
notNullViolation: (message, fieldName, constraint) => new DataError(exports.DataErrorType.NOT_NULL_VIOLATION, message, {
|
|
151
|
+
fieldName,
|
|
152
|
+
constraint,
|
|
153
|
+
code: '23502',
|
|
154
|
+
}),
|
|
155
|
+
unknown: (originalError) => new DataError(exports.DataErrorType.UNKNOWN_ERROR, originalError.message, {
|
|
156
|
+
originalError,
|
|
157
|
+
}),
|
|
158
|
+
};
|
|
159
|
+
function parseGraphQLErrorCode(code) {
|
|
160
|
+
if (!code)
|
|
161
|
+
return exports.DataErrorType.UNKNOWN_ERROR;
|
|
162
|
+
const normalized = code.toUpperCase();
|
|
163
|
+
// GraphQL standard codes
|
|
164
|
+
if (normalized === 'UNAUTHENTICATED')
|
|
165
|
+
return exports.DataErrorType.UNAUTHORIZED;
|
|
166
|
+
if (normalized === 'FORBIDDEN')
|
|
167
|
+
return exports.DataErrorType.FORBIDDEN;
|
|
168
|
+
if (normalized === 'GRAPHQL_VALIDATION_FAILED')
|
|
169
|
+
return exports.DataErrorType.QUERY_GENERATION_FAILED;
|
|
170
|
+
// PostgreSQL SQLSTATE codes
|
|
171
|
+
if (code === exports.PG_ERROR_CODES.UNIQUE_VIOLATION)
|
|
172
|
+
return exports.DataErrorType.UNIQUE_VIOLATION;
|
|
173
|
+
if (code === exports.PG_ERROR_CODES.FOREIGN_KEY_VIOLATION)
|
|
174
|
+
return exports.DataErrorType.FOREIGN_KEY_VIOLATION;
|
|
175
|
+
if (code === exports.PG_ERROR_CODES.NOT_NULL_VIOLATION)
|
|
176
|
+
return exports.DataErrorType.NOT_NULL_VIOLATION;
|
|
177
|
+
if (code === exports.PG_ERROR_CODES.CHECK_VIOLATION)
|
|
178
|
+
return exports.DataErrorType.CHECK_VIOLATION;
|
|
179
|
+
if (code === exports.PG_ERROR_CODES.EXCLUSION_VIOLATION)
|
|
180
|
+
return exports.DataErrorType.EXCLUSION_VIOLATION;
|
|
181
|
+
return exports.DataErrorType.UNKNOWN_ERROR;
|
|
182
|
+
}
|
|
183
|
+
function classifyByMessage(message) {
|
|
184
|
+
const lower = message.toLowerCase();
|
|
185
|
+
if (lower.includes('timeout') || lower.includes('timed out')) {
|
|
186
|
+
return exports.DataErrorType.TIMEOUT_ERROR;
|
|
187
|
+
}
|
|
188
|
+
if (lower.includes('network') ||
|
|
189
|
+
lower.includes('fetch') ||
|
|
190
|
+
lower.includes('failed to fetch')) {
|
|
191
|
+
return exports.DataErrorType.NETWORK_ERROR;
|
|
192
|
+
}
|
|
193
|
+
if (lower.includes('unauthorized') ||
|
|
194
|
+
lower.includes('authentication required')) {
|
|
195
|
+
return exports.DataErrorType.UNAUTHORIZED;
|
|
196
|
+
}
|
|
197
|
+
if (lower.includes('forbidden') || lower.includes('permission')) {
|
|
198
|
+
return exports.DataErrorType.FORBIDDEN;
|
|
199
|
+
}
|
|
200
|
+
if (lower.includes('duplicate key') || lower.includes('already exists')) {
|
|
201
|
+
return exports.DataErrorType.UNIQUE_VIOLATION;
|
|
202
|
+
}
|
|
203
|
+
if (lower.includes('foreign key constraint')) {
|
|
204
|
+
return exports.DataErrorType.FOREIGN_KEY_VIOLATION;
|
|
205
|
+
}
|
|
206
|
+
if (lower.includes('not-null constraint') ||
|
|
207
|
+
lower.includes('null value in column')) {
|
|
208
|
+
return exports.DataErrorType.NOT_NULL_VIOLATION;
|
|
209
|
+
}
|
|
210
|
+
return exports.DataErrorType.UNKNOWN_ERROR;
|
|
211
|
+
}
|
|
212
|
+
function extractFieldFromError(message, constraint, column) {
|
|
213
|
+
if (column)
|
|
214
|
+
return column;
|
|
215
|
+
const columnMatch = message.match(/column\s+"?([a-z_][a-z0-9_]*)"?/i);
|
|
216
|
+
if (columnMatch)
|
|
217
|
+
return columnMatch[1];
|
|
218
|
+
if (constraint) {
|
|
219
|
+
const constraintMatch = constraint.match(/_([a-z_][a-z0-9_]*)_(?:key|fkey|check|pkey)$/i);
|
|
220
|
+
if (constraintMatch)
|
|
221
|
+
return constraintMatch[1];
|
|
222
|
+
}
|
|
223
|
+
const keyMatch = message.match(/Key\s+\(([a-z_][a-z0-9_]*)\)/i);
|
|
224
|
+
if (keyMatch)
|
|
225
|
+
return keyMatch[1];
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Parse any error into a DataError
|
|
230
|
+
*/
|
|
231
|
+
function parseGraphQLError(error) {
|
|
232
|
+
if (error instanceof DataError) {
|
|
233
|
+
return error;
|
|
234
|
+
}
|
|
235
|
+
// GraphQL error object
|
|
236
|
+
if (error &&
|
|
237
|
+
typeof error === 'object' &&
|
|
238
|
+
'message' in error &&
|
|
239
|
+
typeof error.message === 'string') {
|
|
240
|
+
const gqlError = error;
|
|
241
|
+
const extCode = gqlError.extensions?.code;
|
|
242
|
+
const mappedType = parseGraphQLErrorCode(extCode);
|
|
243
|
+
const column = gqlError.extensions?.column;
|
|
244
|
+
const constraint = gqlError.extensions?.constraint;
|
|
245
|
+
const fieldName = extractFieldFromError(gqlError.message, constraint, column);
|
|
246
|
+
if (mappedType !== exports.DataErrorType.UNKNOWN_ERROR) {
|
|
247
|
+
return new DataError(mappedType, gqlError.message, {
|
|
248
|
+
code: extCode,
|
|
249
|
+
fieldName,
|
|
250
|
+
constraint,
|
|
251
|
+
context: gqlError.extensions,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// Fallback: classify by message
|
|
255
|
+
const fallbackType = classifyByMessage(gqlError.message);
|
|
256
|
+
return new DataError(fallbackType, gqlError.message, {
|
|
257
|
+
code: extCode,
|
|
258
|
+
fieldName,
|
|
259
|
+
constraint,
|
|
260
|
+
context: gqlError.extensions,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
// Standard Error
|
|
264
|
+
if (error instanceof Error) {
|
|
265
|
+
const type = classifyByMessage(error.message);
|
|
266
|
+
return new DataError(type, error.message, { originalError: error });
|
|
267
|
+
}
|
|
268
|
+
// Unknown
|
|
269
|
+
const message = typeof error === 'string' ? error : 'Unknown error occurred';
|
|
270
|
+
return new DataError(exports.DataErrorType.UNKNOWN_ERROR, message);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Check if value is a DataError
|
|
274
|
+
*/
|
|
275
|
+
function isDataError(error) {
|
|
276
|
+
return error instanceof DataError;
|
|
277
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GraphQL execution utilities
|
|
3
|
+
*/
|
|
4
|
+
import type { DocumentNode } from 'graphql';
|
|
5
|
+
import { TypedDocumentString } from './typed-document';
|
|
6
|
+
type ExecutableDocument = TypedDocumentString<unknown, unknown> | DocumentNode | string;
|
|
7
|
+
type ResultOf<TDocument> = TDocument extends TypedDocumentString<infer TResult, unknown> ? TResult : unknown;
|
|
8
|
+
type VariablesOf<TDocument> = TDocument extends TypedDocumentString<unknown, infer TVariables> ? TVariables : Record<string, unknown>;
|
|
9
|
+
export interface ExecuteOptions {
|
|
10
|
+
/** Custom headers to include */
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
/** Request timeout in milliseconds */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/** Signal for request cancellation */
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
export interface GraphQLResponse<T = unknown> {
|
|
18
|
+
data?: T;
|
|
19
|
+
errors?: Array<{
|
|
20
|
+
message: string;
|
|
21
|
+
extensions?: {
|
|
22
|
+
code?: string;
|
|
23
|
+
} & Record<string, unknown>;
|
|
24
|
+
locations?: Array<{
|
|
25
|
+
line: number;
|
|
26
|
+
column: number;
|
|
27
|
+
}>;
|
|
28
|
+
path?: Array<string | number>;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Execute a GraphQL operation against an endpoint
|
|
33
|
+
*/
|
|
34
|
+
export declare function execute<TDocument extends ExecutableDocument>(endpoint: string, document: TDocument, variables?: VariablesOf<TDocument>, options?: ExecuteOptions): Promise<ResultOf<TDocument>>;
|
|
35
|
+
export interface GraphQLClientOptions {
|
|
36
|
+
/** GraphQL endpoint URL */
|
|
37
|
+
endpoint: string;
|
|
38
|
+
/** Default headers to include with every request */
|
|
39
|
+
headers?: Record<string, string>;
|
|
40
|
+
/** Default timeout in milliseconds */
|
|
41
|
+
timeout?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a GraphQL client instance
|
|
45
|
+
*/
|
|
46
|
+
export declare function createGraphQLClient(options: GraphQLClientOptions): {
|
|
47
|
+
/**
|
|
48
|
+
* Execute a GraphQL operation
|
|
49
|
+
*/
|
|
50
|
+
execute<TDocument extends ExecutableDocument>(document: TDocument, variables?: VariablesOf<TDocument>, options?: ExecuteOptions): Promise<ResultOf<TDocument>>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the endpoint URL
|
|
53
|
+
*/
|
|
54
|
+
getEndpoint(): string;
|
|
55
|
+
};
|
|
56
|
+
export type GraphQLClient = ReturnType<typeof createGraphQLClient>;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GraphQL execution utilities
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.execute = execute;
|
|
7
|
+
exports.createGraphQLClient = createGraphQLClient;
|
|
8
|
+
const graphql_1 = require("graphql");
|
|
9
|
+
const error_1 = require("./error");
|
|
10
|
+
const typed_document_1 = require("./typed-document");
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Helpers
|
|
13
|
+
// ============================================================================
|
|
14
|
+
function documentToString(document) {
|
|
15
|
+
if (typeof document === 'string')
|
|
16
|
+
return document;
|
|
17
|
+
if (document instanceof typed_document_1.TypedDocumentString)
|
|
18
|
+
return document.toString();
|
|
19
|
+
// DocumentNode
|
|
20
|
+
if (document && typeof document === 'object' && 'kind' in document) {
|
|
21
|
+
return (0, graphql_1.print)(document);
|
|
22
|
+
}
|
|
23
|
+
throw error_1.createError.badRequest('Invalid GraphQL document');
|
|
24
|
+
}
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Execute Function
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/**
|
|
29
|
+
* Execute a GraphQL operation against an endpoint
|
|
30
|
+
*/
|
|
31
|
+
async function execute(endpoint, document, variables, options = {}) {
|
|
32
|
+
const { headers = {}, timeout = 30000, signal } = options;
|
|
33
|
+
// Create timeout controller
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
36
|
+
// Combine signals if provided
|
|
37
|
+
const combinedSignal = signal
|
|
38
|
+
? AbortSignal.any([signal, controller.signal])
|
|
39
|
+
: controller.signal;
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(endpoint, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
Accept: 'application/graphql-response+json, application/json',
|
|
46
|
+
...headers,
|
|
47
|
+
},
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
query: documentToString(document),
|
|
50
|
+
...(variables !== undefined && { variables }),
|
|
51
|
+
}),
|
|
52
|
+
signal: combinedSignal,
|
|
53
|
+
});
|
|
54
|
+
clearTimeout(timeoutId);
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw await handleHttpError(response);
|
|
57
|
+
}
|
|
58
|
+
const result = await response.json();
|
|
59
|
+
if (result.errors?.length) {
|
|
60
|
+
throw (0, error_1.parseGraphQLError)(result.errors[0]);
|
|
61
|
+
}
|
|
62
|
+
return result.data;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
clearTimeout(timeoutId);
|
|
66
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
67
|
+
throw error_1.createError.timeout();
|
|
68
|
+
}
|
|
69
|
+
if (error instanceof Error && error.message.includes('fetch')) {
|
|
70
|
+
throw error_1.createError.network(error);
|
|
71
|
+
}
|
|
72
|
+
throw (0, error_1.parseGraphQLError)(error);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function handleHttpError(response) {
|
|
76
|
+
const { status, statusText } = response;
|
|
77
|
+
if (status === 401) {
|
|
78
|
+
return error_1.createError.unauthorized('Authentication required');
|
|
79
|
+
}
|
|
80
|
+
if (status === 403) {
|
|
81
|
+
return error_1.createError.forbidden('Access forbidden');
|
|
82
|
+
}
|
|
83
|
+
if (status === 404) {
|
|
84
|
+
return error_1.createError.notFound('GraphQL endpoint not found');
|
|
85
|
+
}
|
|
86
|
+
// Try to extract error from response body
|
|
87
|
+
try {
|
|
88
|
+
const body = await response.json();
|
|
89
|
+
if (body.errors?.length) {
|
|
90
|
+
return (0, error_1.parseGraphQLError)(body.errors[0]);
|
|
91
|
+
}
|
|
92
|
+
if (body.message) {
|
|
93
|
+
return error_1.createError.badRequest(body.message);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Couldn't parse response
|
|
98
|
+
}
|
|
99
|
+
return error_1.createError.badRequest(`Request failed: ${status} ${statusText}`);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Create a GraphQL client instance
|
|
103
|
+
*/
|
|
104
|
+
function createGraphQLClient(options) {
|
|
105
|
+
const { endpoint, headers: defaultHeaders = {}, timeout: defaultTimeout = 30000, } = options;
|
|
106
|
+
return {
|
|
107
|
+
/**
|
|
108
|
+
* Execute a GraphQL operation
|
|
109
|
+
*/
|
|
110
|
+
async execute(document, variables, options = {}) {
|
|
111
|
+
return execute(endpoint, document, variables, {
|
|
112
|
+
headers: { ...defaultHeaders, ...options.headers },
|
|
113
|
+
timeout: options.timeout ?? defaultTimeout,
|
|
114
|
+
signal: options.signal,
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* Get the endpoint URL
|
|
119
|
+
*/
|
|
120
|
+
getEndpoint() {
|
|
121
|
+
return endpoint;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client barrel export
|
|
3
|
+
*
|
|
4
|
+
* Re-exports client utilities for GraphQL execution and error handling.
|
|
5
|
+
*/
|
|
6
|
+
export { TypedDocumentString, type DocumentTypeDecoration, } from './typed-document';
|
|
7
|
+
export { DataError, DataErrorType, PG_ERROR_CODES, createError, parseGraphQLError, isDataError, type DataErrorOptions, type GraphQLError, } from './error';
|
|
8
|
+
export { execute, createGraphQLClient, type ExecuteOptions, type GraphQLResponse, type GraphQLClientOptions, type GraphQLClient, } from './execute';
|
package/client/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Client barrel export
|
|
4
|
+
*
|
|
5
|
+
* Re-exports client utilities for GraphQL execution and error handling.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.createGraphQLClient = exports.execute = exports.isDataError = exports.parseGraphQLError = exports.createError = exports.PG_ERROR_CODES = exports.DataErrorType = exports.DataError = exports.TypedDocumentString = void 0;
|
|
9
|
+
var typed_document_1 = require("./typed-document");
|
|
10
|
+
Object.defineProperty(exports, "TypedDocumentString", { enumerable: true, get: function () { return typed_document_1.TypedDocumentString; } });
|
|
11
|
+
var error_1 = require("./error");
|
|
12
|
+
Object.defineProperty(exports, "DataError", { enumerable: true, get: function () { return error_1.DataError; } });
|
|
13
|
+
Object.defineProperty(exports, "DataErrorType", { enumerable: true, get: function () { return error_1.DataErrorType; } });
|
|
14
|
+
Object.defineProperty(exports, "PG_ERROR_CODES", { enumerable: true, get: function () { return error_1.PG_ERROR_CODES; } });
|
|
15
|
+
Object.defineProperty(exports, "createError", { enumerable: true, get: function () { return error_1.createError; } });
|
|
16
|
+
Object.defineProperty(exports, "parseGraphQLError", { enumerable: true, get: function () { return error_1.parseGraphQLError; } });
|
|
17
|
+
Object.defineProperty(exports, "isDataError", { enumerable: true, get: function () { return error_1.isDataError; } });
|
|
18
|
+
var execute_1 = require("./execute");
|
|
19
|
+
Object.defineProperty(exports, "execute", { enumerable: true, get: function () { return execute_1.execute; } });
|
|
20
|
+
Object.defineProperty(exports, "createGraphQLClient", { enumerable: true, get: function () { return execute_1.createGraphQLClient; } });
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypedDocumentString - Type-safe wrapper for GraphQL documents
|
|
3
|
+
* Compatible with GraphQL codegen client preset
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Document type decoration interface (from @graphql-typed-document-node/core)
|
|
7
|
+
*/
|
|
8
|
+
export interface DocumentTypeDecoration<TResult, TVariables> {
|
|
9
|
+
/**
|
|
10
|
+
* This type is used to ensure that the variables you pass in to the query are assignable to Variables
|
|
11
|
+
* and that the Result is assignable to whatever you pass your result to.
|
|
12
|
+
*/
|
|
13
|
+
__apiType?: (variables: TVariables) => TResult;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Enhanced TypedDocumentString with type inference capabilities
|
|
17
|
+
* Compatible with GraphQL codegen client preset
|
|
18
|
+
*/
|
|
19
|
+
export declare class TypedDocumentString<TResult = unknown, TVariables = unknown> extends String implements DocumentTypeDecoration<TResult, TVariables> {
|
|
20
|
+
/** Same shape as the codegen implementation for structural typing */
|
|
21
|
+
__apiType?: (variables: TVariables) => TResult;
|
|
22
|
+
__meta__?: Record<string, unknown>;
|
|
23
|
+
private value;
|
|
24
|
+
constructor(value: string, meta?: Record<string, unknown>);
|
|
25
|
+
private generateHash;
|
|
26
|
+
toString(): string;
|
|
27
|
+
/**
|
|
28
|
+
* Get the hash for caching purposes
|
|
29
|
+
*/
|
|
30
|
+
getHash(): string;
|
|
31
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TypedDocumentString - Type-safe wrapper for GraphQL documents
|
|
4
|
+
* Compatible with GraphQL codegen client preset
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.TypedDocumentString = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Enhanced TypedDocumentString with type inference capabilities
|
|
10
|
+
* Compatible with GraphQL codegen client preset
|
|
11
|
+
*/
|
|
12
|
+
class TypedDocumentString extends String {
|
|
13
|
+
/** Same shape as the codegen implementation for structural typing */
|
|
14
|
+
__apiType;
|
|
15
|
+
__meta__;
|
|
16
|
+
value;
|
|
17
|
+
constructor(value, meta) {
|
|
18
|
+
super(value);
|
|
19
|
+
this.value = value;
|
|
20
|
+
this.__meta__ = {
|
|
21
|
+
hash: this.generateHash(value),
|
|
22
|
+
...meta,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
generateHash(value) {
|
|
26
|
+
let hash = 0;
|
|
27
|
+
for (let i = 0; i < value.length; i++) {
|
|
28
|
+
const char = value.charCodeAt(i);
|
|
29
|
+
hash = (hash << 5) - hash + char;
|
|
30
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
31
|
+
}
|
|
32
|
+
return Math.abs(hash).toString(36);
|
|
33
|
+
}
|
|
34
|
+
toString() {
|
|
35
|
+
return this.value;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the hash for caching purposes
|
|
39
|
+
*/
|
|
40
|
+
getHash() {
|
|
41
|
+
return this.__meta__?.hash;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.TypedDocumentString = TypedDocumentString;
|