Gatsby: GraphQL Error When Sorting on Node Field

Created on 14 Jun 2018  路  4Comments  路  Source: gatsbyjs/gatsby

Description

Sorting based on field raises a _GraphQL Error_ error

Steps to reproduce

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
        }
      }
    }
  }
}

Expected result

I would expect the query to return the results sorted by date.

Actual result

GraphQL Error Argument "sort" has invalid value {fields: [date], order: DESC}.
In field "fields": In element #0: Expected type "MarkdownRemarkConnectionSortByFieldsEnum", found date.

Notes

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.

Environment

File contents (if changed)

gatsby-config.js: N/A
package.json: N/A
gatsby-node.js: N/A
gatsby-browser.js: N/A
gatsby-ssr.js: N/A

question or discussion

Most helpful comment

Can you try if this would work?

-sort: { fields: [date], order: DESC }
+sort: { fields: [fields___date], order: DESC }

All 4 comments

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 }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

totsteps picture totsteps  路  3Comments

timbrandin picture timbrandin  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

mikestopcontinues picture mikestopcontinues  路  3Comments

theduke picture theduke  路  3Comments