vuex-orm / plugin-graphql

Vuex ORM persistence plugin to sync the store against a GraphQL API.
https://vuex-orm.github.io/plugin-graphql/
MIT License
227 stars 52 forks source link

morphToMany not return related models #159

Closed t2thec closed 3 years ago

t2thec commented 4 years ago

Hey,

Firstly - Amazing plugin! Thanks for the hard work.

I have a morphToMany relationship set up. When querying the data, I am not receiving the related models - even through they have been retrieved from GQL and are stored in Vuex.

"vue": "^3.0.0-rc.5",
"vuex": "^4.0.0-beta.4"
"@vuex-orm/core": "^0.36.3",
"@vuex-orm/plugin-graphql": "^1.0.0-rc.41"

My query CurrentUser.query().with('roles').first()

If I log CurrentUser polymorphic manyToMany the relationship roles is an empty array.

However, if I log Role.query().get() and ModelHasRole.query().get(), the correct data has been retrieved and stored correctly from the server (The GQL query and response also proves to be correct).

I presume I am missing something here, but can't for the life of me see what. Normal belongsTo relationships (etc) seem to work perfectly. It is just the polymorphic relationships that don't seem to be working here.

Can you see what's up with this? (Sorry for the long post ;-))

ModelHasRole response when logged

$id: "1_1_currentUsers"
$isPersisted: false
modelId: 1
modelType: "currentUsers"
roleId: 1

CurrentUser model

export default class CurrentUser extends Model {
  static entity = 'currentUsers'
  static eagerLoad = ['roles']

  static fields() {
    return {
      id: this.number(null),
      firstName: this.string(''),
      lastName: this.string(''),
      //......//
      roles: this.morphToMany(
        Role,
        ModelHasRole,
        'roleId',
        'modelId',
        'modelType'
      ),
    }
  }
}

ModelHasRole model

export default class ModelHasRole extends Model {
  static entity = 'modelHasRoles'

  static fields() {
    return {
      roleId: this.number(null),
      modelId: this.number(null),
      modelType: this.string(''),
    }
  }
}

Role model

export default class Role extends Model {
  static entity = 'roles'

  static fields() {
    return {
      id: this.attr(null),
      name: this.string(''),
      //.....//
    }
  }
}
t2thec commented 4 years ago

Anyone able to help at all? Very much appreciated!!

t2thec commented 3 years ago

I’ve not found a solution to this issue and have decided to remove vuex orm from the out projects. Closing this issue for now.