@@ -2,6 +2,7 @@ import { GraphQLID, GraphQLString } from 'graphql';
22import memorize from 'memorize-decorator' ;
33import {
44 ACCESS_GROUP_FIELD ,
5+ ENTITY_CREATED_AT ,
56 FLEX_SEARCH_FULLTEXT_INDEXED_DIRECTIVE ,
67 FLEX_SEARCH_INDEXED_DIRECTIVE ,
78 FLEX_SEARCH_ORDER_ARGUMENT ,
@@ -349,21 +350,29 @@ export class RootEntityType extends ObjectTypeBase {
349350 // primary sort is only used for sorting, so make sure it's unique
350351 // - this makes querying more consistent
351352 // - this enables us to use primary sort for cursor-based pagination (which requires an absolute sort order)
353+ // the default primary sort should be createdAt_DESC, because this is useful most of the time. to avoid
354+ // surprises when you do specify a primary sort, always add this default at the end (as long as it's not already
355+ // included in the index)
356+ if ( ! clauses . some ( clause => clause . field === ENTITY_CREATED_AT ) ) {
357+ clauses = [
358+ ...clauses ,
359+ {
360+ field : ENTITY_CREATED_AT ,
361+ direction : OrderDirection . DESCENDING
362+ }
363+ ] ;
364+ }
352365 if ( ! clauses . some ( clause => clause . field === this . discriminatorField . name ) ) {
353366 clauses = [
354367 ...clauses ,
355368 {
356369 field : this . discriminatorField . name ,
357- asc : true
370+ direction : OrderDirection . DESCENDING
358371 }
359372 ] ;
360373 }
361374 return clauses . map (
362- c =>
363- new FlexSearchPrimarySortClause (
364- new FieldPath ( { path : c . field , baseType : this } ) ,
365- c . asc ? OrderDirection . ASCENDING : OrderDirection . DESCENDING
366- )
375+ c => new FlexSearchPrimarySortClause ( new FieldPath ( { path : c . field , baseType : this } ) , c . direction )
367376 ) ;
368377 }
369378
0 commit comments