Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL Error When Sorting on Node Field #5902

Closed
Undistraction opened this issue Jun 14, 2018 · 4 comments
Closed

GraphQL Error When Sorting on Node Field #5902

Undistraction opened this issue Jun 14, 2018 · 4 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@Undistraction
Copy link
Contributor

Undistraction commented Jun 14, 2018

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

@pieh pieh added the type: question or discussion Issue discussing or asking a question about Gatsby label Jun 14, 2018
@pieh
Copy link
Contributor

pieh commented Jun 14, 2018

Can you try if this would work?

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

@Undistraction
Copy link
Contributor Author

Undistraction commented Jun 15, 2018

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

@pieh
Copy link
Contributor

pieh commented Jul 6, 2018

Yeah, it's invalid but it's understandable (double use of fields word) - fields input param is to specify path to field in note that is used for sorting and isn't actually tied to node.field - so if path is node.field.something, we need to use field___something. If path would node.date, then we would use just date.

@pieh pieh closed this as completed Jul 6, 2018
@Undistraction
Copy link
Contributor Author

@pieh That makes sense. Thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants