Skip to content

Commit 2ab3967

Browse files
Improved naming of anonymous types and types from files outside of functions.ts (#21)
1 parent 7fa60b3 commit 2ab3967

File tree

12 files changed

+1043
-252
lines changed

12 files changed

+1043
-252
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
88
Changes to be included in the next upcoming release
99

1010
- Improved error messages when unsupported enum types or unions of literal types are found, and allow these types to be used in relaxed types mode ([#17](https://github.com/hasura/ndc-nodejs-lambda/pull/17))
11+
- Improved naming of types that reside outside of the main `functions.ts` file. Type names will now only be prefixed with a disambiguator if there is a naming conflict detected (ie. where two different types use the same name). Anonymous types are now also named in a shorter way. ([#21](https://github.com/hasura/ndc-nodejs-lambda/pull/21))
1112

1213
## [1.1.0] - 2024-02-26
1314
- Updated to [NDC TypeScript SDK v4.2.0](https://github.com/hasura/ndc-sdk-typescript/releases/tag/v4.2.0) to include OpenTelemetry improvements. Traced spans should now appear in the Hasura Console

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ export function sum(nums: number[]): number {
136136
}
137137
```
138138

139+
Anonymous types are supported, but will be automatically named after the first place they are used. It is recommended that you **avoid using anonymous types**. Instead, prefer to name all your types to ensure the type name does not change unexpectedly as you rename usage sites and re-order usages of the anonymous type.
140+
141+
```typescript
142+
export function greet(
143+
name: { firstName: string, surname: string } // This type will be automatically named greet_name
144+
): string {
145+
return `Hello ${name.firstName} ${name.surname}`;
146+
}
147+
```
148+
139149
### Unsupported types
140150

141151
These types are unsupported as function parameter types or return types for functions that you export for invocation from Hasura. You can use whatever types you like _inside_ your function or in related code, however.

ndc-lambda-sdk/src/inference.ts

Lines changed: 424 additions & 145 deletions
Large diffs are not rendered by default.

ndc-lambda-sdk/src/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class JSONValue {
203203
export type TypePathSegment =
204204
{ segmentType: "FunctionParameter", functionName: string, parameterName: string }
205205
| { segmentType: "FunctionReturn", functionName: string }
206-
| { segmentType: "ObjectProperty", typeName: string, propertyName: string }
206+
| { segmentType: "ObjectProperty", typeName: string, preferredTypeName: string, propertyName: string }
207207
| { segmentType: "Array" }
208208
| { segmentType: "TypeParameter", typeName: string, index: number }
209209
| { segmentType: "IndexSignature", typeName: string, sigIndex: number, component: "key" | "value" }

ndc-lambda-sdk/test/inference/basic-inference/basic-inference.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ describe("basic inference", function() {
282282
type: {
283283
type: "named",
284284
kind: "object",
285-
name: "bar_arguments_anonObj"
285+
name: "bar_anonObj"
286286
}
287287
},
288288
{
@@ -372,7 +372,7 @@ describe("basic inference", function() {
372372
type: {
373373
type: "named",
374374
kind: "object",
375-
name: "bar_arguments_anonIntersectionObj"
375+
name: "bar_anonIntersectionObj"
376376
}
377377
},
378378
{
@@ -546,7 +546,7 @@ describe("basic inference", function() {
546546
],
547547
isRelaxedType: false,
548548
},
549-
"bar_arguments_anonIntersectionObj": {
549+
"bar_anonIntersectionObj": {
550550
description: null,
551551
properties: [
552552
{
@@ -570,7 +570,7 @@ describe("basic inference", function() {
570570
],
571571
isRelaxedType: false,
572572
},
573-
"bar_arguments_anonObj": {
573+
"bar_anonObj": {
574574
description: null,
575575
properties: [
576576
{

ndc-lambda-sdk/test/inference/naming-conflicts/conflict-from-import.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

ndc-lambda-sdk/test/inference/naming-conflicts/naming-conflicts.test.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

ndc-lambda-sdk/test/inference/relaxed-types/relaxed-types.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ describe("relaxed types", function() {
487487
"unionTypes": [
488488
"Promise types are not supported, but one was encountered in function 'unionTypes' parameter 'numberOrString', type 'number | Promise<string>' union member index '1'.",
489489
"The void type is not supported, but one was encountered in function 'unionTypes' parameter 'aliasedUnion', type 'AliasedUnion' union member index '1'",
490-
"The never type is not supported, but one was encountered in function 'unionTypes' parameter 'unionedObjects', type '{ prop1: never; } | { prop2: string; }' union member index '0', type 'unionTypes_arguments_unionedObjects_union_0' property 'prop1'",
490+
"The never type is not supported, but one was encountered in function 'unionTypes' parameter 'unionedObjects', type '{ prop1: never; } | { prop2: string; }' union member index '0', type '{ prop1: never; }' property 'prop1'",
491491
]
492492
},
493493
functionsSchema: {
@@ -971,6 +971,7 @@ describe("relaxed types", function() {
971971
{
972972
segmentType: "ObjectProperty",
973973
typeName: "ObjectWithRelaxedType",
974+
preferredTypeName: "ObjectWithRelaxedType",
974975
propertyName: "prop",
975976
},
976977
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type AliasedObjectType = {
2+
fullName: { firstName: string, surname: string }
3+
intersectionFullName: { firstName: string } & { surname: string }
4+
nestedType: {
5+
coordinates: { x: number, y: number },
6+
nestedFullName: { firstName: string, surname: string },
7+
nestedIntersectionFullName: { firstName: string } & { surname: string }
8+
},
9+
}
10+
11+
type GenericAliasedObjectType<T> = {
12+
data: T
13+
nestedAnonymous: { prop: T }
14+
}
15+
16+
/** @readonly */
17+
export function anonymousTypes(
18+
dob: { year: number, month: number, day: number },
19+
aliasedObjectType: AliasedObjectType,
20+
stringGenericAliasedObjectType: GenericAliasedObjectType<string>,
21+
numberGenericAliasedObjectType: GenericAliasedObjectType<number>,
22+
): { successful: boolean } {
23+
return { successful: true };
24+
}

ndc-lambda-sdk/test/inference/naming-conflicts/conflict-from-import.dep.ts renamed to ndc-lambda-sdk/test/inference/type-naming/imported-types.dep.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export type AnotherType = {
2+
prop: string
3+
}
4+
15
export type Foo = {
26
a: string,
37
b: number

0 commit comments

Comments
 (0)