Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Handle nested sorting for fields with underscores
Browse files Browse the repository at this point in the history
See #373
  • Loading branch information
johnymontana committed Jan 8, 2020
1 parent 979d712 commit b422f3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ const temporalOrderingFieldExists = (schemaType, filterParams) => {
orderByParam = orderByParam.value;
if (!Array.isArray(orderByParam)) orderByParam = [orderByParam];
return orderByParam.find(e => {
const fieldName = e.substring(0, e.indexOf('_'));
const fieldName = e.substring(0, e.lastIndexOf('_'));
const fieldTypeName = getFieldTypeName(schemaType, fieldName);
return isTemporalType(fieldTypeName);
});
Expand All @@ -1727,7 +1727,7 @@ const buildSortMultiArgs = param => {
if (!Array.isArray(values)) values = [values];
return values
.map(e => {
fieldName = e.substring(0, e.indexOf('_'));
fieldName = e.substring(0, e.lastIndexOf('_'));
return e.includes('_asc') ? `'^${fieldName}'` : `'${fieldName}'`;
})
.join(',');
Expand Down
30 changes: 30 additions & 0 deletions test/unit/cypherTest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5118,3 +5118,33 @@ test('Handle @cypher field with parameterized value for field of input type argu
augmentedSchemaCypherTestRunner(t, graphQLQuery, {}, expectedCypherQuery)
]);
});

test('Handle order by field with underscores - root level', t => {
const graphQLQuery = `{Movie(orderBy: someprefix_title_with_underscores_desc){title}}`,
expectedCypherQuery =
'MATCH (`movie`:`Movie`:`u_user-id`:`newMovieLabel`) WITH `movie` ORDER BY movie.someprefix_title_with_underscores DESC RETURN `movie` { .title } AS `movie`';

t.plan(1);
return Promise.all([
augmentedSchemaCypherTestRunner(t, graphQLQuery, {}, expectedCypherQuery)
]);
});

test('Handle order by field with underscores - nested field ', t => {
const graphQLQuery = `
{
GenresBySubstring(substring: "Foo") {
movies(orderBy: someprefix_title_with_underscores_desc) {
title
}
}
}
`,
expectedCypherQuery =
'WITH apoc.cypher.runFirstColumn("MATCH (g:Genre) WHERE toLower(g.name) CONTAINS toLower($substring) RETURN g", {offset:$offset, first:$first, substring:$substring, cypherParams: $cypherParams}, True) AS x UNWIND x AS `genre` RETURN `genre` {movies: apoc.coll.sortMulti([(`genre`)<-[:`IN_GENRE`]-(`genre_movies`:`Movie`:`u_user-id`:`newMovieLabel`) | genre_movies { .title }], [\'someprefix_title_with_underscores\']) } AS `genre`';

t.plan(1);
return Promise.all([
augmentedSchemaCypherTestRunner(t, graphQLQuery, {}, expectedCypherQuery)
]);
});

0 comments on commit b422f3b

Please sign in to comment.