Contentful logo

Contentful Community

GraphQL for new content type in Gatsby?

I’ve created a new content type, id “jobPost”. I want to display content based on this in the Gatsby site but can’t get the graphQL to work for me. It’s not clear if what I’m trying is the way it’s supposed to be done. My content type structure is very close to the blog post type so I just replicated part of the qraphQL query and amended for my content type:

allContentfulJobPost(sort: { fields: [publishDate], order: DESC }) {
      edges {
        node {
          title
          slug
          publishDate(formatString: "MMMM Do, YYYY")
          tags
          description {
            childMarkdownRemark {
              html
            }
          }
        }
      }
    }

However, when I do this I get an error and the site doesn’t load:

TypeError: Cannot read property ‘0’ of undefined

RootIndex.render
src/pages/index.js:9

6 | import ArticlePreview from '../components/article-preview'
7 | 
8 | class RootIndex extends React.Component {
>  9 |   render() {  
10 |     const siteTitle = get(this, 'props.data.site.siteMetadata.title')  
11 |     const posts = get(this, 'props.data.allContentfulBlogPost.edges')  
12 |     const [author] = get(this, 'props.data.allContentfulPerson.edges')

Should my approach work or am I missing something? Unfortunately the onboarding content talks about using the JS SDK and not graphQL so it’s not clear whether I’m doing the right thing.

Update, having accidentally found that there are better error messages in terminal:

I can see the following detailed error info:

GraphQL Error Argument "sort" has invalid value {fields: [publishDate], order: [desc]}.
In field "fields": In element #0: Expected type "ContentfulJobPostConnectionSortByFieldsEnum", found publishDate.
In field "order": Expected type "contentfulJobPostConnectionSortOrderValues", found [desc].

      file: /Users/jeremy/Documents/Development/starter-gatsby-blog/development/starter-gatsby-blog/src/pages/index.js

   1 | 
   2 |   query HomeQuery {
>  3 |     allContentfulJobPost(sort: { fields: [publishDate], order: [desc] }) {

Do I need to create something for ContentfulJobPostConnectionSortByFieldsEnum? Or can I approach my query in a different way?