Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify query parsing in mongoose adapter #2240

Merged
merged 2 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore-start -->

<!-- markdownlint-disable -->

<table>
<tr>
<td align="center"><a href="http://www.thinkmill.com.au"><img src="https://avatars3.githubusercontent.com/u/872310?v=4" width="80px;" alt=""/><br /><sub><b>Jed Watson</b></sub></a><br /><a href="https://github.com/keystonejs/keystone/commits?author=JedWatson" title="Code">💻</a></td>
Expand Down Expand Up @@ -332,7 +335,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</table>

<!-- markdownlint-enable -->

<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ We'd like to start by thanking all our wonderful contributors:
([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore-start -->

<!-- markdownlint-disable -->

<table>
<tr>
<td align="center"><a href="http://www.thinkmill.com.au"><img src="https://avatars3.githubusercontent.com/u/872310?v=4" width="80px;" alt=""/><br /><sub><b>Jed Watson</b></sub></a><br /><a href="https://github.com/keystonejs/keystone/commits?author=JedWatson" title="Code">💻</a></td>
Expand Down Expand Up @@ -123,7 +126,9 @@ We'd like to start by thanking all our wonderful contributors:
</table>

<!-- markdownlint-enable -->

<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

### Demo Projects
Expand Down
36 changes: 14 additions & 22 deletions packages/adapter-mongoose/lib/adapter-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,35 +216,27 @@ 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);
});
}

// 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}`),
});
let query;
try {
query = graphQlQueryToMongoJoinQuery(args);
} catch (error) {
return Promise.reject(error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Why was this catch here? Looking over the original code, I can't see a reason why it might have thrown. But now I'm anxious that we're going to have an edge case we'll no longer be handling safely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I looked really closely and I couldn't see any reason for having this error handling pattern here. I can't see any good reason to try, catch, re-throw rather than just letting the original error propagate. Nothing about the parsing looked like it was worth having this there so I decided to clear it out 🤷‍♂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference here is that we converted the synchronous Error into an asynchronous rejected Promise, so letting it propagate would change the behaviour of all the calling code when it errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, good point, and now I'm seeing a bunch of failures which are perhaps related? Why must programming be so hard!

}

if (meta) {
// Order is important here, which is why we do it last (v8 will append the
// key, and keep them stable)
Expand Down