Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error syncing model #3137

Closed
cappy123abc opened this issue Aug 10, 2023 · 8 comments
Closed

Error syncing model #3137

cappy123abc opened this issue Aug 10, 2023 · 8 comments
Assignees
Labels
bug Something isn't working datastore Issues related to the DataStore category

Comments

@cappy123abc
Copy link

cappy123abc commented Aug 10, 2023

Describe the bug

When datastore sync runs I get the following error for one of the models other models are syncing this schema is being used in a react native app and the syncing works fine leading me to think codegen might be the issue

DataStoreError: An error occurred syncing Inspection
Caused by:
DataStoreError: Failed to decode GraphQL response to the `ResponseType` PaginatedList<AnyModel>
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
GraphQLResponseError<PaginatedList<AnyModel>>: Failed to decode GraphQL response to the `ResponseType` PaginatedList<AnyModel>
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
APIError: keyNotFound key CodingKeys(stringValue: "project", intValue: nil)
Caused by:
keyNotFound(CodingKeys(stringValue: "project", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "items", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "phase", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"project\", intValue: nil) (\"project\").", underlyingError: nil))

The relevant part of the schema for this model is:

type Inspection @model(subscriptions: {level: public}) @auth(rules: [
  {allow: owner, operations: [create, update, delete]}, 
  {allow: groups, groups: ["ADMIN", "REVIEWER"], operations: [update, delete, read]}, 
  {allow: groups, groupsField: "groupId", operations: [update, delete, read]}, 
  {allow: public, operations: [update, read]}
  ]) 
  @searchable {
  id: ID! @primaryKey
  groupId: String
  clientId: ID
  projectId: ID! @index(name: "byProject", queryField: "inspectionsByProjectId")
  project: Project @belongsTo(fields: ["projectId"])
  phase: Phase @hasOne(fields: ["phaseId"])
  phaseId: ID
  photoSequence: [ID]
  status: String!
  userId: ID! @index(name: "byUser", queryField: "inspectionsByUserId")
  user: User @belongsTo(fields: ["userId"])
  reviewer: User @belongsTo(fields: ["inspectionReviewerId"])
  inspectionReviewerId: ID
  inspectionDate: String
  reviewedDate: String
  deliveredDate: String
  nextInspectionDate: String
  arrivalTime: String
  checkInContact: Contact @hasOne
  notes: String
  coverLetter: String
  comments: [InspectionComment] @hasMany(indexName: "byInspection", fields: ["id"])
  observations: [Observation] @hasMany(indexName: "byInspection", fields: ["id"])
  findings: [Finding] @hasMany(indexName: "byInspection", fields: ["id"])
  hierarchyItems: [InspectionHierarchyItem] @hasMany(indexName: "byInspection", fields: ["id"])
  photos: [InspectionPhoto] @hasMany(indexName: "byInspection", fields: ["id"])
  scopeHierarchyItems: [String]
  scope: AWSJSON
  number: String
  code: String
  report: S3Object
  weather: AWSJSON
  milestoneBundleIds: AWSJSON
  milestoneTrackers: [MilestoneTracker]
}

Steps To Reproduce

Steps to reproduce the behavior:
1. Build app in XCode and run on iPad 10th gen simulator

Expected behavior

Expected model to sync

Amplify Framework Version

2.12

Amplify Categories

API, DataStore

Dependency manager

Swift PM

Swift version

5.8.1

CLI version

10.8.1

Xcode version

14.3.1

Relevant log output

<details>
<summary>Log Messages</summary>
DataStoreError: An error occurred syncing Inspection
Caused by:
DataStoreError: Failed to decode GraphQL response to the `ResponseType` PaginatedList<AnyModel>
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
GraphQLResponseError<PaginatedList<AnyModel>>: Failed to decode GraphQL response to the `ResponseType` PaginatedList<AnyModel>
Recovery suggestion: Failed to transform to `ResponseType`.
Take a look at the `RawGraphQLResponse` and underlying error to see where it failed to decode.
Caused by:
APIError: keyNotFound key CodingKeys(stringValue: "project", intValue: nil)
Caused by:
keyNotFound(CodingKeys(stringValue: "project", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "items", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "phase", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"project\", intValue: nil) (\"project\").", underlyingError: nil))
</details>

Is this a regression?

No

Regression additional context

No response

Platforms

macOS

OS Version

13.1

Device

10th gen iPad sim

Specific to simulators

No response

Additional context

No response

@lawmicha
Copy link
Contributor

