I'm writing a simple application using a repository pattern, nodejs, graphql and sequelize. First here is my base repository:
import db from '../models';
export default class AbstractRepository {
constructor (model, include) {
if (new.target === AbstractRepository) {
throw new TypeError('Cannot construct AbstractRepository instances directly.');
}
this.model = model;
this.include = include;
}
static findAll () {
return db[this.model].findAll({ include: this.include });
}
}
Here is a repository that extends it:
import db from '../models';
import AbstractRepository from './AbstractRepository';
class UserRepository extends AbstractRepository {
constructor () {
super('User', [db.Clickwrap]);
}
}
export default new UserRepository();
Lastly, here is where the UserRespository is used (there's more to this file, but this is the important part):
import UserRepository from '../../repositories/UserRepository';
const resolvers = {
Query: {
users (root, args, context) {
return UserRepository.findAll();
},
},
};
When I run a simple graphql query in graphiql like this:
{
users {
id
}
}
I get this error:
_UserRepository2.default.findAll is not a function
Not sure what I'm missing that's causing this.
Edit
I've updated my code so it's not exporting an instance anymore. Everything else has remained the same. Here's my new UserRepository file:
import db from '../models';
import AbstractRepository from './AbstractRepository';
class UserRepository extends AbstractRepository {
constructor () {
super('User', [db.Clickwrap]);
}
}
export default UserRepository;
Now I'm getting this error:
Cannot read property 'findAll' of undefined