aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 245 forks source link

DataStore - Nested types are not supported #260

Closed kjones closed 2 years ago

kjones commented 3 years ago

An example would be the following:

type TestModel @model {
  id: ID!
  nested: NestedType!
  listOfNested: [NestedType!]!
}

type NestedType {
  field1: Int!
  field2: String!
}

This schema currently results in a SubSelectionRequired response error from AppSync when establishing subscriptions.

raywhiteside commented 3 years ago

To expound upon a major aspect of the issue here, I'm not seeing NestedType have a class generated for it. While the NestedType is not defined as a @model, it does cause a NestedType.dart file to be generated in lib/models. However this is the entirety of the generated file:

/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*  http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
import 'package:flutter/foundation.dart';

Not even a constructor is generated. This class seems to be correctly referenced in the generated TestModel.dart, though the class is imported in neither TestModel.dart nor ModelProvider.dart (the latter of which may be correct as NestedType is not a @model).

I'm a bit confused how the OP got to an AppSync response error in this case, as the code generation issues I describe lead to compile errors. Perhaps my issue isn't quite the same, but does seem to be related to custom nested types.

Additionally I think it's fair to tag this more strongly than a feature request, as I seem to be generating code that doesn't compile from a successful run of amplify codegen models. I suspect this feature will be accepted and added, but otherwise codegen should throw an error.

kjones commented 3 years ago

There are a bunch of problems with the Amplify CLI when generating Flutter models so to produce the SubSelectionRequired error, I was experimenting with rolling my own definitions for the nested types. While doing that I discovered that nested type support in the Flutter model schema definitions was left out on purpose.

Furthermore, I found that it doesn't look like the Android SerializedModel schema definition selection set generation code supports nested types. Nested types in native Android Java models appears to work fine.

For reference: Android SerializedModel schema definition selection set generation Android Java model selection set generation iOS selection set generation

Agree that this should be marked with a little more urgency than a feature request. Flutter DataStore support at this point is pretty much useless for anything other than trivial @model schemas due to various problems with CLI codegen, Android, and Flutter SDKs. I'm hoping the DataStore feature gets a revisit and some much needed ❤️ soon.

rlee1990 commented 3 years ago

I too have this same issue when trying to use codegen and using the S3Object example with graphql schema

kjones commented 3 years ago

I would like to request that this issue be turned into a bug instead of a feature request in hopes of elevating its priority.

Nested types are supported on all other platforms and without this support I am still resorting to using my own custom DataStore plugin for Flutter (described here) I created back in September.

Do you know of any workarounds or hacks that would let me switch to the official DataStore Flutter SDK plugin when the backend GraphQL schema is already in use by other platforms? I thought I might be able to just switch the local schema to use AWSJSON as the field type, generate the Flutter models, and then deserialize these nested types myself. However, that doesn't work because the backend AppSync requests/sync/subscriptions are expecting a specific format and I get the SubSelectionRequired error mentioned in previous comments.

fjnoyp commented 3 years ago

Hi guys thank you all for explaining the issue encountered. @kjones we will mark this as a bug as well and investigate this more on our side.

Alexminator99 commented 3 years ago

I got the same problem now. Awesome!!!

type S3Object { bucket: String! region: String! key: String! }

type DocumentPicture @model (...) { id: ID! name: String owner: String timestamp: AWSDate file: S3Object! }

It only generates the licence header and import 2 .dart archives. Thats it for S3Object. Is there is a workaround?

Im in latest version -> 4.48.0.

lfraker commented 3 years ago

Running into the same issue. Are there any updates on the investigation/fix?

hackrx commented 3 years ago

I am also facing the same issue, any updates on this?

ChaitanyaVootla commented 3 years ago

I'm unable to create and use any 1:n relationship, which is a very basic use case, can someone please tell me if there is a workaround

HuiSF commented 3 years ago

Hi @ChaitanyaVootla sorry for missing your comment above. I wanted to follow up with the use case you mentioned.

I'm unable to create and use any 1:n relationship

What are you actually referring to by 1:n relationship?

e.g. 1:n (hasMany relationship) this is supported by amplify-flutter DataStore today.

type Post @model {
  id: ID!
  title: String!
  comments: [Comment] @connection(keyName: "byPost", fields: ["id"])
}

type Comment @model
  @key(name: "byPost", fields: ["postID", "content"]) {
  id: ID!
  postID: ID!
  content: String!
}

Or are you referring to collection of a CustomType? (this feature is in progress) e.g.

type Album @model {
  id: ID!
  name: String!
  pictures: [S3Object]
}

type S3Object {
  bucket: String!
  region: String!
  key: String!
}
HuiSF commented 2 years ago

Hi all, amplify-flutter 0.3.0-rc.2 has been release which includes the support of CustomType.

This preview release requires a corresponding preview release of @aws-amplify/cli. Please refer to the changelog to get the detail how to start using CustomType with this preview release. Please test with your project, and follow up if you encounter any issues. Feedback is much appreciated!

Ashish-Nanda commented 2 years ago

@kjones @raywhiteside Were you able to give the preview release a try?

raywhiteside commented 2 years ago

I have not, sorry.

kjones commented 2 years ago

I have not had a chance to try this either and probably won't be able to get to this for a few weeks.

djorgji commented 2 years ago

Done a bit of preliminary testing with 0.3.0-rc.3 and 7.7.0-flutter-preview.1 it seems to be working, still need to do a bit more testing, but so far so good.

HuiSF commented 2 years ago

CustomType is now supported with version 0.3.0. Thanks everyone.