@constructive-io/graphql-codegen 4.6.0 → 4.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.
- package/client/error.d.ts +2 -93
- package/client/error.js +9 -273
- package/client/execute.d.ts +2 -55
- package/client/execute.js +5 -120
- package/client/typed-document.d.ts +2 -29
- package/client/typed-document.js +3 -39
- package/core/ast.d.ts +8 -10
- package/core/ast.js +17 -592
- package/core/custom-ast.d.ts +5 -33
- package/core/custom-ast.js +16 -203
- package/core/introspect/infer-tables.d.ts +2 -40
- package/core/introspect/infer-tables.js +4 -653
- package/core/introspect/schema-query.d.ts +3 -18
- package/core/introspect/schema-query.js +3 -118
- package/core/introspect/transform-schema.d.ts +2 -84
- package/core/introspect/transform-schema.js +14 -279
- package/core/introspect/transform.d.ts +2 -18
- package/core/introspect/transform.js +6 -39
- package/core/meta-object/convert.d.ts +2 -63
- package/core/meta-object/convert.js +4 -59
- package/core/meta-object/validate.d.ts +2 -7
- package/core/meta-object/validate.js +4 -30
- package/core/query-builder.d.ts +7 -46
- package/core/query-builder.js +8 -408
- package/core/types.d.ts +9 -139
- package/core/types.js +12 -26
- package/esm/client/error.d.ts +2 -93
- package/esm/client/error.js +2 -269
- package/esm/client/execute.d.ts +2 -55
- package/esm/client/execute.js +2 -118
- package/esm/client/typed-document.d.ts +2 -29
- package/esm/client/typed-document.js +2 -38
- package/esm/core/ast.d.ts +8 -10
- package/esm/core/ast.js +8 -550
- package/esm/core/custom-ast.d.ts +5 -33
- package/esm/core/custom-ast.js +5 -160
- package/esm/core/introspect/infer-tables.d.ts +2 -40
- package/esm/core/introspect/infer-tables.js +2 -652
- package/esm/core/introspect/schema-query.d.ts +3 -18
- package/esm/core/introspect/schema-query.js +2 -118
- package/esm/core/introspect/transform-schema.d.ts +2 -84
- package/esm/core/introspect/transform-schema.js +2 -269
- package/esm/core/introspect/transform.d.ts +2 -18
- package/esm/core/introspect/transform.js +2 -36
- package/esm/core/meta-object/convert.d.ts +2 -63
- package/esm/core/meta-object/convert.js +2 -58
- package/esm/core/meta-object/validate.d.ts +2 -7
- package/esm/core/meta-object/validate.js +2 -26
- package/esm/core/query-builder.d.ts +7 -46
- package/esm/core/query-builder.js +5 -373
- package/esm/core/types.d.ts +9 -139
- package/esm/core/types.js +9 -24
- package/esm/generators/field-selector.d.ts +5 -28
- package/esm/generators/field-selector.js +5 -354
- package/esm/generators/mutations.d.ts +5 -29
- package/esm/generators/mutations.js +5 -195
- package/esm/generators/naming-helpers.d.ts +6 -0
- package/esm/generators/naming-helpers.js +6 -0
- package/esm/generators/select.d.ts +6 -48
- package/esm/generators/select.js +6 -634
- package/esm/types/query.d.ts +9 -0
- package/esm/types/schema.d.ts +4 -0
- package/generators/field-selector.d.ts +5 -28
- package/generators/field-selector.js +12 -360
- package/generators/mutations.d.ts +5 -29
- package/generators/mutations.js +9 -231
- package/generators/naming-helpers.d.ts +6 -0
- package/generators/naming-helpers.js +20 -0
- package/generators/select.d.ts +6 -48
- package/generators/select.js +17 -677
- package/package.json +7 -6
- package/types/query.d.ts +9 -0
- package/types/schema.d.ts +4 -0
- package/core/meta-object/format.json +0 -93
- package/esm/core/meta-object/format.json +0 -93
package/core/types.d.ts
CHANGED
|
@@ -1,139 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
isNotNull: boolean;
|
|
11
|
-
isArray: boolean;
|
|
12
|
-
isArrayNotNull: boolean;
|
|
13
|
-
properties?: NestedProperties;
|
|
14
|
-
}
|
|
15
|
-
export interface QueryDefinition {
|
|
16
|
-
model: string;
|
|
17
|
-
qtype: 'getMany' | 'getOne' | 'mutation';
|
|
18
|
-
mutationType?: 'create' | 'patch' | 'delete';
|
|
19
|
-
selection: string[];
|
|
20
|
-
properties: Record<string, QueryProperty>;
|
|
21
|
-
}
|
|
22
|
-
export interface MutationDefinition extends QueryDefinition {
|
|
23
|
-
qtype: 'mutation';
|
|
24
|
-
mutationType: 'create' | 'patch' | 'delete';
|
|
25
|
-
}
|
|
26
|
-
export interface IntrospectionSchema {
|
|
27
|
-
[key: string]: QueryDefinition | MutationDefinition;
|
|
28
|
-
}
|
|
29
|
-
export interface MetaFieldType {
|
|
30
|
-
gqlType: string;
|
|
31
|
-
isArray: boolean;
|
|
32
|
-
modifier?: string | number | null;
|
|
33
|
-
pgAlias?: string | null;
|
|
34
|
-
pgType?: string | null;
|
|
35
|
-
subtype?: string | null;
|
|
36
|
-
typmod?: number | null;
|
|
37
|
-
}
|
|
38
|
-
export interface MetaField {
|
|
39
|
-
name: string;
|
|
40
|
-
type: MetaFieldType;
|
|
41
|
-
}
|
|
42
|
-
export interface MetaConstraint {
|
|
43
|
-
name: string;
|
|
44
|
-
type: MetaFieldType;
|
|
45
|
-
alias?: string;
|
|
46
|
-
}
|
|
47
|
-
export interface MetaForeignConstraint {
|
|
48
|
-
fromKey: MetaConstraint;
|
|
49
|
-
refTable: string;
|
|
50
|
-
toKey: MetaConstraint;
|
|
51
|
-
}
|
|
52
|
-
export interface MetaTable {
|
|
53
|
-
name: string;
|
|
54
|
-
fields: MetaField[];
|
|
55
|
-
primaryConstraints: MetaConstraint[];
|
|
56
|
-
uniqueConstraints: MetaConstraint[];
|
|
57
|
-
foreignConstraints: MetaForeignConstraint[];
|
|
58
|
-
}
|
|
59
|
-
export interface MetaObject {
|
|
60
|
-
tables: MetaTable[];
|
|
61
|
-
}
|
|
62
|
-
export type GraphQLVariableValue = string | number | boolean | null;
|
|
63
|
-
export interface GraphQLVariables {
|
|
64
|
-
[key: string]: GraphQLVariableValue | GraphQLVariableValue[] | GraphQLVariables | GraphQLVariables[];
|
|
65
|
-
}
|
|
66
|
-
export interface QueryFieldSelection {
|
|
67
|
-
name: string;
|
|
68
|
-
isObject: boolean;
|
|
69
|
-
fieldDefn?: MetaField | CleanField;
|
|
70
|
-
selection?: QueryFieldSelection[];
|
|
71
|
-
variables?: GraphQLVariables;
|
|
72
|
-
isBelongTo?: boolean;
|
|
73
|
-
}
|
|
74
|
-
export interface QuerySelectionOptions {
|
|
75
|
-
[fieldName: string]: boolean | {
|
|
76
|
-
select: Record<string, boolean>;
|
|
77
|
-
variables?: GraphQLVariables;
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
export interface QueryBuilderInstance {
|
|
81
|
-
_introspection: IntrospectionSchema;
|
|
82
|
-
_meta: MetaObject;
|
|
83
|
-
_edges?: boolean;
|
|
84
|
-
}
|
|
85
|
-
export interface ASTFunctionParams {
|
|
86
|
-
queryName: string;
|
|
87
|
-
operationName: string;
|
|
88
|
-
query: QueryDefinition;
|
|
89
|
-
selection: QueryFieldSelection[];
|
|
90
|
-
builder?: QueryBuilderInstance;
|
|
91
|
-
}
|
|
92
|
-
export interface MutationASTParams {
|
|
93
|
-
mutationName: string;
|
|
94
|
-
operationName: string;
|
|
95
|
-
mutation: MutationDefinition;
|
|
96
|
-
selection?: QueryFieldSelection[];
|
|
97
|
-
}
|
|
98
|
-
export interface QueryBuilderOptions {
|
|
99
|
-
meta: MetaObject;
|
|
100
|
-
introspection: IntrospectionSchema;
|
|
101
|
-
}
|
|
102
|
-
export interface QueryBuilderResult {
|
|
103
|
-
_hash: string;
|
|
104
|
-
_queryName: string;
|
|
105
|
-
_ast: DocumentNode;
|
|
106
|
-
}
|
|
107
|
-
export interface IQueryBuilder {
|
|
108
|
-
query(model: string): IQueryBuilder;
|
|
109
|
-
getMany(options?: {
|
|
110
|
-
select?: QuerySelectionOptions;
|
|
111
|
-
}): IQueryBuilder;
|
|
112
|
-
getOne(options?: {
|
|
113
|
-
select?: QuerySelectionOptions;
|
|
114
|
-
}): IQueryBuilder;
|
|
115
|
-
all(options?: {
|
|
116
|
-
select?: QuerySelectionOptions;
|
|
117
|
-
}): IQueryBuilder;
|
|
118
|
-
count(): IQueryBuilder;
|
|
119
|
-
create(options?: {
|
|
120
|
-
select?: QuerySelectionOptions;
|
|
121
|
-
}): IQueryBuilder;
|
|
122
|
-
update(options?: {
|
|
123
|
-
select?: QuerySelectionOptions;
|
|
124
|
-
}): IQueryBuilder;
|
|
125
|
-
delete(options?: {
|
|
126
|
-
select?: QuerySelectionOptions;
|
|
127
|
-
}): IQueryBuilder;
|
|
128
|
-
edges(useEdges: boolean): IQueryBuilder;
|
|
129
|
-
print(): QueryBuilderResult;
|
|
130
|
-
}
|
|
131
|
-
export interface ObjectArrayItem extends QueryProperty {
|
|
132
|
-
name: string;
|
|
133
|
-
key?: string;
|
|
134
|
-
}
|
|
135
|
-
export declare function isGraphQLVariableValue(value: unknown): value is GraphQLVariableValue;
|
|
136
|
-
export declare function isGraphQLVariables(obj: unknown): obj is GraphQLVariables;
|
|
137
|
-
export type StrictRecord<K extends PropertyKey, V> = Record<K, V> & {
|
|
138
|
-
[P in PropertyKey]: P extends K ? V : never;
|
|
139
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Re-export core types from @constructive-io/graphql-query.
|
|
3
|
+
*
|
|
4
|
+
* This file used to contain the canonical type definitions. They now live in
|
|
5
|
+
* the `graphql-query` package and are re-exported here for backward
|
|
6
|
+
* compatibility so existing codegen consumers continue to work unchanged.
|
|
7
|
+
*/
|
|
8
|
+
export { type ASTNode, type NestedProperties, type QueryProperty, type QueryDefinition, type MutationDefinition, type MetaFieldType, type MetaField, type MetaConstraint, type MetaForeignConstraint, type MetaTable, type MetaObject, type GraphQLVariableValue, type GraphQLVariables, type QueryFieldSelection, type QuerySelectionOptions, type QueryBuilderInstance, type ASTFunctionParams, type MutationASTParams, type QueryBuilderOptions, type QueryBuilderResult, type IQueryBuilder, type ObjectArrayItem, isGraphQLVariableValue, isGraphQLVariables, type StrictRecord, } from '@constructive-io/graphql-query';
|
|
9
|
+
export { type QueryIntrospectionSchema as IntrospectionSchema } from '@constructive-io/graphql-query';
|
package/core/types.js
CHANGED
|
@@ -1,28 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Re-export core types from @constructive-io/graphql-query.
|
|
4
|
+
*
|
|
5
|
+
* This file used to contain the canonical type definitions. They now live in
|
|
6
|
+
* the `graphql-query` package and are re-exported here for backward
|
|
7
|
+
* compatibility so existing codegen consumers continue to work unchanged.
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isGraphQLVariableValue =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
-
typeof value === 'string' ||
|
|
9
|
-
typeof value === 'number' ||
|
|
10
|
-
typeof value === 'boolean');
|
|
11
|
-
}
|
|
12
|
-
function isGraphQLVariables(obj) {
|
|
13
|
-
if (!obj || typeof obj !== 'object')
|
|
14
|
-
return false;
|
|
15
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
16
|
-
if (typeof key !== 'string')
|
|
17
|
-
return false;
|
|
18
|
-
if (Array.isArray(value)) {
|
|
19
|
-
if (!value.every((item) => isGraphQLVariableValue(item) || isGraphQLVariables(item))) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
else if (!isGraphQLVariableValue(value) && !isGraphQLVariables(value)) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
10
|
+
exports.isGraphQLVariables = exports.isGraphQLVariableValue = void 0;
|
|
11
|
+
// Re-export everything from the canonical source
|
|
12
|
+
var graphql_query_1 = require("@constructive-io/graphql-query");
|
|
13
|
+
Object.defineProperty(exports, "isGraphQLVariableValue", { enumerable: true, get: function () { return graphql_query_1.isGraphQLVariableValue; } });
|
|
14
|
+
Object.defineProperty(exports, "isGraphQLVariables", { enumerable: true, get: function () { return graphql_query_1.isGraphQLVariables; } });
|
package/esm/client/error.d.ts
CHANGED
|
@@ -1,95 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Provides consistent error types and parsing for PostGraphile responses
|
|
2
|
+
* Re-export error handling utilities from @constructive-io/graphql-query.
|
|
4
3
|
*/
|
|
5
|
-
export
|
|
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;
|
|
4
|
+
export { DataError, DataErrorType, PG_ERROR_CODES, createError, parseGraphQLError, isDataError, type DataErrorOptions, type GraphQLError, } from '@constructive-io/graphql-query';
|
package/esm/client/error.js
CHANGED
|
@@ -1,271 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Provides consistent error types and parsing for PostGraphile responses
|
|
2
|
+
* Re-export error handling utilities from @constructive-io/graphql-query.
|
|
4
3
|
*/
|
|
5
|
-
|
|
6
|
-
// Error Types
|
|
7
|
-
// ============================================================================
|
|
8
|
-
export const DataErrorType = {
|
|
9
|
-
// Network/Connection errors
|
|
10
|
-
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
11
|
-
TIMEOUT_ERROR: 'TIMEOUT_ERROR',
|
|
12
|
-
// Validation errors
|
|
13
|
-
VALIDATION_FAILED: 'VALIDATION_FAILED',
|
|
14
|
-
REQUIRED_FIELD_MISSING: 'REQUIRED_FIELD_MISSING',
|
|
15
|
-
INVALID_MUTATION_DATA: 'INVALID_MUTATION_DATA',
|
|
16
|
-
// Query errors
|
|
17
|
-
QUERY_GENERATION_FAILED: 'QUERY_GENERATION_FAILED',
|
|
18
|
-
QUERY_EXECUTION_FAILED: 'QUERY_EXECUTION_FAILED',
|
|
19
|
-
// Permission errors
|
|
20
|
-
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
21
|
-
FORBIDDEN: 'FORBIDDEN',
|
|
22
|
-
// Schema errors
|
|
23
|
-
TABLE_NOT_FOUND: 'TABLE_NOT_FOUND',
|
|
24
|
-
// Request errors
|
|
25
|
-
BAD_REQUEST: 'BAD_REQUEST',
|
|
26
|
-
NOT_FOUND: 'NOT_FOUND',
|
|
27
|
-
// GraphQL-specific errors
|
|
28
|
-
GRAPHQL_ERROR: 'GRAPHQL_ERROR',
|
|
29
|
-
// PostgreSQL constraint errors (surfaced via PostGraphile)
|
|
30
|
-
UNIQUE_VIOLATION: 'UNIQUE_VIOLATION',
|
|
31
|
-
FOREIGN_KEY_VIOLATION: 'FOREIGN_KEY_VIOLATION',
|
|
32
|
-
NOT_NULL_VIOLATION: 'NOT_NULL_VIOLATION',
|
|
33
|
-
CHECK_VIOLATION: 'CHECK_VIOLATION',
|
|
34
|
-
EXCLUSION_VIOLATION: 'EXCLUSION_VIOLATION',
|
|
35
|
-
// Generic errors
|
|
36
|
-
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Standard error class for data layer operations
|
|
40
|
-
*/
|
|
41
|
-
export class DataError extends Error {
|
|
42
|
-
type;
|
|
43
|
-
code;
|
|
44
|
-
originalError;
|
|
45
|
-
context;
|
|
46
|
-
tableName;
|
|
47
|
-
fieldName;
|
|
48
|
-
constraint;
|
|
49
|
-
constructor(type, message, options = {}) {
|
|
50
|
-
super(message);
|
|
51
|
-
this.name = 'DataError';
|
|
52
|
-
this.type = type;
|
|
53
|
-
this.code = options.code;
|
|
54
|
-
this.originalError = options.originalError;
|
|
55
|
-
this.context = options.context;
|
|
56
|
-
this.tableName = options.tableName;
|
|
57
|
-
this.fieldName = options.fieldName;
|
|
58
|
-
this.constraint = options.constraint;
|
|
59
|
-
if (Error.captureStackTrace) {
|
|
60
|
-
Error.captureStackTrace(this, DataError);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
getUserMessage() {
|
|
64
|
-
switch (this.type) {
|
|
65
|
-
case DataErrorType.NETWORK_ERROR:
|
|
66
|
-
return 'Network error. Please check your connection and try again.';
|
|
67
|
-
case DataErrorType.TIMEOUT_ERROR:
|
|
68
|
-
return 'Request timed out. Please try again.';
|
|
69
|
-
case DataErrorType.UNAUTHORIZED:
|
|
70
|
-
return 'You are not authorized. Please log in and try again.';
|
|
71
|
-
case DataErrorType.FORBIDDEN:
|
|
72
|
-
return 'You do not have permission to access this resource.';
|
|
73
|
-
case DataErrorType.VALIDATION_FAILED:
|
|
74
|
-
return 'Validation failed. Please check your input and try again.';
|
|
75
|
-
case DataErrorType.REQUIRED_FIELD_MISSING:
|
|
76
|
-
return this.fieldName
|
|
77
|
-
? `The field "${this.fieldName}" is required.`
|
|
78
|
-
: 'A required field is missing.';
|
|
79
|
-
case DataErrorType.UNIQUE_VIOLATION:
|
|
80
|
-
return this.fieldName
|
|
81
|
-
? `A record with this ${this.fieldName} already exists.`
|
|
82
|
-
: 'A record with this value already exists.';
|
|
83
|
-
case DataErrorType.FOREIGN_KEY_VIOLATION:
|
|
84
|
-
return 'This record references a record that does not exist.';
|
|
85
|
-
case DataErrorType.NOT_NULL_VIOLATION:
|
|
86
|
-
return this.fieldName
|
|
87
|
-
? `The field "${this.fieldName}" cannot be empty.`
|
|
88
|
-
: 'A required field cannot be empty.';
|
|
89
|
-
case DataErrorType.CHECK_VIOLATION:
|
|
90
|
-
return this.fieldName
|
|
91
|
-
? `The value for "${this.fieldName}" is not valid.`
|
|
92
|
-
: 'The value does not meet the required constraints.';
|
|
93
|
-
default:
|
|
94
|
-
return this.message || 'An unexpected error occurred.';
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
isRetryable() {
|
|
98
|
-
return (this.type === DataErrorType.NETWORK_ERROR ||
|
|
99
|
-
this.type === DataErrorType.TIMEOUT_ERROR);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
// ============================================================================
|
|
103
|
-
// PostgreSQL Error Codes
|
|
104
|
-
// ============================================================================
|
|
105
|
-
export const PG_ERROR_CODES = {
|
|
106
|
-
UNIQUE_VIOLATION: '23505',
|
|
107
|
-
FOREIGN_KEY_VIOLATION: '23503',
|
|
108
|
-
NOT_NULL_VIOLATION: '23502',
|
|
109
|
-
CHECK_VIOLATION: '23514',
|
|
110
|
-
EXCLUSION_VIOLATION: '23P01',
|
|
111
|
-
NUMERIC_VALUE_OUT_OF_RANGE: '22003',
|
|
112
|
-
STRING_DATA_RIGHT_TRUNCATION: '22001',
|
|
113
|
-
INVALID_TEXT_REPRESENTATION: '22P02',
|
|
114
|
-
DATETIME_FIELD_OVERFLOW: '22008',
|
|
115
|
-
UNDEFINED_TABLE: '42P01',
|
|
116
|
-
UNDEFINED_COLUMN: '42703',
|
|
117
|
-
INSUFFICIENT_PRIVILEGE: '42501',
|
|
118
|
-
};
|
|
119
|
-
// ============================================================================
|
|
120
|
-
// Error Factory
|
|
121
|
-
// ============================================================================
|
|
122
|
-
export const createError = {
|
|
123
|
-
network: (originalError) => new DataError(DataErrorType.NETWORK_ERROR, 'Network error occurred', {
|
|
124
|
-
originalError,
|
|
125
|
-
}),
|
|
126
|
-
timeout: (originalError) => new DataError(DataErrorType.TIMEOUT_ERROR, 'Request timed out', {
|
|
127
|
-
originalError,
|
|
128
|
-
}),
|
|
129
|
-
unauthorized: (message = 'Authentication required') => new DataError(DataErrorType.UNAUTHORIZED, message),
|
|
130
|
-
forbidden: (message = 'Access forbidden') => new DataError(DataErrorType.FORBIDDEN, message),
|
|
131
|
-
badRequest: (message, code) => new DataError(DataErrorType.BAD_REQUEST, message, { code }),
|
|
132
|
-
notFound: (message = 'Resource not found') => new DataError(DataErrorType.NOT_FOUND, message),
|
|
133
|
-
graphql: (message, code) => new DataError(DataErrorType.GRAPHQL_ERROR, message, { code }),
|
|
134
|
-
uniqueViolation: (message, fieldName, constraint) => new DataError(DataErrorType.UNIQUE_VIOLATION, message, {
|
|
135
|
-
fieldName,
|
|
136
|
-
constraint,
|
|
137
|
-
code: '23505',
|
|
138
|
-
}),
|
|
139
|
-
foreignKeyViolation: (message, fieldName, constraint) => new DataError(DataErrorType.FOREIGN_KEY_VIOLATION, message, {
|
|
140
|
-
fieldName,
|
|
141
|
-
constraint,
|
|
142
|
-
code: '23503',
|
|
143
|
-
}),
|
|
144
|
-
notNullViolation: (message, fieldName, constraint) => new DataError(DataErrorType.NOT_NULL_VIOLATION, message, {
|
|
145
|
-
fieldName,
|
|
146
|
-
constraint,
|
|
147
|
-
code: '23502',
|
|
148
|
-
}),
|
|
149
|
-
unknown: (originalError) => new DataError(DataErrorType.UNKNOWN_ERROR, originalError.message, {
|
|
150
|
-
originalError,
|
|
151
|
-
}),
|
|
152
|
-
};
|
|
153
|
-
function parseGraphQLErrorCode(code) {
|
|
154
|
-
if (!code)
|
|
155
|
-
return DataErrorType.UNKNOWN_ERROR;
|
|
156
|
-
const normalized = code.toUpperCase();
|
|
157
|
-
// GraphQL standard codes
|
|
158
|
-
if (normalized === 'UNAUTHENTICATED')
|
|
159
|
-
return DataErrorType.UNAUTHORIZED;
|
|
160
|
-
if (normalized === 'FORBIDDEN')
|
|
161
|
-
return DataErrorType.FORBIDDEN;
|
|
162
|
-
if (normalized === 'GRAPHQL_VALIDATION_FAILED')
|
|
163
|
-
return DataErrorType.QUERY_GENERATION_FAILED;
|
|
164
|
-
// PostgreSQL SQLSTATE codes
|
|
165
|
-
if (code === PG_ERROR_CODES.UNIQUE_VIOLATION)
|
|
166
|
-
return DataErrorType.UNIQUE_VIOLATION;
|
|
167
|
-
if (code === PG_ERROR_CODES.FOREIGN_KEY_VIOLATION)
|
|
168
|
-
return DataErrorType.FOREIGN_KEY_VIOLATION;
|
|
169
|
-
if (code === PG_ERROR_CODES.NOT_NULL_VIOLATION)
|
|
170
|
-
return DataErrorType.NOT_NULL_VIOLATION;
|
|
171
|
-
if (code === PG_ERROR_CODES.CHECK_VIOLATION)
|
|
172
|
-
return DataErrorType.CHECK_VIOLATION;
|
|
173
|
-
if (code === PG_ERROR_CODES.EXCLUSION_VIOLATION)
|
|
174
|
-
return DataErrorType.EXCLUSION_VIOLATION;
|
|
175
|
-
return DataErrorType.UNKNOWN_ERROR;
|
|
176
|
-
}
|
|
177
|
-
function classifyByMessage(message) {
|
|
178
|
-
const lower = message.toLowerCase();
|
|
179
|
-
if (lower.includes('timeout') || lower.includes('timed out')) {
|
|
180
|
-
return DataErrorType.TIMEOUT_ERROR;
|
|
181
|
-
}
|
|
182
|
-
if (lower.includes('network') ||
|
|
183
|
-
lower.includes('fetch') ||
|
|
184
|
-
lower.includes('failed to fetch')) {
|
|
185
|
-
return DataErrorType.NETWORK_ERROR;
|
|
186
|
-
}
|
|
187
|
-
if (lower.includes('unauthorized') ||
|
|
188
|
-
lower.includes('authentication required')) {
|
|
189
|
-
return DataErrorType.UNAUTHORIZED;
|
|
190
|
-
}
|
|
191
|
-
if (lower.includes('forbidden') || lower.includes('permission')) {
|
|
192
|
-
return DataErrorType.FORBIDDEN;
|
|
193
|
-
}
|
|
194
|
-
if (lower.includes('duplicate key') || lower.includes('already exists')) {
|
|
195
|
-
return DataErrorType.UNIQUE_VIOLATION;
|
|
196
|
-
}
|
|
197
|
-
if (lower.includes('foreign key constraint')) {
|
|
198
|
-
return DataErrorType.FOREIGN_KEY_VIOLATION;
|
|
199
|
-
}
|
|
200
|
-
if (lower.includes('not-null constraint') ||
|
|
201
|
-
lower.includes('null value in column')) {
|
|
202
|
-
return DataErrorType.NOT_NULL_VIOLATION;
|
|
203
|
-
}
|
|
204
|
-
return DataErrorType.UNKNOWN_ERROR;
|
|
205
|
-
}
|
|
206
|
-
function extractFieldFromError(message, constraint, column) {
|
|
207
|
-
if (column)
|
|
208
|
-
return column;
|
|
209
|
-
const columnMatch = message.match(/column\s+"?([a-z_][a-z0-9_]*)"?/i);
|
|
210
|
-
if (columnMatch)
|
|
211
|
-
return columnMatch[1];
|
|
212
|
-
if (constraint) {
|
|
213
|
-
const constraintMatch = constraint.match(/_([a-z_][a-z0-9_]*)_(?:key|fkey|check|pkey)$/i);
|
|
214
|
-
if (constraintMatch)
|
|
215
|
-
return constraintMatch[1];
|
|
216
|
-
}
|
|
217
|
-
const keyMatch = message.match(/Key\s+\(([a-z_][a-z0-9_]*)\)/i);
|
|
218
|
-
if (keyMatch)
|
|
219
|
-
return keyMatch[1];
|
|
220
|
-
return undefined;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Parse any error into a DataError
|
|
224
|
-
*/
|
|
225
|
-
export function parseGraphQLError(error) {
|
|
226
|
-
if (error instanceof DataError) {
|
|
227
|
-
return error;
|
|
228
|
-
}
|
|
229
|
-
// GraphQL error object
|
|
230
|
-
if (error &&
|
|
231
|
-
typeof error === 'object' &&
|
|
232
|
-
'message' in error &&
|
|
233
|
-
typeof error.message === 'string') {
|
|
234
|
-
const gqlError = error;
|
|
235
|
-
const extCode = gqlError.extensions?.code;
|
|
236
|
-
const mappedType = parseGraphQLErrorCode(extCode);
|
|
237
|
-
const column = gqlError.extensions?.column;
|
|
238
|
-
const constraint = gqlError.extensions?.constraint;
|
|
239
|
-
const fieldName = extractFieldFromError(gqlError.message, constraint, column);
|
|
240
|
-
if (mappedType !== DataErrorType.UNKNOWN_ERROR) {
|
|
241
|
-
return new DataError(mappedType, gqlError.message, {
|
|
242
|
-
code: extCode,
|
|
243
|
-
fieldName,
|
|
244
|
-
constraint,
|
|
245
|
-
context: gqlError.extensions,
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
// Fallback: classify by message
|
|
249
|
-
const fallbackType = classifyByMessage(gqlError.message);
|
|
250
|
-
return new DataError(fallbackType, gqlError.message, {
|
|
251
|
-
code: extCode,
|
|
252
|
-
fieldName,
|
|
253
|
-
constraint,
|
|
254
|
-
context: gqlError.extensions,
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
// Standard Error
|
|
258
|
-
if (error instanceof Error) {
|
|
259
|
-
const type = classifyByMessage(error.message);
|
|
260
|
-
return new DataError(type, error.message, { originalError: error });
|
|
261
|
-
}
|
|
262
|
-
// Unknown
|
|
263
|
-
const message = typeof error === 'string' ? error : 'Unknown error occurred';
|
|
264
|
-
return new DataError(DataErrorType.UNKNOWN_ERROR, message);
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Check if value is a DataError
|
|
268
|
-
*/
|
|
269
|
-
export function isDataError(error) {
|
|
270
|
-
return error instanceof DataError;
|
|
271
|
-
}
|
|
4
|
+
export { DataError, DataErrorType, PG_ERROR_CODES, createError, parseGraphQLError, isDataError, } from '@constructive-io/graphql-query';
|
package/esm/client/execute.d.ts
CHANGED
|
@@ -1,57 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* GraphQL execution utilities
|
|
2
|
+
* Re-export GraphQL execution utilities from @constructive-io/graphql-query.
|
|
3
3
|
*/
|
|
4
|
-
|
|
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 {};
|
|
4
|
+
export { execute, createGraphQLClient, type ExecuteOptions, type GraphQLResponse, type GraphQLClientOptions, type GraphQLClient, } from '@constructive-io/graphql-query';
|