I'm working on a project with GraphQl but have run into a problem with querying through GraphQl and sequelize. When trying to query associated objects i receive the following error:
Cannot return null for non-nullable field User.organisation.
But when I only query the User objects without the associations I get no error and everything works fine. Here is my code:
Query Code:
return await models.user.findAll({ include: [{ model: models.organisation, as: 'Creator'}]});
GraphQl query that works:
query{users{username}}
Graphql query that doesn't work:
query{users{username organisation{id}}}
When troubleshooting the error i found that when console logging the response from the database it shows like this: Where the Array of organizations simply are shown as an Array and not as the actual objects I'm looking for which is something i can't explain...
dataValues:
{ id: 1,
username: 'asdfasd',
password: '$2b$10$ynFfJ8NFrSC7ycA0Xskjqu5uq.wegBuZkphkfMiG99F.vqN7Wqw9C',
email: 'asdf@gmo.com',
active: true,
createdAt: 2018-11-25T16:42:32.140Z,
updatedAt: 2018-11-25T16:42:32.140Z,
Creator: [Array] },
Sequelize model Association definition:
user.associate = function(models) {
models.user.hasMany(models.organisation, {
foreignKey: 'OrgAdmin',
as: 'Creator'
});
Any help would be much appreciated!