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

belongsToMany associations have undefined output type #92

Open
tapvt opened this issue Jan 25, 2024 · 10 comments
Open

belongsToMany associations have undefined output type #92

tapvt opened this issue Jan 25, 2024 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@tapvt
Copy link

tapvt commented Jan 25, 2024

I've run into an issue regarding belongsToMany relationships. It seems that a type is not generated when there is a join table. This is causing graphcraft to not function using graphql and graphql-http (specifically the express library).

For example:

This is the error: The type of Workorder.WorkorderParts must be Output Type but got: undefined.

We have a Workorder model, a Parts model and a join table, WorkorderParts.

Can anyone provide any guidance as to why this is happening? Is this a known issue or am I just doing something incorrectly?

Thanks in advance.

@alirizwan
Copy link
Collaborator

alirizwan commented Jan 25, 2024 via email

@tapvt
Copy link
Author

tapvt commented Jan 25, 2024

Wow. Thanks for the quick reply!

I'm using v1.2.1

Here are the relevant portions of the least complex models. Here, we're looking at the belongsToMany relationship between Vendors and Contacts, which uses a { through: 'VendorContacts" }. This is for the sake of an example.

// models/vendor.js - Vendor model
module.exports = (sequelize, DataTypes) => {
  const Vendor = sequelize.define('Vendor', {
    name: DataTypes.STRING,
    accountNumber: DataTypes.STRING,
    website: DataTypes.STRING,
    address: DataTypes.STRING,
    addressTwo: DataTypes.STRING,
    city: DataTypes.STRING,
    state: DataTypes.STRING,
    postalCode: DataTypes.STRING,
    shipOnly: DataTypes.BOOLEAN
  }, {})
  Vendor.associate = function ({VendorLocation, Contact, Part}) {
    //Vendor.hasMany(VendorLocation)
    Vendor.belongsToMany(Contact, { through: 'VendorContacts'})
    //Vendor.belongsToMany(Part, { through: 'VendorParts', constraints: false})
  }
  return Vendor
}

// models/contact.js - Contact model
module.exports = (sequelize, DataTypes) => {
  const Contact = sequelize.define('Contact', {
    name: DataTypes.STRING,
    email: DataTypes.STRING,
    position: DataTypes.STRING
  }, {})

  Contact.associate = function ({Vendor, VendorLocation}) {
    Contact.belongsToMany(Vendor, { through: 'VendorContacts'})
    Contact.belongsToMany(VendorLocation, { through: 'VendorLocationContacts'})
  }

  return Contact
}

The result is:

The type of Vendor.VendorContacts must be Output Type but got: undefined.
The type of Contact.VendorContacts must be Output Type but got: undefined.

If I log the relation in generateAssociationFields(), I can pick out VendorContacts, which looks like this:

VendorContacts: {
  name: 'VendorContacts',
  description: undefined,
  type: undefined,
  args: [],
  resolve: undefined,
  subscribe: undefined,
  deprecationReason: undefined,
  extensions: [Object: null prototype] {},
  astNode: undefined
}

@alirizwan
Copy link
Collaborator

Thank you, I will replicate this issue and get back with a solution.

@tapvt
Copy link
Author

tapvt commented Jan 25, 2024

Thank you so much, @alirizwan!

@tapvt
Copy link
Author

tapvt commented Feb 1, 2024

Hi @alirizwan. I know your time is valuable, and that this is an open-source project. If you have time, I wonder if you might point me in the right direction(s) so I can troubleshoot this myself? Thanks..

@alirizwan
Copy link
Collaborator

alirizwan commented Feb 1, 2024 via email

@alirizwan
Copy link
Collaborator

You can fix it by creating your through models to be sequelize models instead of String, in the meantime I will fix the bug.

@tapvt
Copy link
Author

tapvt commented Feb 4, 2024 via email

@alirizwan
Copy link
Collaborator

For example your VendorContacts could be

module.exports = (sequelize, DataTypes) => {

  const VendorContacts = sequelize.define('VendorContacts ', {});

  return VendorContacts ;

};

Just an empty sequelize model and then in through you can do models.VendorContacts or just refer to that model however you want to.

@alirizwan
Copy link
Collaborator

@tapvt I have found the issue, it was a tricky one. It will be fixed in next version.

@alirizwan alirizwan self-assigned this Feb 13, 2024
@alirizwan alirizwan added the bug Something isn't working label Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants