Sorting based on field raises a _GraphQL Error_ error
Using http://localhost:8000/__graphql I can run the following query:
{
allMarkdownRemark {
edges {
node {
fields {
title
type
slug
date
author
category
}
}
}
}
}
Which shows me all my nodes with all fields populated. However if I try and sort based on any of these fields, for example on date:
{
allMarkdownRemark(
sort: { fields: [date], order: DESC }
) {
edges {
node {
fields {
title
type
slug
date
author
category
}
}
}
}
}
I would expect the query to return the results sorted by date.
GraphQL Error Argument "sort" has invalid value {fields: [date], order: DESC}.
In field "fields": In element #0: Expected type "MarkdownRemarkConnectionSortByFieldsEnum", found date.
My understanding is that these fields should be added dynamically and that I should be able to sort on them. Trying to filter on any field results in the same error. I am only able to sort on markdown fields. These issues suggest this should be possible #2886 #2024.
gatsby-config.js: N/A
package.json: N/A
gatsby-node.js: N/A
gatsby-browser.js: N/A
gatsby-ssr.js: N/A
Can you try if this would work?
-sort: { fields: [date], order: DESC }
+sort: { fields: [fields___date], order: DESC }
@pieh Thanks. Yes that does work. Is my original approach invalid then? Is this the correct way to query for sub fields?
For anyone stumbling on this, you can go deeper than one level with this approach, for example to sort on node.fields.metadata.keywords:
sort: { fields: [fields___metadata___keywords], order: DESC }
Most helpful comment
Can you try if this would work?