Skip to content

Commit 794bf04

Browse files
committed
Improve queries
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
1 parent 97fbb54 commit 794bf04

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

graphql/queries/author/authors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ export default {
2323
const limit = args.first || 10;
2424
delete args.offset;
2525
delete args.first;
26-
return models.author.findAll({where: args, offset, limit});
26+
return models.author.findAll({where: args, include: [ { model: models.quote } ], offset, limit});
2727
}
2828
};

graphql/types/author.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export default new GraphQLObjectType({
3838
type: new GraphQLList(Quote),
3939
description: "author's quotes",
4040
resolve(author) {
41+
if (author.hasOwnProperty('quotes')) {
42+
return author.quotes;
43+
}
4144
return models.quote.findAll({ where: { author_id: author.id } });
4245
}
4346
}

graphql/types/quote.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export default new GraphQLObjectType({
2323
type: Author,
2424
description: "author of this quote",
2525
resolve (quote) {
26+
if (quote.hasOwnProperty('author')) {
27+
return quote.author;
28+
}
2629
return models.author.findById(quote.author_id);
2730
}
2831
},

0 commit comments

Comments
 (0)