System Information
- Node.js version: 20.12.0
- NPM version: 10.5.0
- Strapi version: 5.4.0
- Database: sqlite
- Operating system: MacOS
- Is your project Javascript or Typescript: JS
Describe the bug
Nested collection in graphql ignores the sort
value. The correspondent REST requests is actually ordering the nested collection
Steps to reproduce the behavior
- Have a relationship one to many in a collection, eg, in my case:
schedulesByDay
has manyschedule
and have atime
field onschedule
- Query an object with the nested collection
- The nested collection is sorted
Won’t order the schedules
by time
Expected behavior
It orders schedules
by time
Screenshots
Code snippets
query SchedulesByDays($sort: [String], $schedulesSort2: [String]) {
schedulesByDays(sort: $sort) {
day
schedules(sort: $schedulesSort2) {
time
title
}
}
}
with:
{
"sort": [
"day:asc"
],
"schedulesSort2": [
"time:desc"
]
}
Additional context
The query is not sorting the nested collection by time
Been trying to implement it myself like this
const extensionService = strapi.service('plugin::graphql.extension');
extensionService.use(({ strapi }) => ({
typeDefs: `
type Query {
schedulesByDaysSorted(sort: [String], schedulesSort: [String], pagination: PaginationArg): [SchedulesByDay]
}
`,
resolvers: {
Query: {
schedulesByDaysSorted: async (parent, args, ctx) => {
const { sort, schedulesSort, pagination } = args;
console.log('*** __dirname: ', __dirname);
// Fetch days with sorting and nested schedules sorting
const days = await strapi
.documents('api::schedules-by-day.schedules-by-day')
.findMany({
sort,
pagination,
populate: {
schedules: {
sort: schedulesSort,
},
},
});
fs.writeFileSync(
`${__dirname}/debug/debug-output.json`,
JSON.stringify(days, null, 2),
'utf8'
);
return days;
},
},
},
resolversConfig: {
'Query.schedulesByDaysSorted': {
auth: false,
},
},
}));
and thing is that the output file is correctly printing the right order, but what goes back to the client is not.