Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1cb5233

Browse files
committed
fix: create a custom scalars map with some Hasura specific types
1 parent b913dd6 commit 1cb5233

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const customScalarsMap: Record<string, string> = {
2+
uuid: 'string',
3+
timestamptz: 'string',
4+
json: 'string',
5+
jsonb: 'string',
6+
numeric: 'number',
7+
};

src/TreeToTS/templates/resolveValueTypes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Options, ParserField } from '@/Models';
22
import { Helpers, TypeDefinition, TypeSystemDefinition } from '@/Models/Spec';
33
import { truthyType } from '@/TreeToTS/templates/truthy';
4+
import { customScalarsMap } from './customScalarsMap';
45

56
export const VALUETYPES = 'ValueTypes';
67

@@ -16,6 +17,7 @@ const typeScriptMap: Record<GqlTypes, TSTypes> = {
1617
ID: 'string',
1718
String: 'string',
1819
};
20+
1921
const toTypeScriptPrimitive = (a: GqlTypes): string => typeScriptMap[a] || a;
2022

2123
const plusDescription = (description?: string, prefix = ''): string =>
@@ -72,6 +74,11 @@ const resolveValueTypeFromRoot = (i: ParserField, rootNodes: ParserField[], enum
7274
return '';
7375
}
7476

77+
if (i.data.type === TypeDefinition.ScalarTypeDefinition) {
78+
const type = customScalarsMap[i.name] || 'unknown';
79+
return `${plusDescription(i.description)}["${i.name}"]:${type}`;
80+
}
81+
7582
if (!i.args || !i.args.length) {
7683
return `${plusDescription(i.description)}["${i.name}"]:unknown`;
7784
}

src/TreeToTS/templates/returnedModelTypes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Options, ParserField } from '@/Models';
22
import { TypeDefinition, TypeSystemDefinition } from '@/Models/Spec';
33
import { TYPES } from './returnedTypes';
4+
import { customScalarsMap } from './customScalarsMap';
45

56
export const MODEL_TYPES = 'ModelTypes';
67

@@ -11,6 +12,7 @@ const typeScriptMap: Record<string, string> = {
1112
ID: 'string',
1213
String: 'string',
1314
};
15+
1416
const toTypeScriptPrimitive = (a: string): string => typeScriptMap[a] || `${MODEL_TYPES}["${a}"]`;
1517

1618
const plusDescription = (description?: string, prefix = ''): string =>
@@ -57,7 +59,8 @@ const resolveTypeFromRoot = (i: ParserField, rootNodes: ParserField[]): string =
5759
return '';
5860
}
5961
if (i.data.type === TypeDefinition.ScalarTypeDefinition) {
60-
return `${plusDescription(i.description)}["${i.name}"]:any`;
62+
const type = customScalarsMap[i.name] || 'any';
63+
return `${plusDescription(i.description)}["${i.name}"]:${type}`;
6164
}
6265
if (!i.args || !i.args.length) {
6366
return ``;

src/TreeToTS/templates/returnedTypes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Options, ParserField } from '@/Models';
22
import { Helpers, TypeDefinition, TypeSystemDefinition } from '@/Models/Spec';
3+
import { customScalarsMap } from './customScalarsMap';
34

45
export const TYPES = 'GraphQLTypes';
56

@@ -10,6 +11,7 @@ const typeScriptMap: Record<string, string> = {
1011
ID: 'string',
1112
String: 'string',
1213
};
14+
1315
const toTypeScriptPrimitive = (a: string): string => typeScriptMap[a] || `${TYPES}["${a}"]`;
1416

1517
const plusDescription = (description?: string, prefix = ''): string =>
@@ -72,6 +74,10 @@ export const resolveTypeFromRoot = (i: ParserField, rootNodes: ParserField[]): s
7274
if (i.data.type === Helpers.Comment) {
7375
return `// ${i.description}`;
7476
}
77+
if (i.data.type === TypeDefinition.ScalarTypeDefinition) {
78+
const type = customScalarsMap[i.name] || 'any';
79+
return `${plusDescription(i.description)}["${i.name}"]:${type}`;
80+
}
7581
if (!i.args || !i.args.length) {
7682
return `${plusDescription(i.description)}["${i.name}"]:any`;
7783
}

0 commit comments

Comments
 (0)