aws-amplify / aws-amplify/amplify-codegen

When creating a type with a sort key on the id field, codegen is making id required

Open
#639 4 comments 0 reactions 0 assignees View on GitHub
bug p2 transferred
Dominant language
TypeScript
Stars
59
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### Amplify CLI Version

12.1.1

### Question

When creating a type in schema.graphql like this

```ts
type NodeModel
@model
@auth(rules: [{ allow: private}])
{
id: ID! @primaryKey
...
}
```
codegen creates a type like this:

```ts
type LazyNodeModel = {
readonly [__modelMeta__]: {
identifier: OptionallyManagedIdentifier;
};
readonly id: string;
...
}
```
Which allows me to do:

```ts
const node = new NodeModel({
// id not required
// other fields...
})
```

The ID is not required.

But if I add a sort key to the primary key like this:

```ts
type NodeModel
@model
@auth(rules: [{ allow: private}])
{
id: ID! @primaryKey(sortKeyFields: ["someOtherID"])
...
}
```

The generated type is:

```ts
type LazyNodeModel = {
readonly [__modelMeta__]: {
identifier: CompositeIdentifier;
};
readonly id: string;
...
}
```
And when I try to do:
```ts
const node = new NodeModel({
id: 'some-value', // typescript complains if I don't add this
// other fields...
});
```

It's requiring that I specify the ID myself.

Is this a bug or am I misusing the @primarayKey and sortKeyFields directives?

Contributor guide

Open the contributing guide

Research direction

Start with the schema.graphql examples and compare the generated LazyNodeModel and NodeModel types for primary keys with and without sortKeyFields. Trace the codegen path that determines whether id is optional, then verify that a composite primary key still permits the shown constructor usage and add coverage for both schemas if the repository provides a suitable test entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.