Hey @cappy123abc, we haven't tested using a type that has both @searchable and @model in Swift DataStore, this means we're not sure if it is supported. If it's syncing properly with React Native, this leads me to believe the backend should have the necessary APIs for DataStore to operate properly. I'm also not familiar with the the subscriptions level set to public and how that impacts the auth rule's read operations, i'm assuming subscriptions public allow all users regardless of auth type to subscribe. From what I can gather, we can reproduce the issue with a simplified schema:

type Inspection @model(subscriptions: {level: public}) @auth(rules: [
  {allow: owner, operations: [create, update, delete]}, 
  {allow: groups, groups: ["ADMIN", "REVIEWER"], operations: [update, delete, read]}, 
  {allow: groups, groupsField: "groupId", operations: [update, delete, read]}, 
  {allow: public, operations: [update, read]}
  ]) 
  @searchable {
 id: ID! @primaryKey
 number: String
 projectId: ID! @index(name: "byProject", queryField: "inspectionsByProjectId")
 project: Project @belongsTo(fields: ["projectId"])
}

The error mentions keyNotFound(CodingKeys(stringValue: "project", which is why i've included that relationship in the Inspection type above, could you provide us with how you've defined the Project type as well?

@lawmicha lawmicha added bug Something isn't working datastore Issues related to the DataStore category labels Aug 11, 2023
@cappy123abc
Copy link
Author

cappy123abc commented Aug 11, 2023

Here is the Project model definition.

type Project @model @auth(rules: [
  {allow: owner, operations: [create]}, 
  {allow: private, operations: [update, delete, read]}, 
  {allow: public, operations: [read]}
  ]) 
  @searchable {
  id: ID! @primaryKey
  groupId: String
  name: String!
  status: String!
  clientId: ID! @index(name: "byClient", queryField: "projectsByClientId")
  client: Client @belongsTo(fields: ["clientId"])
  eft: User @hasOne
  reviewer: User @hasOne
  admin: User @hasOne
  address: Address!
  primaryContact: Contact @hasOne
  reportTemplateId: String
  reportDisclaimer: String
  inspections: [Inspection] @hasMany(indexName: "byProject", fields: ["id"])
  contacts: [ProjectContact] @hasMany(indexName: "byProject", fields: ["id"])
  distributionList: AWSJSON
  externalLinks: [ExternalLink] @hasMany(indexName: "byProject", fields: ["id"])
  tags: [Tag] @hasMany(indexName: "byProject", fields: ["id"])
  findings: [Finding] @hasMany(indexName: "byProject", fields: ["id"])
  phases: [Phase] @hasMany(indexName: "byProject", fields: ["id"])
  hierarchyItemExclusions: [String]
  exclusions: AWSJSON
  notes: String
  code: String
  milestoneTemplate: MilestoneTemplate @hasOne(fields: ["milestoneTemplateId"])
  milestoneTemplateId: ID
  externalId: String
}

@cappy123abc
Copy link
Author

cappy123abc commented Aug 11, 2023

And some of the graphql response........ it appear the project is coming through fine.

[
    "syncInspections": Amplify.JSONValue.object([
        "items": Amplify.JSONValue.array([Amplify.JSONValue.object([
                "scope": Amplify.JSONValue.string("[]"),
                "status": Amplify.JSONValue.string("SENT"),
                "report": Amplify.JSONValue.null,
                "id": Amplify.JSONValue.string("xxxxxxx"),
                "arrivalTime": Amplify.JSONValue.string("1:30 PM"),
                "checkInContact": Amplify.JSONValue.object([
                    "_lastChangedAt": Amplify.JSONValue.number(1667921648335.0),
                    "createdAt": Amplify.JSONValue.string("2021-07-27T18:50:22.335Z"),
                    "lastName": Amplify.JSONValue.string("xxxxxxx"),
                    "id": Amplify.JSONValue.string("xxxxxxxxx"),
                    "email": Amplify.JSONValue.string("xxxxxxxxx"),
                    "companyName": Amplify.JSONValue.string("xxxxxxx"),
                    "_deleted": Amplify.JSONValue.null,
                    "phone": Amplify.JSONValue.string("8482026530"),
                    "firstName": Amplify.JSONValue.string("xxxxxxx "),
                    "updatedAt": Amplify.JSONValue.string("2022-11-08T15:34:08.310Z"),
                    "_version": Amplify.JSONValue.number(322.0),
                    "__typename": Amplify.JSONValue.string("Contact"),
                    "type": Amplify.JSONValue.string("CONTACT"),
                    "jobTitle": Amplify.JSONValue.string("xxxxxxx")
                ]),
                "clientId": Amplify.JSONValue.string("xxxxxxx"),
                "user": Amplify.JSONValue.object([
                    "status": Amplify.JSONValue.string("ACTIVE"),
                    "firstName": Amplify.JSONValue.string("xxxxxxx"),
                    "_lastChangedAt": Amplify.JSONValue.number(1669139129920.0),
                    "_version": Amplify.JSONValue.number(696.0),
                    "type": Amplify.JSONValue.string("xxxxxxx"),
                    "phone": Amplify.JSONValue.string("xxxxxxx"),
                    "email": Amplify.JSONValue.string("xxxxxxx"),
                    "updatedAt": Amplify.JSONValue.string("2022-11-22T17:45:29.919Z"),
                    "createdAt": Amplify.JSONValue.string("2021-07-20T19:56:00.070Z"),
                    "metadata": Amplify.JSONValue.string(""),
                    "__typename": Amplify.JSONValue.string("User"),
                    "favoriteProjects": Amplify.JSONValue.string("xxxxxxx"),
                    "jobTitle": Amplify.JSONValue.string("xxxxxxx"),
                    "companyName": Amplify.JSONValue.string("xxxxxxx"),
                    "lastName": Amplify.JSONValue.string("xxxxxxx"),
                    "projects": Amplify.JSONValue.null,
                    "_deleted": Amplify.JSONValue.null,
                    "id": Amplify.JSONValue.string("xxxxxxx")
                ]),
                "phase": Amplify.JSONValue.object([
                    "groupId": Amplify.JSONValue.string("xxxxxxx"),
                    "__typename": Amplify.JSONValue.string("Phase"),
                    "_version": Amplify.JSONValue.number(8.0),
                    "status": Amplify.JSONValue.string("ACTIVE"),
                    "id": Amplify.JSONValue.string("xxxxxxx"),
                    "_lastChangedAt": Amplify.JSONValue.number(1657027799413.0),
                    "units": Amplify.JSONValue.array([Amplify.JSONValue.object([
                            "__typename": Amplify.JSONValue.string("Unit"),
                            "id": Amplify.JSONValue.string("xxxxxxx"),
                            "name": Amplify.JSONValue.string("201")
                        ]), Amplify.JSONValue.object([
                            "id": Amplify.JSONValue.string("xxxxxxx"),
                            "name": Amplify.JSONValue.string("202"),
                            "__typename": Amplify.JSONValue.string("Unit")
                        ]), Amplify.JSONValue.object([
                            "__typename": Amplify.JSONValue.string("Unit"),
                            "name": Amplify.JSONValue.string("203"),
                            "id": Amplify.JSONValue.string("xxxxxxx")
                        ]), Amplify.JSONValue.object([
                            "name": Amplify.JSONValue.string("204"),
                            "__typename": Amplify.JSONValue.string("Unit"),
                            "id": Amplify.JSONValue.string("xxxxxxx")
                        ]), Amplify.JSONValue.object([
                            "__typename": Amplify.JSONValue.string("Unit"),
                            "name": Amplify.JSONValue.string("205"),
                            "id": Amplify.JSONValue.string("xxxxxxx")
                        ]), Amplify.JSONValue.object([
                            "id": Amplify.JSONValue.string("xxxxxxx"),
                            "__typename": Amplify.JSONValue.string("Unit"),
                            "name": Amplify.JSONValue.string("206")
                        ])
                    ]),
                    "milestoneTemplateId": Amplify.JSONValue.string("xxxxxxx"),
                    "createdAt": Amplify.JSONValue.string("2022-07-05T13:26:39.133Z"),
                    "name": Amplify.JSONValue.string("Bld 2"),
                    "updatedAt": Amplify.JSONValue.string("2022-07-05T13:29:59.375Z"),
                    "_deleted": Amplify.JSONValue.null
                ]),
                "updatedAt": Amplify.JSONValue.string("2022-07-13T18:55:47.701Z"),
                "_deleted": Amplify.JSONValue.null,
                "scopeHierarchyItems": Amplify.JSONValue.null,
                "reviewedDate": Amplify.JSONValue.string("2022-07-13T18:22:51.652Z"),
                "_lastChangedAt": Amplify.JSONValue.number(1657738547730.0),
                "reviewer": Amplify.JSONValue.object([
                    "createdAt": Amplify.JSONValue.string("2021-04-19T14:26:20.810Z"),
                    "firstName": Amplify.JSONValue.string("xxxxxxx"),
                    "_version": Amplify.JSONValue.number(759.0),
                    "favoriteProjects": Amplify.JSONValue.null,
                    "_deleted": Amplify.JSONValue.null,
                    "_lastChangedAt": Amplify.JSONValue.number(1669209371197.0),
                    "metadata": Amplify.JSONValue.null,
                    "jobTitle": Amplify.JSONValue.string("xxxxxxx"),
                    "updatedAt": Amplify.JSONValue.string("2022-11-23T13:16:11.175Z"),
                    "projects": Amplify.JSONValue.null,
                    "lastName": Amplify.JSONValue.string("xxxxxxx"),
                    "__typename": Amplify.JSONValue.string("User"),
                    "email": Amplify.JSONValue.string("xxxxxxx"),
                    "id": Amplify.JSONValue.string("xxxxxxx"),
                    "phone": Amplify.JSONValue.string("xxxxxxx"),
                    "type": Amplify.JSONValue.string("REVIEWER"),
                    "companyName": Amplify.JSONValue.string("xxxxxxx"),
                    "status": Amplify.JSONValue.string("ACTIVE")
                ]),
                "deliveredDate": Amplify.JSONValue.string("2022-07-13T18:55:46.841Z"),
                "coverLetter": Amplify.JSONValue.null,
                "weather": Amplify.JSONValue.string("xxxxxxx"),
                "inspectionDate": Amplify.JSONValue.string("2022-07-11T12:00:00.000Z"),
                "milestoneBundleIds": Amplify.JSONValue.string("[\"xxxxxxx",\"xxxxxxx,\"xxxxxxx",\"xxxxxxx"),
                "__typename": Amplify.JSONValue.string("Inspection"),
                "nextInspectionDate": Amplify.JSONValue.null,
                "number": Amplify.JSONValue.string("1"),
                "project": Amplify.JSONValue.object([
                    "name": Amplify.JSONValue.string("xxxxxxx"),
                    "projectReviewerId": Amplify.JSONValue.string("xxxxxxx"),
                    "reportTemplateId": Amplify.JSONValue.null,
                    "_lastChangedAt": Amplify.JSONValue.number(1669139131476.0),
                    "__typename": Amplify.JSONValue.string("Project"),
                    "address": Amplify.JSONValue.object([
                        "state": Amplify.JSONValue.string("xxxxxxx"),
                        "city": Amplify.JSONValue.string("xxxxxxx"),
                        "street": Amplify.JSONValue.string("xxxxxxx"),
                        "street2": Amplify.JSONValue.null,
                        "zip": Amplify.JSONValue.string("xxxxxxx"),
                        "__typename": Amplify.JSONValue.string("Address")
                    ]),
                    "projectPrimaryContactId": Amplify.JSONValue.null,
                    "createdAt": Amplify.JSONValue.string("2021-07-08T13:23:47.469Z"),
                    "id": Amplify.JSONValue.string("xxxxxxx"),
                    "reportDisclaimer": Amplify.JSONValue.null,
                    "status": Amplify.JSONValue.string("ACTIVE"),
                    "_version": Amplify.JSONValue.number(441.0),
                    "distributionList": Amplify.JSONValue.null,
                    "hierarchyItemExclusions": Amplify.JSONValue.null,
                    "notes": Amplify.JSONValue.null,
                    "projectAdminId": Amplify.JSONValue.string("xxxxxxx"),
                    "groupId": Amplify.JSONValue.string("xxxxxxx"),
                    "updatedAt": Amplify.JSONValue.string("2022-11-22T17:45:31.454Z"),
                    "code": Amplify.JSONValue.string("xxxxxxx"),
                    "exclusions": Amplify.JSONValue.null,
                    "_deleted": Amplify.JSONValue.null,
                    "projectEftId": Amplify.JSONValue.string("xxxxxxx"),
                    "externalId": Amplify.JSONValue.string("xxxxxxx"),
                    "milestoneTemplateId": Amplify.JSONValue.string("xxxxxxx")
                ]),
                "_version": Amplify.JSONValue.number(9.0),
                "createdAt": Amplify.JSONValue.string("2022-07-11T19:35:26.761Z"),
                "photoSequence": Amplify.JSONValue.array([Amplify.JSONValue.string("xxxxxxx"), Amplify.JSONValue.string("xxxxxxx")
                ]),
                "inspectionCheckInContactId": Amplify.JSONValue.string("xxxxxxx"),
                "milestoneTrackers": Amplify.JSONValue.array([Amplify.JSONValue.object([
                        "__typename": Amplify.JSONValue.string("MilestoneTracker"),
                        "milestoneId": Amplify.JSONValue.string("xxxxxxx"),
                        "all": Amplify.JSONValue.boolean(true),
                        "units": Amplify.JSONValue.array([])
                    ]), Amplify.JSONValue.object([
                        "__typename": Amplify.JSONValue.string("MilestoneTracker"),
                        "milestoneId": Amplify.JSONValue.string("xxxxxxx"),
                        "all": Amplify.JSONValue.null,
                        "units": Amplify.JSONValue.array([Amplify.JSONValue.string("xxxxxxx")
                        ])
                    ])
                ]),
                "notes": Amplify.JSONValue.string("xxxxxxx "),
                "code": Amplify.JSONValue.string("xxxxxxx"),
                "phaseId": Amplify.JSONValue.string("xxxxxxx"),
                "groupId": Amplify.JSONValue.string("xxxxxxx"),
                "owner": Amplify.JSONValue.string("xxxxxxx")
            ]),

@lawmicha lawmicha self-assigned this Aug 15, 2023
@lawmicha
Copy link
Contributor

That GraphQL response pertains to syncInspections, do you see any sync calls for syncProjects and logs around these?

Another question about your setup, do you have the feature flag generateModelsForLazyLoadAndCustomSelectionSet enabled? There are a lot of bug fixes in the code paths under this feature flag toggle that pertains to data loading, especially for a complex schema like yours, lazy loading enabled will enhance the datastore's sync process (an internal performance improvement).

https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/

@lawmicha
Copy link
Contributor

lawmicha commented Aug 29, 2023

To reduce the complexity of the schema and focus on verifying the key features are working, i'm using:

type Inspection @model(subscriptions: {level: public}) @auth(rules: [
  {allow: owner, operations: [create, update, delete]}, 
  {allow: groups, groups: ["ADMIN", "REVIEWER"], operations: [update, delete, read]}, 
  {allow: groups, groupsField: "groupId", operations: [update, delete, read]}, 
  {allow: public, operations: [update, read]}
  ]) 
  @searchable {
 id: ID! @primaryKey
 number: String
 projectId: ID! @index(name: "byProject", queryField: "inspectionsByProjectId")
 project: Project @belongsTo(fields: ["projectId"])
}

type Project @model @auth(rules: [
  {allow: owner, operations: [create]}, 
  {allow: private, operations: [update, delete, read]}, 
  {allow: public, operations: [read]}
  ]) 
  @searchable {
  id: ID! @primaryKey
  groupId: String
  name: String!
  status: String!
  clientId: ID! @index(name: "byClient", queryField: "projectsByClientId")
  reportTemplateId: String
  reportDisclaimer: String
  inspections: [Inspection] @hasMany(indexName: "byProject", fields: ["id"])
  milestoneTemplateId: ID
  externalId: String
}

This appears to sync fine. I'm using v2.16.0 and with the feature flag generatemodelsforlazyloadandcustomselectionset in the CLI.json enabled .

@lawmicha
Copy link
Contributor

Looking back at your original error, when syncing Inspection

keyNotFound(CodingKeys(stringValue: "project", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "items", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "phase", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: \"project\", intValue: nil) (\"project\").", underlyingError: nil))

  1. Do you have the new model type generation enabled? In cli.json file in your amplify project, the feature flag is
{ 
  "codegen": {
    "generatemodelsforlazyloadandcustomselectionset": false
   }
}

If not, you can enable this to generate the latest model types (for more information on the changes: https://docs.amplify.aws/cli/migration/lazy-load-custom-selection-set/

  1. Can you provide us what the Swift model type file looks like for Inspection and the Inspection+Schema file?

@cappy123abc
Copy link
Author

I will regenerate the model files with the feature flag and let you know how it goes. Sorry for the slow response, I was out of office.

@ruisebas ruisebas added the pending-community-response Issue is pending response from the issue requestor label Oct 4, 2023
@atierian
Copy link
Member

atierian commented Dec 8, 2023

We're considering this issue resolved. Please open a new issue as needed.

@atierian atierian closed this as completed Dec 8, 2023
@github-actions github-actions bot removed the pending-community-response Issue is pending response from the issue requestor label Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datastore Issues related to the DataStore category
Projects
None yet
Development

No branches or pull requests

4 participants