Skip to content

Commit

Permalink
Improve queries
Browse files Browse the repository at this point in the history
If the user requested authors with their quotes, it did one query
to get the authors and then one query per each author to get his
quotes. Now it loads everything in the first query, then just
checks if it is already loaded and that is all.

Resolves #13
  • Loading branch information
juffalow committed Jul 26, 2018
1 parent 97fbb54 commit 794bf04
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphql/queries/author/authors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export default {
const limit = args.first || 10;
delete args.offset;
delete args.first;
return models.author.findAll({where: args, offset, limit});
return models.author.findAll({where: args, include: [ { model: models.quote } ], offset, limit});
}
};
3 changes: 3 additions & 0 deletions graphql/types/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default new GraphQLObjectType({
type: new GraphQLList(Quote),
description: "author's quotes",
resolve(author) {
if (author.hasOwnProperty('quotes')) {
return author.quotes;
}
return models.quote.findAll({ where: { author_id: author.id } });
}
}
Expand Down
3 changes: 3 additions & 0 deletions graphql/types/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default new GraphQLObjectType({
type: Author,
description: "author of this quote",
resolve (quote) {
if (quote.hasOwnProperty('author')) {
return quote.author;
}
return models.author.findById(quote.author_id);
}
},
Expand Down

0 comments on commit 794bf04

Please sign in to comment.