You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/postgraphile/make-add-pg-table-order-by-plugin.md
+29-10Lines changed: 29 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -146,7 +146,10 @@ this function creates.
146
146
`columnOrSqlFragment` is where the order value is specified, it becomes the
147
147
first entry in the `OrderSpec` tuple defined above.
148
148
149
-
`uniqueOrOptions` define 1) whether the sort order is unique, and 2) how to sort null values when sorting by ascending and descending order. Only set `uniqueOrOptions` (or `unique`) to `true` if you can guarantee that the sort order is unique.
149
+
`uniqueOrOptions` define 1) whether the sort order is unique, and 2) how to sort
150
+
null values when sorting by ascending and descending order. Only set
151
+
`uniqueOrOptions` (or `unique`) to `true` if you can guarantee that the sort
152
+
order is unique.
150
153
151
154
As of v4.12, you can also customize how nulls are sorted:
The `nulls` option extends the `ORDER BY` clause of the SQL query with either `NULLS FIRST` or `NULLS LAST` according to the following rules:
169
+
170
+
The `nulls` option extends the `ORDER BY` clause of the SQL query with either
171
+
`NULLS FIRST` or `NULLS LAST` according to the following rules:
172
+
167
173
- "first": specify `NULLS FIRST` for both ascending and descending;
168
174
- "last": specify `NULLS LAST` for both ascending and descending;
169
-
- "first-iff-ascending": specify `NULLS FIRST` when ordering by ascending, and `NULLS LAST` when ordering by descending;
170
-
- "last-iff-ascending": specify `NULLS LAST` when ordering by ascending, and `NULLS FIRST` when ordering by descending;
171
-
- (default) undefined: omit both `NULLS FIRST` and `NULLS LAST` in the order by clause for both ascending and descending, thus using the default ordering behavior.
175
+
- "first-iff-ascending": specify `NULLS FIRST` when ordering by ascending, and
176
+
`NULLS LAST` when ordering by descending;
177
+
- "last-iff-ascending": specify `NULLS LAST` when ordering by ascending, and
178
+
`NULLS FIRST` when ordering by descending;
179
+
- (default) undefined: omit both `NULLS FIRST` and `NULLS LAST` in the order by
180
+
clause for both ascending and descending, thus using the default ordering
181
+
behavior.
182
+
183
+
For example, you may wish to create a plugin to sort movies by either top-rated
184
+
or lowest-rated first (meaning the average of the movie's reviews):
172
185
173
-
For example, you may wish to create a plugin to sort movies by either top-rated or lowest-rated first (meaning the average of the movie's reviews):
To get the top-rated movies, one would then use the `RATING_DESC` option in the GraphQL query. However, by default, `RATING_DESC` would put movies with no reviews (and thus an average of `null`) first, followed by the sorted movies. A movie with no ratings is not exactly what one thinks of when one hears "top-rated"! By specifying `{ nulls: 'last' }`, however, PostGraphile knows that this orderBy plugin should still show the movies without any reviews, but just put them at the end of the list.
203
+
204
+
To get the top-rated movies, one would then use the `RATING_DESC` option in the
205
+
GraphQL query. However, by default, `RATING_DESC` would put movies with no
206
+
reviews (and thus an average of `null`) first, followed by the sorted movies. A
207
+
movie with no ratings is not exactly what one thinks of when one hears
208
+
"top-rated"! By specifying `{ nulls: 'last' }`, however, PostGraphile knows that
209
+
this orderBy plugin should still show the movies without any reviews, but just
0 commit comments