Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
Changes to be included in the next upcoming release

- 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))
- 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))

## [1.1.0] - 2024-02-26
- 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
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ export function sum(nums: number[]): number {
}
```

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.

```typescript
export function greet(
name: { firstName: string, surname: string } // This type will be automatically named greet_name
): string {
return `Hello ${name.firstName} ${name.surname}`;
}
```

### Unsupported types

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.
Expand Down
569 changes: 424 additions & 145 deletions ndc-lambda-sdk/src/inference.ts
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is getting pretty gnarly.

Copy link
Collaborator Author

@daniel-chambers daniel-chambers Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, might pull it apart in another PR another time. There's still likely to be a biggish file though, I'd keep all the type derivation functions together.

At least it's not 2.5MB like TypeScript! 😂

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ndc-lambda-sdk/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class JSONValue {
export type TypePathSegment =
{ segmentType: "FunctionParameter", functionName: string, parameterName: string }
| { segmentType: "FunctionReturn", functionName: string }
| { segmentType: "ObjectProperty", typeName: string, propertyName: string }
| { segmentType: "ObjectProperty", typeName: string, preferredTypeName: string, propertyName: string }
| { segmentType: "Array" }
| { segmentType: "TypeParameter", typeName: string, index: number }
| { segmentType: "IndexSignature", typeName: string, sigIndex: number, component: "key" | "value" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe("basic inference", function() {
type: {
type: "named",
kind: "object",
name: "bar_arguments_anonObj"
name: "bar_anonObj"
}
},
{
Expand Down Expand Up @@ -372,7 +372,7 @@ describe("basic inference", function() {
type: {
type: "named",
kind: "object",
name: "bar_arguments_anonIntersectionObj"
name: "bar_anonIntersectionObj"
}
},
{
Expand Down Expand Up @@ -546,7 +546,7 @@ describe("basic inference", function() {
],
isRelaxedType: false,
},
"bar_arguments_anonIntersectionObj": {
"bar_anonIntersectionObj": {
description: null,
properties: [
{
Expand All @@ -570,7 +570,7 @@ describe("basic inference", function() {
],
isRelaxedType: false,
},
"bar_arguments_anonObj": {
"bar_anonObj": {
description: null,
properties: [
{
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ describe("relaxed types", function() {
"unionTypes": [
"Promise types are not supported, but one was encountered in function 'unionTypes' parameter 'numberOrString', type 'number | Promise<string>' union member index '1'.",
"The void type is not supported, but one was encountered in function 'unionTypes' parameter 'aliasedUnion', type 'AliasedUnion' union member index '1'",
"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'",
"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'",
]
},
functionsSchema: {
Expand Down Expand Up @@ -971,6 +971,7 @@ describe("relaxed types", function() {
{
segmentType: "ObjectProperty",
typeName: "ObjectWithRelaxedType",
preferredTypeName: "ObjectWithRelaxedType",
propertyName: "prop",
},
]
Expand Down
24 changes: 24 additions & 0 deletions ndc-lambda-sdk/test/inference/type-naming/anonymous-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type AliasedObjectType = {
fullName: { firstName: string, surname: string }
intersectionFullName: { firstName: string } & { surname: string }
nestedType: {
coordinates: { x: number, y: number },
nestedFullName: { firstName: string, surname: string },
nestedIntersectionFullName: { firstName: string } & { surname: string }
},
}

type GenericAliasedObjectType<T> = {
data: T
nestedAnonymous: { prop: T }
}

/** @readonly */
export function anonymousTypes(
dob: { year: number, month: number, day: number },
aliasedObjectType: AliasedObjectType,
stringGenericAliasedObjectType: GenericAliasedObjectType<string>,
numberGenericAliasedObjectType: GenericAliasedObjectType<number>,
): { successful: boolean } {
return { successful: true };
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export type AnotherType = {
prop: string
}

export type Foo = {
a: string,
b: number
Expand Down
36 changes: 36 additions & 0 deletions ndc-lambda-sdk/test/inference/type-naming/imported-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as dep from './imported-types.dep';
import * as commander from 'commander';

/** @readonly */
export function npmTypeImport(): commander.HelpContext {
return { error: false };
}

/** @readonly */
export function localTypeImport(): dep.AnotherType {
return { prop: "" };
}

type Foo = {
x: boolean,
y: dep.Foo
}

export function conflictWithLocalImport(): Foo {
return {
x: true,
y: {
a: 'hello',
b: 33
}
}
}

type ErrorOptions = {
retval: number
}

/** @readonly */
export function conflictWithNpmImport(myErrorOptions: ErrorOptions): commander.ErrorOptions {
return { exitCode: myErrorOptions.retval };
}
Loading