From 794bf043db61acac0bc5d77e0374f7174665c6bc Mon Sep 17 00:00:00 2001 From: Matej Jellus Date: Fri, 27 Jul 2018 00:26:53 +0200 Subject: [PATCH] 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 --- graphql/queries/author/authors.js | 2 +- graphql/types/author.js | 3 +++ graphql/types/quote.js | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/graphql/queries/author/authors.js b/graphql/queries/author/authors.js index d06c24f..b5961bf 100644 --- a/graphql/queries/author/authors.js +++ b/graphql/queries/author/authors.js @@ -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}); } }; diff --git a/graphql/types/author.js b/graphql/types/author.js index 4379c29..46d9545 100644 --- a/graphql/types/author.js +++ b/graphql/types/author.js @@ -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 } }); } } diff --git a/graphql/types/quote.js b/graphql/types/quote.js index dff29b6..a0a3101 100644 --- a/graphql/types/quote.js +++ b/graphql/types/quote.js @@ -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); } },