From 6bebcaaa7ea846138597ac5e0a421bf3773f9337 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Wed, 15 Jan 2020 16:05:31 +1100 Subject: [PATCH 1/2] Simplify query parsing in mongoose adapter --- CONTRIBUTING.md | 5 +++ README.md | 5 +++ .../adapter-mongoose/lib/adapter-mongoose.js | 43 +++++++------------ 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5cc91e4a0a8..b30314b0762 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -275,8 +275,11 @@ We also build commonjs builds to run in node (for testing with jest or etc.) and Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): + + + @@ -332,7 +335,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

Jed Watson

💻
+ + This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! diff --git a/README.md b/README.md index cbec8357e61..6fe425ac48b 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,11 @@ We'd like to start by thanking all our wonderful contributors: ([emoji key](https://allcontributors.org/docs/en/emoji-key)): + + + @@ -123,7 +126,9 @@ We'd like to start by thanking all our wonderful contributors:

Jed Watson

💻
+ + ### Demo Projects diff --git a/packages/adapter-mongoose/lib/adapter-mongoose.js b/packages/adapter-mongoose/lib/adapter-mongoose.js index 2026639be36..a5cf4171312 100644 --- a/packages/adapter-mongoose/lib/adapter-mongoose.js +++ b/packages/adapter-mongoose/lib/adapter-mongoose.js @@ -216,35 +216,22 @@ class MongooseListAdapter extends BaseListAdapter { args = mergeWhereClause(args, { id: { $in: ids[0][from.fromField] || [] } }); } } - function graphQlQueryToMongoJoinQuery(query) { - const _query = { - ...query.where, - ...mapKeyNames( - // Grab all the modifiers - pick(query, ['search', 'orderBy', 'skip', 'first']), - // and prefix with a dollar symbol so they can be picked out by the - // query builder tokeniser - key => `$${key}` - ), - }; - - return mapKeys(_query, field => { - if (getType(field) !== 'Object' || !field.where) { - return field; - } - - // recurse on object (ie; relationship) types - return graphQlQueryToMongoJoinQuery(field); - }); - } - - let query; - try { - query = graphQlQueryToMongoJoinQuery(args); - } catch (error) { - return Promise.reject(error); - } + // Convert the args `where` clauses and modifiers into a data structure + // which can be consumed by the queryParser. Modifiers are prefixed with a + // $ symbol (e.g. skip => $skip) to be identified by the tokenizer. + // `where` keys are removed, and nested queries are handled recursively. + // { where: { a: 'A', b: { where: { c: 'C' } } }, skip: 10 } + // => { a: 'A', b: { c: 'C' }, $skip: 10 } + const graphQlQueryToMongoJoinQuery = ({ where, ...modifiers }) => ({ + ...mapKeys(where, whereElement => + getType(whereElement) === 'Object' && whereElement.where + ? graphQlQueryToMongoJoinQuery(whereElement) // Recursively traverse relationship fields + : whereElement + ), + ...mapKeyNames(pick(modifiers, ['search', 'orderBy', 'skip', 'first']), key => `$${key}`), + }); + const query = graphQlQueryToMongoJoinQuery(args); if (meta) { // Order is important here, which is why we do it last (v8 will append the // key, and keep them stable) From 0339de4c29ac776474f8c3ef6d445bfde7908b0b Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Wed, 15 Jan 2020 16:27:18 +1100 Subject: [PATCH 2/2] Fix test failures --- packages/adapter-mongoose/lib/adapter-mongoose.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/adapter-mongoose/lib/adapter-mongoose.js b/packages/adapter-mongoose/lib/adapter-mongoose.js index a5cf4171312..40ba2691f4f 100644 --- a/packages/adapter-mongoose/lib/adapter-mongoose.js +++ b/packages/adapter-mongoose/lib/adapter-mongoose.js @@ -224,14 +224,19 @@ class MongooseListAdapter extends BaseListAdapter { // { where: { a: 'A', b: { where: { c: 'C' } } }, skip: 10 } // => { a: 'A', b: { c: 'C' }, $skip: 10 } const graphQlQueryToMongoJoinQuery = ({ where, ...modifiers }) => ({ - ...mapKeys(where, whereElement => + ...mapKeys(where || {}, whereElement => getType(whereElement) === 'Object' && whereElement.where ? graphQlQueryToMongoJoinQuery(whereElement) // Recursively traverse relationship fields : whereElement ), ...mapKeyNames(pick(modifiers, ['search', 'orderBy', 'skip', 'first']), key => `$${key}`), }); - const query = graphQlQueryToMongoJoinQuery(args); + let query; + try { + query = graphQlQueryToMongoJoinQuery(args); + } catch (error) { + return Promise.reject(error); + } if (meta) { // Order is important here, which is why we do it last (v8 will append the // key, and keep them stable)