aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Missing nested data in hasOne relationship #6973

Open edomazi opened 4 years ago

edomazi commented 4 years ago

Hello, I have a small problem with a hasOne relationship between those 2 models:

type Client @model @auth(rules: [{ allow: owner }]) {
    id: ID!
    name: String!
    email: String!
}

type Order @model @auth(rules: [{ allow: owner }]) {
        id: ID!
        payment_type: String!
        clientID: ID!
        client: Client @connection(fields: ["clientID"])
}

When I query the database from appsync I get the following nested data which is correct:

    query MyQuery {
        syncOrders {
            items {
                id
                client {
                    id
                    name
                    email
                }
            }
        }
    }

    "data": {
        "syncOrders": {
            "items": [{
                "id": "5252c836-d5fb-4709-aadc-dd4227e274ae",
                "client": {
                    "id": "47097933-b6dc-4bad-9d8c-7dd8da773d30",
                    "name": "Testing Name",
                    "email": "test@test.com"
                }
            }]
        }
    }

But when I query the database from my app ( this.orders = await DataStore.query(Orders); ) I get those results which don't have the nested client name and email:

    syncOrders: {
        items: [
            0: {
                id: "5252c836-d5fb-4709-aadc-dd4227e274ae"
                createdAt: "2020-10-01T11:43:39Z"
                payment_type: ""
                clientID: "47097933-b6dc-4bad-9d8c-7dd8da773d30"
                _version: 1
                _lastChangedAt: 1601552619740
                _deleted: null
                nextToken: null
                startedAt: 1602700534251
            }
        ]
    }

What am I am missing? Why i don't get the name and email from the client? Also this is what grapql generated with amplify codegen:

export const syncOrders = /* GraphQL */ `
  query SyncOrders(
    $filter: ModelOrderFilterInput
    $limit: Int
    $nextToken: String
    $lastSync: AWSTimestamp
  ) {
    syncOrders(
      filter: $filter
      limit: $limit
      nextToken: $nextToken
      lastSync: $lastSync
    ) {
      items {
        id
        payment_type
        clientID
        client {
          id
          name
          email
        }
        _version
        _deleted
        _lastChangedAt
        updatedAt

amplify versions:

"@aws-amplify/core": "^3.5.3",
"@aws-amplify/datastore": "2.2.8",
"@aws-amplify/ui-vue": "^0.2.14",
"aws-amplify": "^3.0.24",

What should I do to get all the data I need? Thanks

amhinson commented 4 years ago

@edomazi DataStore doesn't currently support syncing any connections to a model. In your case, you can query Order and Client separately then combine them in your app for the time being. With that said, we are working on making this simpler. I'm going to mark this as a feature request for now, and we will come back with any related future updates.

TheMoums commented 3 years ago

Any updates on this?

The connections are solved automatically by DataStore when dealing with BELONGS_TO relations but still not working with HAS_ONE relations and this issue is almost one year old....

jrobbins-LiveData commented 2 years ago

Re https://github.com/aws-amplify/amplify-js/issues/6973#issuecomment-712272925: could we please have an update?

acusti commented 2 years ago

i ran into this issue and would love to get myself unblocked. in my case, i have a model, RentalItem, which is restricted to CRUD from the model owner only. i have another model, Rental, which is also restricted to CRUD from the model owner only. this way, i create a Rental item for a logged in user on a particular RentalItem based on the id of a third model, a MediaItem (RentalItem has a mediaItemID @hasOne field). the schema for the Rental model includes:

    rentalItemID: ID!
    rentalItem: RentalItem! @hasOne(fields: ["rentalItemID"])

in this way, whoever creates the Rental gets access to the RentalItem via the hasOne relationship. however, i cannot cross-reference the RentalItem with the rentalItemID i receive from DataStore because only the owner of the RentalItem has access to it.

looking at this thread, i see that using @belongsTo will make it so that DataStore includes the referenced model. is replacing my @hasOne with a @belongsTo a viable workaround? the Rental to RentalItem relationship is many-to-one, where “many” could be 100,000+. thanks for any advice any of you can provide!