@constructive-io/graphql-codegen 3.2.0 → 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 +25 -30
- package/cli/index.js +36 -41
- package/cli/shared.d.ts +15 -17
- package/cli/shared.js +113 -21
- package/client/error.js +31 -9
- package/client/execute.js +2 -2
- package/client/index.d.ts +3 -3
- package/client/index.js +6 -6
- package/core/ast.d.ts +1 -1
- package/core/ast.js +1 -1
- package/core/codegen/babel-ast.d.ts +1 -1
- package/core/codegen/babel-ast.js +2 -2
- package/core/codegen/barrel.d.ts +0 -6
- package/core/codegen/barrel.js +22 -19
- package/core/codegen/client.d.ts +2 -12
- package/core/codegen/client.js +7 -21
- package/core/codegen/custom-mutations.d.ts +0 -14
- package/core/codegen/custom-mutations.js +139 -88
- package/core/codegen/custom-queries.d.ts +0 -14
- package/core/codegen/custom-queries.js +483 -193
- package/core/codegen/hooks-ast.d.ts +75 -0
- package/core/codegen/hooks-ast.js +522 -0
- package/core/codegen/index.d.ts +16 -18
- package/core/codegen/index.js +42 -88
- package/core/codegen/invalidation.d.ts +1 -7
- package/core/codegen/invalidation.js +50 -16
- package/core/codegen/mutation-keys.d.ts +1 -10
- package/core/codegen/mutation-keys.js +22 -8
- package/core/codegen/mutations.d.ts +0 -13
- package/core/codegen/mutations.js +301 -366
- package/core/codegen/orm/barrel.d.ts +0 -5
- package/core/codegen/orm/barrel.js +5 -0
- package/core/codegen/orm/client-generator.d.ts +0 -5
- package/core/codegen/orm/client-generator.js +7 -2
- package/core/codegen/orm/client.js +3 -1
- package/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/core/codegen/orm/custom-ops-generator.js +104 -51
- package/core/codegen/orm/index.d.ts +4 -4
- package/core/codegen/orm/index.js +28 -15
- package/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/core/codegen/orm/input-types-generator.js +85 -23
- package/core/codegen/orm/model-generator.d.ts +0 -5
- package/core/codegen/orm/model-generator.js +309 -131
- package/core/codegen/orm/select-types.d.ts +19 -14
- package/core/codegen/queries.d.ts +0 -8
- package/core/codegen/queries.js +360 -559
- package/core/codegen/query-keys.d.ts +1 -1
- package/core/codegen/query-keys.js +37 -23
- package/core/codegen/scalars.js +3 -1
- package/core/codegen/schema-types-generator.d.ts +1 -1
- package/core/codegen/schema-types-generator.js +17 -2
- package/core/codegen/select-helpers.d.ts +19 -0
- package/core/codegen/select-helpers.js +40 -0
- package/core/codegen/selection.d.ts +4 -0
- package/core/codegen/selection.js +65 -0
- package/core/codegen/shared/index.d.ts +2 -15
- package/core/codegen/shared/index.js +17 -4
- package/core/codegen/templates/hooks-client.ts +49 -0
- package/core/codegen/templates/hooks-selection.ts +58 -0
- package/core/codegen/templates/orm-client.ts +8 -6
- package/core/codegen/templates/query-builder.ts +250 -46
- package/core/codegen/templates/select-types.ts +31 -14
- package/core/codegen/type-resolver.d.ts +1 -5
- package/core/codegen/type-resolver.js +0 -22
- package/core/codegen/types.d.ts +0 -3
- package/core/codegen/types.js +71 -14
- package/core/codegen/utils.d.ts +1 -4
- package/core/codegen/utils.js +4 -1
- package/core/config/index.d.ts +1 -1
- package/core/config/resolver.js +1 -3
- package/core/generate.js +38 -50
- package/core/index.d.ts +3 -3
- package/core/index.js +3 -4
- package/core/introspect/index.d.ts +6 -6
- package/core/introspect/index.js +5 -8
- package/core/introspect/infer-tables.d.ts +0 -14
- package/core/introspect/infer-tables.js +15 -1
- package/core/introspect/source/database.js +1 -1
- package/core/introspect/source/endpoint.d.ts +0 -6
- package/core/introspect/source/endpoint.js +7 -1
- package/core/introspect/source/index.d.ts +4 -4
- package/core/introspect/source/index.js +5 -9
- package/core/introspect/source/pgpm-module.js +3 -3
- package/core/introspect/transform-schema.d.ts +2 -2
- package/core/introspect/transform-schema.js +2 -2
- package/core/output/index.d.ts +1 -1
- package/core/output/index.js +2 -2
- package/core/output/writer.d.ts +3 -0
- package/core/output/writer.js +20 -1
- package/core/pipeline/index.d.ts +2 -2
- package/core/query-builder.d.ts +2 -2
- package/core/query-builder.js +1 -1
- package/core/watch/index.d.ts +4 -4
- package/core/watch/index.js +9 -9
- package/core/watch/orchestrator.js +5 -3
- package/esm/cli/index.js +37 -42
- package/esm/cli/shared.d.ts +15 -17
- package/esm/cli/shared.js +103 -20
- package/esm/client/error.js +31 -9
- package/esm/client/execute.js +2 -2
- package/esm/client/index.d.ts +3 -3
- package/esm/client/index.js +3 -3
- package/esm/core/ast.d.ts +1 -1
- package/esm/core/ast.js +1 -1
- package/esm/core/codegen/babel-ast.d.ts +1 -1
- package/esm/core/codegen/babel-ast.js +2 -2
- package/esm/core/codegen/barrel.d.ts +0 -6
- package/esm/core/codegen/barrel.js +23 -20
- package/esm/core/codegen/client.d.ts +2 -12
- package/esm/core/codegen/client.js +7 -21
- package/esm/core/codegen/custom-mutations.d.ts +0 -14
- package/esm/core/codegen/custom-mutations.js +141 -90
- package/esm/core/codegen/custom-queries.d.ts +0 -14
- package/esm/core/codegen/custom-queries.js +486 -196
- package/esm/core/codegen/hooks-ast.d.ts +75 -0
- package/esm/core/codegen/hooks-ast.js +424 -0
- package/esm/core/codegen/index.d.ts +16 -18
- package/esm/core/codegen/index.js +26 -71
- package/esm/core/codegen/invalidation.d.ts +1 -7
- package/esm/core/codegen/invalidation.js +51 -17
- package/esm/core/codegen/mutation-keys.d.ts +1 -10
- package/esm/core/codegen/mutation-keys.js +23 -9
- package/esm/core/codegen/mutations.d.ts +0 -13
- package/esm/core/codegen/mutations.js +302 -367
- package/esm/core/codegen/orm/barrel.d.ts +0 -5
- package/esm/core/codegen/orm/barrel.js +6 -1
- package/esm/core/codegen/orm/client-generator.d.ts +0 -5
- package/esm/core/codegen/orm/client-generator.js +7 -2
- package/esm/core/codegen/orm/client.js +3 -1
- package/esm/core/codegen/orm/custom-ops-generator.d.ts +0 -6
- package/esm/core/codegen/orm/custom-ops-generator.js +103 -50
- package/esm/core/codegen/orm/index.d.ts +4 -4
- package/esm/core/codegen/orm/index.js +25 -12
- package/esm/core/codegen/orm/input-types-generator.d.ts +1 -13
- package/esm/core/codegen/orm/input-types-generator.js +85 -23
- package/esm/core/codegen/orm/model-generator.d.ts +0 -5
- package/esm/core/codegen/orm/model-generator.js +310 -132
- package/esm/core/codegen/orm/select-types.d.ts +19 -14
- package/esm/core/codegen/queries.d.ts +0 -8
- package/esm/core/codegen/queries.js +362 -561
- package/esm/core/codegen/query-keys.d.ts +1 -1
- package/esm/core/codegen/query-keys.js +38 -24
- package/esm/core/codegen/scalars.js +3 -1
- package/esm/core/codegen/schema-types-generator.d.ts +1 -1
- package/esm/core/codegen/schema-types-generator.js +17 -2
- package/esm/core/codegen/select-helpers.d.ts +19 -0
- package/esm/core/codegen/select-helpers.js +35 -0
- package/esm/core/codegen/selection.d.ts +4 -0
- package/esm/core/codegen/selection.js +29 -0
- package/esm/core/codegen/shared/index.d.ts +2 -15
- package/esm/core/codegen/shared/index.js +16 -3
- package/esm/core/codegen/type-resolver.d.ts +1 -5
- package/esm/core/codegen/type-resolver.js +1 -22
- package/esm/core/codegen/types.d.ts +0 -3
- package/esm/core/codegen/types.js +72 -15
- package/esm/core/codegen/utils.d.ts +1 -4
- package/esm/core/codegen/utils.js +4 -1
- package/esm/core/config/index.d.ts +1 -1
- package/esm/core/config/resolver.js +2 -4
- package/esm/core/generate.js +38 -50
- package/esm/core/index.d.ts +3 -3
- package/esm/core/index.js +2 -3
- package/esm/core/introspect/index.d.ts +6 -6
- package/esm/core/introspect/index.js +3 -6
- package/esm/core/introspect/infer-tables.d.ts +0 -14
- package/esm/core/introspect/infer-tables.js +16 -2
- package/esm/core/introspect/source/database.js +2 -2
- package/esm/core/introspect/source/endpoint.d.ts +0 -6
- package/esm/core/introspect/source/endpoint.js +7 -1
- package/esm/core/introspect/source/index.d.ts +4 -4
- package/esm/core/introspect/source/index.js +6 -10
- package/esm/core/introspect/source/pgpm-module.js +3 -3
- package/esm/core/introspect/transform-schema.d.ts +2 -2
- package/esm/core/introspect/transform-schema.js +2 -2
- package/esm/core/output/index.d.ts +1 -1
- package/esm/core/output/index.js +1 -1
- package/esm/core/output/writer.d.ts +3 -0
- package/esm/core/output/writer.js +20 -1
- package/esm/core/pipeline/index.d.ts +2 -2
- package/esm/core/pipeline/index.js +2 -2
- package/esm/core/query-builder.d.ts +2 -2
- package/esm/core/query-builder.js +2 -2
- package/esm/core/watch/index.d.ts +4 -4
- package/esm/core/watch/index.js +3 -3
- package/esm/core/watch/orchestrator.js +5 -3
- package/esm/generators/index.d.ts +3 -3
- package/esm/generators/index.js +3 -3
- package/esm/generators/mutations.d.ts +1 -1
- package/esm/generators/select.d.ts +1 -1
- package/esm/index.d.ts +3 -3
- package/esm/index.js +1 -4
- package/esm/types/config.d.ts +0 -10
- package/esm/types/config.js +0 -2
- package/esm/types/index.d.ts +6 -6
- package/esm/types/index.js +1 -1
- package/generators/index.d.ts +3 -3
- package/generators/index.js +8 -8
- package/generators/mutations.d.ts +1 -1
- package/generators/select.d.ts +1 -1
- package/index.d.ts +3 -3
- package/index.js +11 -5
- package/package.json +11 -11
- package/types/config.d.ts +0 -10
- package/types/config.js +0 -2
- package/types/index.d.ts +6 -6
- package/types/index.js +2 -2
- package/core/codegen/gql-ast.d.ts +0 -41
- package/core/codegen/gql-ast.js +0 -353
- package/core/codegen/schema-gql-ast.d.ts +0 -51
- package/core/codegen/schema-gql-ast.js +0 -385
- package/core/codegen/templates/client.browser.ts +0 -271
- package/core/codegen/templates/client.node.ts +0 -337
- package/esm/core/codegen/gql-ast.d.ts +0 -41
- package/esm/core/codegen/gql-ast.js +0 -312
- package/esm/core/codegen/schema-gql-ast.d.ts +0 -51
- package/esm/core/codegen/schema-gql-ast.js +0 -343
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared Babel AST helpers for React Query hook generators
|
|
3
|
+
*
|
|
4
|
+
* Provides reusable AST-building functions for the common patterns
|
|
5
|
+
* used across queries.ts, mutations.ts, custom-queries.ts, and custom-mutations.ts.
|
|
6
|
+
*/
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import type { CleanArgument } from '../../types/schema';
|
|
9
|
+
export declare function createImportDeclaration(moduleSpecifier: string, namedImports: string[], typeOnly?: boolean): t.ImportDeclaration;
|
|
10
|
+
export declare function createTypeReExport(names: string[], moduleSpecifier: string): t.ExportNamedDeclaration;
|
|
11
|
+
export declare function typeRef(name: string, params?: t.TSType[]): t.TSTypeReference;
|
|
12
|
+
export declare function sRef(): t.TSTypeReference;
|
|
13
|
+
export declare function typeofRef(name: string): t.TSTypeQuery;
|
|
14
|
+
export declare function stringLiteralType(value: string): t.TSLiteralType;
|
|
15
|
+
export declare function inferSelectResultType(entityTypeName: string, selectType: t.TSType): t.TSTypeReference;
|
|
16
|
+
export declare function connectionResultType(entityTypeName: string, selectType: t.TSType): t.TSTypeReference;
|
|
17
|
+
export declare function typeLiteralWithProps(props: Array<{
|
|
18
|
+
name: string;
|
|
19
|
+
type: t.TSType;
|
|
20
|
+
optional?: boolean;
|
|
21
|
+
}>): t.TSTypeLiteral;
|
|
22
|
+
export declare function queryResultType(queryName: string, innerType: t.TSType): t.TSTypeLiteral;
|
|
23
|
+
export declare function listQueryResultType(queryName: string, entityTypeName: string, selectType: t.TSType): t.TSTypeLiteral;
|
|
24
|
+
export declare function singleQueryResultType(queryName: string, entityTypeName: string, selectType: t.TSType, nullable?: boolean): t.TSTypeLiteral;
|
|
25
|
+
export declare function mutationResultType(mutationName: string, entityField: string, entityTypeName: string, selectType: t.TSType): t.TSTypeLiteral;
|
|
26
|
+
export declare function omitType(baseType: t.TSType, keys: string[]): t.TSTypeReference;
|
|
27
|
+
export declare function listSelectionConfigType(selectType: t.TSType, filterTypeName: string, orderByTypeName: string): t.TSTypeReference;
|
|
28
|
+
export declare function selectionConfigType(selectType: t.TSType): t.TSTypeReference;
|
|
29
|
+
export declare function strictSelectType(selectType: t.TSType, shapeTypeName: string): t.TSTypeReference;
|
|
30
|
+
export declare function withFieldsSelectionType(selectType: t.TSType, selectTypeName: string): t.TSIntersectionType;
|
|
31
|
+
export declare function withFieldsListSelectionType(selectType: t.TSType, selectTypeName: string, filterTypeName: string, orderByTypeName: string): t.TSIntersectionType;
|
|
32
|
+
export declare function useQueryOptionsType(queryDataType: t.TSType, dataType: t.TSType, extraProps?: t.TSType): t.TSType;
|
|
33
|
+
export declare function useQueryOptionsImplType(extraProps?: t.TSType): t.TSType;
|
|
34
|
+
export declare function useMutationOptionsType(resultType: t.TSType, varType: t.TSType): t.TSTypeReference;
|
|
35
|
+
export declare function useMutationResultType(resultType: t.TSType, varType: t.TSType): t.TSTypeReference;
|
|
36
|
+
export declare function createSTypeParam(constraintName: string): t.TSTypeParameterDeclaration;
|
|
37
|
+
export declare function createSAndTDataTypeParams(constraintName: string, defaultDataType: t.TSType): t.TSTypeParameterDeclaration;
|
|
38
|
+
export declare function createTDataTypeParam(defaultType: t.TSType): t.TSTypeParameterDeclaration;
|
|
39
|
+
export declare function createFunctionParam(name: string, typeAnnotation: t.TSType, optional?: boolean): t.Identifier;
|
|
40
|
+
export declare function exportDeclareFunction(name: string, typeParameters: t.TSTypeParameterDeclaration | null, params: t.Identifier[], returnType: t.TSType): t.ExportNamedDeclaration;
|
|
41
|
+
export declare function exportFunction(name: string, typeParameters: t.TSTypeParameterDeclaration | null, params: t.Identifier[], body: t.Statement[], returnType?: t.TSType): t.ExportNamedDeclaration;
|
|
42
|
+
export declare function exportAsyncFunction(name: string, typeParameters: t.TSTypeParameterDeclaration | null, params: t.Identifier[], body: t.Statement[], returnType?: t.TSType): t.ExportNamedDeclaration;
|
|
43
|
+
export declare function exportAsyncDeclareFunction(name: string, typeParameters: t.TSTypeParameterDeclaration | null, params: t.Identifier[], returnType: t.TSType): t.ExportNamedDeclaration;
|
|
44
|
+
export declare function callExpr(callee: string | t.Expression, args: (t.Expression | t.SpreadElement)[]): t.CallExpression;
|
|
45
|
+
export declare function memberExpr(obj: string, prop: string): t.MemberExpression;
|
|
46
|
+
export declare function optionalMemberExpr(obj: string, prop: string): t.OptionalMemberExpression;
|
|
47
|
+
export declare function arrowFn(params: t.Identifier[], body: t.Expression | t.BlockStatement): t.ArrowFunctionExpression;
|
|
48
|
+
export declare function awaitExpr(expr: t.Expression): t.AwaitExpression;
|
|
49
|
+
export declare function spreadObj(expr: t.Expression): t.SpreadElement;
|
|
50
|
+
export declare function objectProp(key: string, value: t.Expression, shorthand?: boolean): t.ObjectProperty;
|
|
51
|
+
export declare function shorthandProp(name: string): t.ObjectProperty;
|
|
52
|
+
export declare function constDecl(name: string, init: t.Expression): t.VariableDeclaration;
|
|
53
|
+
export declare function asConstExpr(expr: t.Expression): t.TSAsExpression;
|
|
54
|
+
export declare function asTypeExpr(expr: t.Expression, typeName: string): t.TSAsExpression;
|
|
55
|
+
export declare function voidStatement(name: string): t.ExpressionStatement;
|
|
56
|
+
export declare function returnUseQuery(queryKeyExpr: t.Expression, queryFnExpr: t.Expression, extraProps?: Array<t.ObjectProperty | t.SpreadElement>, enabledExpr?: t.Expression): t.ReturnStatement;
|
|
57
|
+
export declare function returnUseMutation(mutationFnExpr: t.Expression, extraProps: Array<t.ObjectProperty | t.SpreadElement>, mutationKeyExpr?: t.Expression): t.ReturnStatement;
|
|
58
|
+
export declare function destructureWithRest(source: t.Expression, keys: string[], restName: string): t.VariableDeclaration;
|
|
59
|
+
export declare function destructureParamsWithSelection(restName: string, extraKeys?: string[]): t.VariableDeclaration;
|
|
60
|
+
export declare function destructureParamsWithSelectionAndScope(restName: string): t.VariableDeclaration;
|
|
61
|
+
export declare function addJSDocComment<T extends t.Node>(node: T, lines: string[]): T;
|
|
62
|
+
export declare function addLineComment<T extends t.Node>(node: T, text: string): T;
|
|
63
|
+
export declare function getClientCall(modelName: string, method: string, args: t.Expression): t.CallExpression;
|
|
64
|
+
export declare function getClientCallUnwrap(modelName: string, method: string, args: t.Expression): t.CallExpression;
|
|
65
|
+
export declare function getClientCustomCall(operationType: 'query' | 'mutation', operationName: string, args: t.Expression[], optionsArg?: t.Expression): t.CallExpression;
|
|
66
|
+
export declare function getClientCustomCallUnwrap(operationType: 'query' | 'mutation', operationName: string, args: t.Expression[], optionsArg?: t.Expression): t.CallExpression;
|
|
67
|
+
export declare function buildFindManyCallExpr(singularName: string, argsIdent: string): t.CallExpression;
|
|
68
|
+
export declare function buildFindOneCallExpr(singularName: string, pkFieldName: string, argsIdent: string, paramsIdent?: string): t.CallExpression;
|
|
69
|
+
export declare function generateHookFileCode(headerDescription: string, statements: t.Statement[]): string;
|
|
70
|
+
export declare function scopeTypeLiteral(scopeTypeName: string): t.TSTypeLiteral;
|
|
71
|
+
export declare function wrapInferSelectResultType(typeRefNode: CleanArgument['type'], payloadTypeName: string, selectType: t.TSType): t.TSType;
|
|
72
|
+
export declare function typeRefToTsTypeAST(typeRefNode: CleanArgument['type']): t.TSType;
|
|
73
|
+
export declare function buildSelectionArgsCall(selectTypeName: string): t.VariableDeclaration;
|
|
74
|
+
export declare function buildListSelectionArgsCall(selectTypeName: string, filterTypeName: string, orderByTypeName: string): t.VariableDeclaration;
|
|
75
|
+
export declare function customSelectResultTypeLiteral(opName: string, returnType: CleanArgument['type'], payloadTypeName: string, selectType: t.TSType): t.TSTypeLiteral;
|
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createImportDeclaration = createImportDeclaration;
|
|
37
|
+
exports.createTypeReExport = createTypeReExport;
|
|
38
|
+
exports.typeRef = typeRef;
|
|
39
|
+
exports.sRef = sRef;
|
|
40
|
+
exports.typeofRef = typeofRef;
|
|
41
|
+
exports.stringLiteralType = stringLiteralType;
|
|
42
|
+
exports.inferSelectResultType = inferSelectResultType;
|
|
43
|
+
exports.connectionResultType = connectionResultType;
|
|
44
|
+
exports.typeLiteralWithProps = typeLiteralWithProps;
|
|
45
|
+
exports.queryResultType = queryResultType;
|
|
46
|
+
exports.listQueryResultType = listQueryResultType;
|
|
47
|
+
exports.singleQueryResultType = singleQueryResultType;
|
|
48
|
+
exports.mutationResultType = mutationResultType;
|
|
49
|
+
exports.omitType = omitType;
|
|
50
|
+
exports.listSelectionConfigType = listSelectionConfigType;
|
|
51
|
+
exports.selectionConfigType = selectionConfigType;
|
|
52
|
+
exports.strictSelectType = strictSelectType;
|
|
53
|
+
exports.withFieldsSelectionType = withFieldsSelectionType;
|
|
54
|
+
exports.withFieldsListSelectionType = withFieldsListSelectionType;
|
|
55
|
+
exports.useQueryOptionsType = useQueryOptionsType;
|
|
56
|
+
exports.useQueryOptionsImplType = useQueryOptionsImplType;
|
|
57
|
+
exports.useMutationOptionsType = useMutationOptionsType;
|
|
58
|
+
exports.useMutationResultType = useMutationResultType;
|
|
59
|
+
exports.createSTypeParam = createSTypeParam;
|
|
60
|
+
exports.createSAndTDataTypeParams = createSAndTDataTypeParams;
|
|
61
|
+
exports.createTDataTypeParam = createTDataTypeParam;
|
|
62
|
+
exports.createFunctionParam = createFunctionParam;
|
|
63
|
+
exports.exportDeclareFunction = exportDeclareFunction;
|
|
64
|
+
exports.exportFunction = exportFunction;
|
|
65
|
+
exports.exportAsyncFunction = exportAsyncFunction;
|
|
66
|
+
exports.exportAsyncDeclareFunction = exportAsyncDeclareFunction;
|
|
67
|
+
exports.callExpr = callExpr;
|
|
68
|
+
exports.memberExpr = memberExpr;
|
|
69
|
+
exports.optionalMemberExpr = optionalMemberExpr;
|
|
70
|
+
exports.arrowFn = arrowFn;
|
|
71
|
+
exports.awaitExpr = awaitExpr;
|
|
72
|
+
exports.spreadObj = spreadObj;
|
|
73
|
+
exports.objectProp = objectProp;
|
|
74
|
+
exports.shorthandProp = shorthandProp;
|
|
75
|
+
exports.constDecl = constDecl;
|
|
76
|
+
exports.asConstExpr = asConstExpr;
|
|
77
|
+
exports.asTypeExpr = asTypeExpr;
|
|
78
|
+
exports.voidStatement = voidStatement;
|
|
79
|
+
exports.returnUseQuery = returnUseQuery;
|
|
80
|
+
exports.returnUseMutation = returnUseMutation;
|
|
81
|
+
exports.destructureWithRest = destructureWithRest;
|
|
82
|
+
exports.destructureParamsWithSelection = destructureParamsWithSelection;
|
|
83
|
+
exports.destructureParamsWithSelectionAndScope = destructureParamsWithSelectionAndScope;
|
|
84
|
+
exports.addJSDocComment = addJSDocComment;
|
|
85
|
+
exports.addLineComment = addLineComment;
|
|
86
|
+
exports.getClientCall = getClientCall;
|
|
87
|
+
exports.getClientCallUnwrap = getClientCallUnwrap;
|
|
88
|
+
exports.getClientCustomCall = getClientCustomCall;
|
|
89
|
+
exports.getClientCustomCallUnwrap = getClientCustomCallUnwrap;
|
|
90
|
+
exports.buildFindManyCallExpr = buildFindManyCallExpr;
|
|
91
|
+
exports.buildFindOneCallExpr = buildFindOneCallExpr;
|
|
92
|
+
exports.generateHookFileCode = generateHookFileCode;
|
|
93
|
+
exports.scopeTypeLiteral = scopeTypeLiteral;
|
|
94
|
+
exports.wrapInferSelectResultType = wrapInferSelectResultType;
|
|
95
|
+
exports.typeRefToTsTypeAST = typeRefToTsTypeAST;
|
|
96
|
+
exports.buildSelectionArgsCall = buildSelectionArgsCall;
|
|
97
|
+
exports.buildListSelectionArgsCall = buildListSelectionArgsCall;
|
|
98
|
+
exports.customSelectResultTypeLiteral = customSelectResultTypeLiteral;
|
|
99
|
+
/**
|
|
100
|
+
* Shared Babel AST helpers for React Query hook generators
|
|
101
|
+
*
|
|
102
|
+
* Provides reusable AST-building functions for the common patterns
|
|
103
|
+
* used across queries.ts, mutations.ts, custom-queries.ts, and custom-mutations.ts.
|
|
104
|
+
*/
|
|
105
|
+
const t = __importStar(require("@babel/types"));
|
|
106
|
+
const babel_ast_1 = require("./babel-ast");
|
|
107
|
+
const type_resolver_1 = require("./type-resolver");
|
|
108
|
+
const utils_1 = require("./utils");
|
|
109
|
+
// ============================================================================
|
|
110
|
+
// Import helpers
|
|
111
|
+
// ============================================================================
|
|
112
|
+
function createImportDeclaration(moduleSpecifier, namedImports, typeOnly = false) {
|
|
113
|
+
const specifiers = namedImports.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name)));
|
|
114
|
+
const decl = t.importDeclaration(specifiers, t.stringLiteral(moduleSpecifier));
|
|
115
|
+
decl.importKind = typeOnly ? 'type' : 'value';
|
|
116
|
+
return decl;
|
|
117
|
+
}
|
|
118
|
+
function createTypeReExport(names, moduleSpecifier) {
|
|
119
|
+
const specifiers = names.map((name) => t.exportSpecifier(t.identifier(name), t.identifier(name)));
|
|
120
|
+
const decl = t.exportNamedDeclaration(null, specifiers, t.stringLiteral(moduleSpecifier));
|
|
121
|
+
decl.exportKind = 'type';
|
|
122
|
+
return decl;
|
|
123
|
+
}
|
|
124
|
+
// ============================================================================
|
|
125
|
+
// Type reference helpers
|
|
126
|
+
// ============================================================================
|
|
127
|
+
function typeRef(name, params) {
|
|
128
|
+
return t.tsTypeReference(t.identifier(name), params ? t.tsTypeParameterInstantiation(params) : undefined);
|
|
129
|
+
}
|
|
130
|
+
function sRef() {
|
|
131
|
+
return typeRef('S');
|
|
132
|
+
}
|
|
133
|
+
function typeofRef(name) {
|
|
134
|
+
return t.tsTypeQuery(t.identifier(name));
|
|
135
|
+
}
|
|
136
|
+
function stringLiteralType(value) {
|
|
137
|
+
return t.tsLiteralType(t.stringLiteral(value));
|
|
138
|
+
}
|
|
139
|
+
// ============================================================================
|
|
140
|
+
// Complex type builders
|
|
141
|
+
// ============================================================================
|
|
142
|
+
function inferSelectResultType(entityTypeName, selectType) {
|
|
143
|
+
return typeRef('InferSelectResult', [typeRef(entityTypeName), selectType]);
|
|
144
|
+
}
|
|
145
|
+
function connectionResultType(entityTypeName, selectType) {
|
|
146
|
+
return typeRef('ConnectionResult', [
|
|
147
|
+
inferSelectResultType(entityTypeName, selectType),
|
|
148
|
+
]);
|
|
149
|
+
}
|
|
150
|
+
function typeLiteralWithProps(props) {
|
|
151
|
+
return t.tsTypeLiteral(props.map((p) => {
|
|
152
|
+
const prop = t.tsPropertySignature(t.identifier(p.name), t.tsTypeAnnotation(p.type));
|
|
153
|
+
if (p.optional) {
|
|
154
|
+
prop.optional = true;
|
|
155
|
+
}
|
|
156
|
+
return prop;
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
function queryResultType(queryName, innerType) {
|
|
160
|
+
return typeLiteralWithProps([{ name: queryName, type: innerType }]);
|
|
161
|
+
}
|
|
162
|
+
function listQueryResultType(queryName, entityTypeName, selectType) {
|
|
163
|
+
return queryResultType(queryName, connectionResultType(entityTypeName, selectType));
|
|
164
|
+
}
|
|
165
|
+
function singleQueryResultType(queryName, entityTypeName, selectType, nullable = true) {
|
|
166
|
+
const resultType = inferSelectResultType(entityTypeName, selectType);
|
|
167
|
+
const innerType = nullable
|
|
168
|
+
? t.tsUnionType([resultType, t.tsNullKeyword()])
|
|
169
|
+
: resultType;
|
|
170
|
+
return queryResultType(queryName, innerType);
|
|
171
|
+
}
|
|
172
|
+
function mutationResultType(mutationName, entityField, entityTypeName, selectType) {
|
|
173
|
+
return queryResultType(mutationName, typeLiteralWithProps([
|
|
174
|
+
{
|
|
175
|
+
name: entityField,
|
|
176
|
+
type: inferSelectResultType(entityTypeName, selectType),
|
|
177
|
+
},
|
|
178
|
+
]));
|
|
179
|
+
}
|
|
180
|
+
function omitType(baseType, keys) {
|
|
181
|
+
const keyType = keys.length === 1
|
|
182
|
+
? stringLiteralType(keys[0])
|
|
183
|
+
: t.tsUnionType(keys.map(stringLiteralType));
|
|
184
|
+
return typeRef('Omit', [baseType, keyType]);
|
|
185
|
+
}
|
|
186
|
+
function listSelectionConfigType(selectType, filterTypeName, orderByTypeName) {
|
|
187
|
+
return typeRef('ListSelectionConfig', [
|
|
188
|
+
selectType,
|
|
189
|
+
typeRef(filterTypeName),
|
|
190
|
+
typeRef(orderByTypeName),
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
function selectionConfigType(selectType) {
|
|
194
|
+
return typeRef('SelectionConfig', [selectType]);
|
|
195
|
+
}
|
|
196
|
+
function strictSelectType(selectType, shapeTypeName) {
|
|
197
|
+
return typeRef('StrictSelect', [selectType, typeRef(shapeTypeName)]);
|
|
198
|
+
}
|
|
199
|
+
function withFieldsSelectionType(selectType, selectTypeName) {
|
|
200
|
+
return t.tsIntersectionType([
|
|
201
|
+
typeLiteralWithProps([{ name: 'fields', type: selectType }]),
|
|
202
|
+
strictSelectType(selectType, selectTypeName),
|
|
203
|
+
]);
|
|
204
|
+
}
|
|
205
|
+
function withFieldsListSelectionType(selectType, selectTypeName, filterTypeName, orderByTypeName) {
|
|
206
|
+
return t.tsIntersectionType([
|
|
207
|
+
typeLiteralWithProps([{ name: 'fields', type: selectType }]),
|
|
208
|
+
omitType(listSelectionConfigType(selectType, filterTypeName, orderByTypeName), ['fields']),
|
|
209
|
+
strictSelectType(selectType, selectTypeName),
|
|
210
|
+
]);
|
|
211
|
+
}
|
|
212
|
+
function useQueryOptionsType(queryDataType, dataType, extraProps) {
|
|
213
|
+
const base = omitType(typeRef('UseQueryOptions', [queryDataType, typeRef('Error'), dataType]), ['queryKey', 'queryFn']);
|
|
214
|
+
if (extraProps) {
|
|
215
|
+
return t.tsIntersectionType([base, extraProps]);
|
|
216
|
+
}
|
|
217
|
+
return base;
|
|
218
|
+
}
|
|
219
|
+
function useQueryOptionsImplType(extraProps) {
|
|
220
|
+
const base = omitType(typeRef('UseQueryOptions', [
|
|
221
|
+
t.tsAnyKeyword(),
|
|
222
|
+
typeRef('Error'),
|
|
223
|
+
t.tsAnyKeyword(),
|
|
224
|
+
t.tsAnyKeyword(),
|
|
225
|
+
]), ['queryKey', 'queryFn']);
|
|
226
|
+
if (extraProps) {
|
|
227
|
+
return t.tsIntersectionType([base, extraProps]);
|
|
228
|
+
}
|
|
229
|
+
return base;
|
|
230
|
+
}
|
|
231
|
+
function useMutationOptionsType(resultType, varType) {
|
|
232
|
+
return omitType(typeRef('UseMutationOptions', [resultType, typeRef('Error'), varType]), ['mutationFn']);
|
|
233
|
+
}
|
|
234
|
+
function useMutationResultType(resultType, varType) {
|
|
235
|
+
return typeRef('UseMutationResult', [resultType, typeRef('Error'), varType]);
|
|
236
|
+
}
|
|
237
|
+
// ============================================================================
|
|
238
|
+
// Type parameter helpers
|
|
239
|
+
// ============================================================================
|
|
240
|
+
function createSTypeParam(constraintName) {
|
|
241
|
+
const param = t.tsTypeParameter(typeRef(constraintName), null, 'S');
|
|
242
|
+
return t.tsTypeParameterDeclaration([param]);
|
|
243
|
+
}
|
|
244
|
+
function createSAndTDataTypeParams(constraintName, defaultDataType) {
|
|
245
|
+
const sParam = t.tsTypeParameter(typeRef(constraintName), null, 'S');
|
|
246
|
+
const tDataParam = t.tsTypeParameter(null, defaultDataType, 'TData');
|
|
247
|
+
return t.tsTypeParameterDeclaration([sParam, tDataParam]);
|
|
248
|
+
}
|
|
249
|
+
function createTDataTypeParam(defaultType) {
|
|
250
|
+
const param = t.tsTypeParameter(null, defaultType, 'TData');
|
|
251
|
+
return t.tsTypeParameterDeclaration([param]);
|
|
252
|
+
}
|
|
253
|
+
// ============================================================================
|
|
254
|
+
// Function declaration helpers
|
|
255
|
+
// ============================================================================
|
|
256
|
+
function createFunctionParam(name, typeAnnotation, optional = false) {
|
|
257
|
+
const param = t.identifier(name);
|
|
258
|
+
param.typeAnnotation = t.tsTypeAnnotation(typeAnnotation);
|
|
259
|
+
param.optional = optional;
|
|
260
|
+
return param;
|
|
261
|
+
}
|
|
262
|
+
function exportDeclareFunction(name, typeParameters, params, returnType) {
|
|
263
|
+
const func = t.tsDeclareFunction(t.identifier(name), typeParameters, params, t.tsTypeAnnotation(returnType));
|
|
264
|
+
return t.exportNamedDeclaration(func);
|
|
265
|
+
}
|
|
266
|
+
function exportFunction(name, typeParameters, params, body, returnType) {
|
|
267
|
+
const func = t.functionDeclaration(t.identifier(name), params, t.blockStatement(body));
|
|
268
|
+
func.typeParameters = typeParameters;
|
|
269
|
+
if (returnType) {
|
|
270
|
+
func.returnType = t.tsTypeAnnotation(returnType);
|
|
271
|
+
}
|
|
272
|
+
return t.exportNamedDeclaration(func);
|
|
273
|
+
}
|
|
274
|
+
function exportAsyncFunction(name, typeParameters, params, body, returnType) {
|
|
275
|
+
const func = t.functionDeclaration(t.identifier(name), params, t.blockStatement(body));
|
|
276
|
+
func.async = true;
|
|
277
|
+
func.typeParameters = typeParameters;
|
|
278
|
+
if (returnType) {
|
|
279
|
+
func.returnType = t.tsTypeAnnotation(returnType);
|
|
280
|
+
}
|
|
281
|
+
return t.exportNamedDeclaration(func);
|
|
282
|
+
}
|
|
283
|
+
function exportAsyncDeclareFunction(name, typeParameters, params, returnType) {
|
|
284
|
+
const func = t.tsDeclareFunction(t.identifier(name), typeParameters, params, t.tsTypeAnnotation(returnType));
|
|
285
|
+
func.async = true;
|
|
286
|
+
return t.exportNamedDeclaration(func);
|
|
287
|
+
}
|
|
288
|
+
// ============================================================================
|
|
289
|
+
// Expression helpers
|
|
290
|
+
// ============================================================================
|
|
291
|
+
function callExpr(callee, args) {
|
|
292
|
+
const calleeExpr = typeof callee === 'string' ? t.identifier(callee) : callee;
|
|
293
|
+
return t.callExpression(calleeExpr, args);
|
|
294
|
+
}
|
|
295
|
+
function memberExpr(obj, prop) {
|
|
296
|
+
return t.memberExpression(t.identifier(obj), t.identifier(prop));
|
|
297
|
+
}
|
|
298
|
+
function optionalMemberExpr(obj, prop) {
|
|
299
|
+
return t.optionalMemberExpression(t.identifier(obj), t.identifier(prop), false, true);
|
|
300
|
+
}
|
|
301
|
+
function arrowFn(params, body) {
|
|
302
|
+
return t.arrowFunctionExpression(params, body);
|
|
303
|
+
}
|
|
304
|
+
function awaitExpr(expr) {
|
|
305
|
+
return t.awaitExpression(expr);
|
|
306
|
+
}
|
|
307
|
+
function spreadObj(expr) {
|
|
308
|
+
return t.spreadElement(expr);
|
|
309
|
+
}
|
|
310
|
+
function objectProp(key, value, shorthand = false) {
|
|
311
|
+
return t.objectProperty(t.identifier(key), value, false, shorthand);
|
|
312
|
+
}
|
|
313
|
+
function shorthandProp(name) {
|
|
314
|
+
return t.objectProperty(t.identifier(name), t.identifier(name), false, true);
|
|
315
|
+
}
|
|
316
|
+
function constDecl(name, init) {
|
|
317
|
+
return t.variableDeclaration('const', [
|
|
318
|
+
t.variableDeclarator(t.identifier(name), init),
|
|
319
|
+
]);
|
|
320
|
+
}
|
|
321
|
+
function asConstExpr(expr) {
|
|
322
|
+
return t.tsAsExpression(expr, t.tsTypeReference(t.identifier('const')));
|
|
323
|
+
}
|
|
324
|
+
function asTypeExpr(expr, typeName) {
|
|
325
|
+
return t.tsAsExpression(expr, typeRef(typeName));
|
|
326
|
+
}
|
|
327
|
+
// ============================================================================
|
|
328
|
+
// Statement helpers
|
|
329
|
+
// ============================================================================
|
|
330
|
+
function voidStatement(name) {
|
|
331
|
+
return t.expressionStatement(t.unaryExpression('void', t.identifier(name)));
|
|
332
|
+
}
|
|
333
|
+
function returnUseQuery(queryKeyExpr, queryFnExpr, extraProps, enabledExpr) {
|
|
334
|
+
const props = [
|
|
335
|
+
objectProp('queryKey', queryKeyExpr),
|
|
336
|
+
objectProp('queryFn', queryFnExpr),
|
|
337
|
+
];
|
|
338
|
+
if (enabledExpr) {
|
|
339
|
+
props.push(objectProp('enabled', enabledExpr));
|
|
340
|
+
}
|
|
341
|
+
if (extraProps) {
|
|
342
|
+
props.push(...extraProps);
|
|
343
|
+
}
|
|
344
|
+
return t.returnStatement(callExpr('useQuery', [t.objectExpression(props)]));
|
|
345
|
+
}
|
|
346
|
+
function returnUseMutation(mutationFnExpr, extraProps, mutationKeyExpr) {
|
|
347
|
+
const props = [];
|
|
348
|
+
if (mutationKeyExpr) {
|
|
349
|
+
props.push(objectProp('mutationKey', mutationKeyExpr));
|
|
350
|
+
}
|
|
351
|
+
props.push(objectProp('mutationFn', mutationFnExpr));
|
|
352
|
+
props.push(...extraProps);
|
|
353
|
+
return t.returnStatement(callExpr('useMutation', [t.objectExpression(props)]));
|
|
354
|
+
}
|
|
355
|
+
// ============================================================================
|
|
356
|
+
// Destructuring helpers
|
|
357
|
+
// ============================================================================
|
|
358
|
+
function destructureWithRest(source, keys, restName) {
|
|
359
|
+
const properties = keys.map((key) => t.objectProperty(t.identifier(key), t.identifier(`_${key}`), false, false));
|
|
360
|
+
const pattern = t.objectPattern([
|
|
361
|
+
...properties,
|
|
362
|
+
t.restElement(t.identifier(restName)),
|
|
363
|
+
]);
|
|
364
|
+
return constDecl(restName, t.identifier('__placeholder__'));
|
|
365
|
+
}
|
|
366
|
+
function destructureParamsWithSelection(restName, extraKeys = []) {
|
|
367
|
+
const properties = [];
|
|
368
|
+
for (const key of extraKeys) {
|
|
369
|
+
properties.push(t.objectProperty(t.identifier(key), t.identifier(key), false, true));
|
|
370
|
+
}
|
|
371
|
+
properties.push(t.objectProperty(t.identifier('selection'), t.identifier('_selection'), false, false));
|
|
372
|
+
properties.push(t.restElement(t.identifier(restName)));
|
|
373
|
+
const pattern = t.objectPattern(properties);
|
|
374
|
+
return t.variableDeclaration('const', [
|
|
375
|
+
t.variableDeclarator(pattern, t.logicalExpression('??', t.identifier('params'), t.objectExpression([]))),
|
|
376
|
+
]);
|
|
377
|
+
}
|
|
378
|
+
function destructureParamsWithSelectionAndScope(restName) {
|
|
379
|
+
const pattern = t.objectPattern([
|
|
380
|
+
t.objectProperty(t.identifier('scope'), t.identifier('scope'), false, true),
|
|
381
|
+
t.objectProperty(t.identifier('selection'), t.identifier('_selection'), false, false),
|
|
382
|
+
t.restElement(t.identifier(restName)),
|
|
383
|
+
]);
|
|
384
|
+
return t.variableDeclaration('const', [
|
|
385
|
+
t.variableDeclarator(pattern, t.logicalExpression('??', t.identifier('params'), t.objectExpression([]))),
|
|
386
|
+
]);
|
|
387
|
+
}
|
|
388
|
+
// ============================================================================
|
|
389
|
+
// JSDoc comment helpers
|
|
390
|
+
// ============================================================================
|
|
391
|
+
function addJSDocComment(node, lines) {
|
|
392
|
+
const text = lines.length === 1
|
|
393
|
+
? `* ${lines[0]} `
|
|
394
|
+
: `*\n${lines.map((line) => ` * ${line}`).join('\n')}\n `;
|
|
395
|
+
if (!node.leadingComments) {
|
|
396
|
+
node.leadingComments = [];
|
|
397
|
+
}
|
|
398
|
+
node.leadingComments.push((0, babel_ast_1.commentBlock)(text));
|
|
399
|
+
return node;
|
|
400
|
+
}
|
|
401
|
+
function addLineComment(node, text) {
|
|
402
|
+
if (!node.leadingComments) {
|
|
403
|
+
node.leadingComments = [];
|
|
404
|
+
}
|
|
405
|
+
node.leadingComments.push({
|
|
406
|
+
type: 'CommentLine',
|
|
407
|
+
value: ` ${text}`,
|
|
408
|
+
start: null,
|
|
409
|
+
end: null,
|
|
410
|
+
loc: null,
|
|
411
|
+
});
|
|
412
|
+
return node;
|
|
413
|
+
}
|
|
414
|
+
// ============================================================================
|
|
415
|
+
// ORM client call builders
|
|
416
|
+
// ============================================================================
|
|
417
|
+
function getClientCall(modelName, method, args) {
|
|
418
|
+
return t.callExpression(t.memberExpression(t.memberExpression(callExpr('getClient', []), t.identifier(modelName)), t.identifier(method)), [args]);
|
|
419
|
+
}
|
|
420
|
+
function getClientCallUnwrap(modelName, method, args) {
|
|
421
|
+
return t.callExpression(t.memberExpression(getClientCall(modelName, method, args), t.identifier('unwrap')), []);
|
|
422
|
+
}
|
|
423
|
+
function getClientCustomCall(operationType, operationName, args, optionsArg) {
|
|
424
|
+
const callArgs = optionsArg ? [...args, optionsArg] : args;
|
|
425
|
+
return t.callExpression(t.memberExpression(t.memberExpression(callExpr('getClient', []), t.identifier(operationType)), t.identifier(operationName)), callArgs);
|
|
426
|
+
}
|
|
427
|
+
function getClientCustomCallUnwrap(operationType, operationName, args, optionsArg) {
|
|
428
|
+
return t.callExpression(t.memberExpression(getClientCustomCall(operationType, operationName, args, optionsArg), t.identifier('unwrap')), []);
|
|
429
|
+
}
|
|
430
|
+
// ============================================================================
|
|
431
|
+
// Select/args expression builders
|
|
432
|
+
// ============================================================================
|
|
433
|
+
function buildFindManyCallExpr(singularName, argsIdent) {
|
|
434
|
+
return getClientCallUnwrap(singularName, 'findMany', t.identifier(argsIdent));
|
|
435
|
+
}
|
|
436
|
+
function buildFindOneCallExpr(singularName, pkFieldName, argsIdent, paramsIdent = 'params') {
|
|
437
|
+
return getClientCallUnwrap(singularName, 'findOne', t.objectExpression([
|
|
438
|
+
objectProp(pkFieldName, t.memberExpression(t.identifier(paramsIdent), t.identifier(pkFieldName))),
|
|
439
|
+
objectProp('select', t.memberExpression(t.identifier(argsIdent), t.identifier('select'))),
|
|
440
|
+
]));
|
|
441
|
+
}
|
|
442
|
+
// ============================================================================
|
|
443
|
+
// Code generation
|
|
444
|
+
// ============================================================================
|
|
445
|
+
function generateHookFileCode(headerDescription, statements) {
|
|
446
|
+
const header = (0, utils_1.getGeneratedFileHeader)(headerDescription);
|
|
447
|
+
const code = (0, babel_ast_1.generateCode)(statements);
|
|
448
|
+
return header + '\n\n' + code + '\n';
|
|
449
|
+
}
|
|
450
|
+
// ============================================================================
|
|
451
|
+
// Scope type helper
|
|
452
|
+
// ============================================================================
|
|
453
|
+
function scopeTypeLiteral(scopeTypeName) {
|
|
454
|
+
return typeLiteralWithProps([
|
|
455
|
+
{
|
|
456
|
+
name: 'scope',
|
|
457
|
+
type: typeRef(scopeTypeName),
|
|
458
|
+
optional: true,
|
|
459
|
+
},
|
|
460
|
+
]);
|
|
461
|
+
}
|
|
462
|
+
// ============================================================================
|
|
463
|
+
// Type conversion helpers (GraphQL -> AST)
|
|
464
|
+
// ============================================================================
|
|
465
|
+
function wrapInferSelectResultType(typeRefNode, payloadTypeName, selectType) {
|
|
466
|
+
if (typeRefNode.kind === 'NON_NULL' && typeRefNode.ofType) {
|
|
467
|
+
return wrapInferSelectResultType(typeRefNode.ofType, payloadTypeName, selectType);
|
|
468
|
+
}
|
|
469
|
+
if (typeRefNode.kind === 'LIST' && typeRefNode.ofType) {
|
|
470
|
+
return t.tsArrayType(wrapInferSelectResultType(typeRefNode.ofType, payloadTypeName, selectType));
|
|
471
|
+
}
|
|
472
|
+
return inferSelectResultType(payloadTypeName, selectType);
|
|
473
|
+
}
|
|
474
|
+
function typeRefToTsTypeAST(typeRefNode) {
|
|
475
|
+
if (typeRefNode.kind === 'NON_NULL' && typeRefNode.ofType) {
|
|
476
|
+
return typeRefToTsTypeAST(typeRefNode.ofType);
|
|
477
|
+
}
|
|
478
|
+
if (typeRefNode.kind === 'LIST' && typeRefNode.ofType) {
|
|
479
|
+
return t.tsArrayType(typeRefToTsTypeAST(typeRefNode.ofType));
|
|
480
|
+
}
|
|
481
|
+
if (typeRefNode.kind === 'SCALAR') {
|
|
482
|
+
const tsType = (0, type_resolver_1.scalarToTsType)(typeRefNode.name ?? 'unknown');
|
|
483
|
+
if (tsType === 'string')
|
|
484
|
+
return t.tsStringKeyword();
|
|
485
|
+
if (tsType === 'number')
|
|
486
|
+
return t.tsNumberKeyword();
|
|
487
|
+
if (tsType === 'boolean')
|
|
488
|
+
return t.tsBooleanKeyword();
|
|
489
|
+
return t.tsUnknownKeyword();
|
|
490
|
+
}
|
|
491
|
+
return typeRef(typeRefNode.name ?? 'unknown');
|
|
492
|
+
}
|
|
493
|
+
function buildSelectionArgsCall(selectTypeName) {
|
|
494
|
+
const call = t.callExpression(t.identifier('buildSelectionArgs'), [
|
|
495
|
+
t.memberExpression(t.identifier('params'), t.identifier('selection')),
|
|
496
|
+
]);
|
|
497
|
+
// @ts-ignore - Babel types support typeParameters on CallExpression for TS
|
|
498
|
+
call.typeParameters = t.tsTypeParameterInstantiation([
|
|
499
|
+
typeRef(selectTypeName),
|
|
500
|
+
]);
|
|
501
|
+
return constDecl('args', call);
|
|
502
|
+
}
|
|
503
|
+
function buildListSelectionArgsCall(selectTypeName, filterTypeName, orderByTypeName) {
|
|
504
|
+
const call = t.callExpression(t.identifier('buildListSelectionArgs'), [
|
|
505
|
+
t.memberExpression(t.identifier('params'), t.identifier('selection')),
|
|
506
|
+
]);
|
|
507
|
+
// @ts-ignore - Babel types support typeParameters on CallExpression for TS
|
|
508
|
+
call.typeParameters = t.tsTypeParameterInstantiation([
|
|
509
|
+
typeRef(selectTypeName),
|
|
510
|
+
typeRef(filterTypeName),
|
|
511
|
+
typeRef(orderByTypeName),
|
|
512
|
+
]);
|
|
513
|
+
return constDecl('args', call);
|
|
514
|
+
}
|
|
515
|
+
function customSelectResultTypeLiteral(opName, returnType, payloadTypeName, selectType) {
|
|
516
|
+
return typeLiteralWithProps([
|
|
517
|
+
{
|
|
518
|
+
name: opName,
|
|
519
|
+
type: wrapInferSelectResultType(returnType, payloadTypeName, selectType),
|
|
520
|
+
},
|
|
521
|
+
]);
|
|
522
|
+
}
|
package/core/codegen/index.d.ts
CHANGED
|
@@ -2,29 +2,28 @@
|
|
|
2
2
|
* Code generation orchestrator
|
|
3
3
|
*
|
|
4
4
|
* Coordinates all code generators to produce the complete SDK output.
|
|
5
|
+
* Hooks delegate to ORM model methods - types are imported from ORM's input-types.ts.
|
|
5
6
|
*
|
|
6
7
|
* Output structure:
|
|
7
8
|
* generated/
|
|
8
9
|
* index.ts - Main barrel export
|
|
9
|
-
* client.ts -
|
|
10
|
-
* types.ts - Entity interfaces and filter types
|
|
10
|
+
* client.ts - ORM client wrapper (configure/getClient)
|
|
11
11
|
* queries/
|
|
12
12
|
* index.ts - Query hooks barrel
|
|
13
|
-
* useCarsQuery.ts - List query hook
|
|
14
|
-
* useCarQuery.ts - Single item query hook
|
|
15
|
-
* useCurrentUserQuery.ts - Custom query hook
|
|
13
|
+
* useCarsQuery.ts - List query hook -> ORM findMany
|
|
14
|
+
* useCarQuery.ts - Single item query hook -> ORM findOne
|
|
15
|
+
* useCurrentUserQuery.ts - Custom query hook -> ORM query.xxx
|
|
16
16
|
* ...
|
|
17
17
|
* mutations/
|
|
18
18
|
* index.ts - Mutation hooks barrel
|
|
19
|
-
* useCreateCarMutation.ts -
|
|
20
|
-
* useUpdateCarMutation.ts
|
|
21
|
-
* useDeleteCarMutation.ts
|
|
22
|
-
* useLoginMutation.ts - Custom mutation
|
|
23
|
-
* useRegisterMutation.ts
|
|
19
|
+
* useCreateCarMutation.ts - -> ORM create
|
|
20
|
+
* useUpdateCarMutation.ts - -> ORM update
|
|
21
|
+
* useDeleteCarMutation.ts - -> ORM delete
|
|
22
|
+
* useLoginMutation.ts - Custom mutation -> ORM mutation.xxx
|
|
24
23
|
* ...
|
|
25
24
|
*/
|
|
26
|
-
import type { CleanTable, CleanOperation, TypeRegistry } from '../../types/schema';
|
|
27
25
|
import type { GraphQLSDKConfigTarget } from '../../types/config';
|
|
26
|
+
import type { CleanOperation, CleanTable, TypeRegistry } from '../../types/schema';
|
|
28
27
|
export interface GeneratedFile {
|
|
29
28
|
/** Relative path from output directory */
|
|
30
29
|
path: string;
|
|
@@ -72,13 +71,12 @@ export declare function generateAllFiles(tables: CleanTable[], config: GraphQLSD
|
|
|
72
71
|
* (they're expected to exist in the shared types directory).
|
|
73
72
|
*/
|
|
74
73
|
export declare function generate(options: GenerateOptions): GenerateResult;
|
|
74
|
+
export { generateCustomMutationsBarrel, generateCustomQueriesBarrel, generateMainBarrel, generateMutationsBarrel, generateQueriesBarrel, } from './barrel';
|
|
75
75
|
export { generateClientFile } from './client';
|
|
76
|
-
export { generateTypesFile } from './types';
|
|
77
|
-
export { generateAllQueryHooks, generateListQueryHook, generateSingleQueryHook, } from './queries';
|
|
78
|
-
export { generateAllMutationHooks, generateCreateMutationHook, generateUpdateMutationHook, generateDeleteMutationHook, } from './mutations';
|
|
79
|
-
export { generateAllCustomQueryHooks, generateCustomQueryHook, } from './custom-queries';
|
|
80
76
|
export { generateAllCustomMutationHooks, generateCustomMutationHook, } from './custom-mutations';
|
|
81
|
-
export {
|
|
82
|
-
export { generateQueryKeysFile } from './query-keys';
|
|
83
|
-
export { generateMutationKeysFile } from './mutation-keys';
|
|
77
|
+
export { generateAllCustomQueryHooks, generateCustomQueryHook, } from './custom-queries';
|
|
84
78
|
export { generateInvalidationFile } from './invalidation';
|
|
79
|
+
export { generateMutationKeysFile } from './mutation-keys';
|
|
80
|
+
export { generateAllMutationHooks, generateCreateMutationHook, generateDeleteMutationHook, generateUpdateMutationHook, } from './mutations';
|
|
81
|
+
export { generateAllQueryHooks, generateListQueryHook, generateSingleQueryHook, } from './queries';
|
|
82
|
+
export { generateQueryKeysFile } from './query-keys';
|