aws-amplify / aws-amplify/amplify-codegen

Codegen does not generate correct models & model provider for flutter

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

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-codegen/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-codegen/blob/main/CONTRIBUTING.md#bugs).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

### How did you install the Amplify CLI?

curl, then npm

### If applicable, what version of Node.js are you using?

v16.15.1

### Amplify CLI Version

9.1.0

### What operating system are you using?

windows

### Amplify Codegen Command

codegen models

### Describe the bug

The model provider does not get generated correctly, none of the types in my graphQL query object are being transferred to the getModelTypeByModelName function.

This causes an error because the function is not actually being generated at all
![image](https://user-images.githubusercontent.com/60675668/177902706-e75ba12d-7219-4f0e-93bf-4b02fda7941f.png)

I have noticed that if you add the @model directive onto the type it works fine and generates the correct type, however if you simply use graphql types which are mapped from a rest endpoint (using an http resolver) the model provider interface does not get created.

Ive looked into the associated issues with the error however I believe this is a new bug because the function is actually generated with models when the @model directive is added.

Additionally, the model is incorrect - It appears to be missing a generator classtype which is generated with the @model directive.

### Expected behavior

Regardless of the @model directive we should still create the getModelTypeByModelName with the corresponding models.

The reason why I dont need the @model directive is because the data I am getting is mapped from several REST endpoints and I dont need a dynamo DB to resolve them.

### Reproduction steps

1. create a schema with a type, and query object
2. run codegen models
3. notice model "TestObj" is created, but it isnt included in the model provider & does not have a generator class

### GraphQL schema(s)

```graphql
type TestObj{
testdata: String!
}

type Query {
test_call(body: String!): TestObj!
}

schema {
query: Query
}
```

### Log output

```dart
//error: Missing concrete implementation of 'ModelProviderInterface.getModelTypeByModelName'
class ModelProvider implements ModelProviderInterface {
@override
String version = "5b447b93db4219b9b4e96df9fb440d78";
@override
List modelSchemas = [];
static final ModelProvider _instance = ModelProvider();
@override
List customTypeSchemas = [TestObj.schema];

static ModelProvider get instance => _instance;
}
```

```dart
@immutable
class TestObj {
final String? _testdata;

String get testdata {
try {
return _testdata!;
} catch(e) {
throw new AmplifyCodeGenModelException(
AmplifyExceptionMessages.codeGenRequiredFieldForceCastExceptionMessage,
recoverySuggestion:
AmplifyExceptionMessages.codeGenRequiredFieldForceCastRecoverySuggestion,
underlyingException: e.toString()
);
}
}

const TestObj._internal({required testdata}): _testdata = testdata;

factory TestObj({required String testdata}) {
return TestObj._internal(
testdata: testdata);
}

bool equals(Object other) {
return this == other;
}

@override
bool operator ==(Object other) {
if (identical(other, this)) return true;
return other is TestObj &&
_testdata == other._testdata;
}

@override
int get hashCode => toString().hashCode;

@override
String toString() {
var buffer = new StringBuffer();

buffer.write("TestObj {");
buffer.write("testdata=" + "$_testdata");
buffer.write("}");

return buffer.toString();
}

TestObj copyWith({String? testdata}) {
return TestObj._internal(
testdata: testdata ?? this.testdata);
}

TestObj.fromJson(Map json)
: _testdata = json['testdata'];

Map toJson() => {
'testdata': _testdata
};

static var schema = Model.defineSchema(define: (ModelSchemaDefinition modelSchemaDefinition) {
modelSchemaDefinition.name = "TestObj";
modelSchemaDefinition.pluralName = "TestObjs";

modelSchemaDefinition.addField(ModelFieldDefinition.customTypeField(
fieldName: 'testdata',
isRequired: true,
ofType: ModelFieldType(ModelFieldTypeEnum.string)
));
});
}
```

### Additional information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.