2

Having an optional field on a content type in Contentful, I tried to get Gatsby-Plugin-Contentful-Optional-Fields working.

The optional field is an image, that I render with the new Gatsby-Image-Plugin. After configuring the plugin in my gatsby-config.js, I still get the following error:

There was an error in your GraphQL query:
Cannot query field "description" on type "Node".

Spent hours to solve it, with no success unfortunately. Im fairly new to developing, so for someone else, maybe just an obvious mistake. Any help is highly appreciated.

The plugin configuration in my gatsby-config.js:

{
      resolve: "gatsby-plugin-contentful-optional-fields",
      options: {
        optionalFields: {
          ContentfulNews: {
            image: "Node",
          },
        },
      },
    }, 

My Query:

query {
    allContentfulNews(sort: { fields: date, order: DESC }) {
      edges {
        node {
          id
          heading
          date(formatString: "DD.MM.YYYY")
          text {
            childMarkdownRemark {
              html
            }
          }
          image {
            gatsbyImageData(
              layout: FULL_WIDTH
              placeholder: BLURRED
              formats: [AUTO, WEBP]
              quality: 100
              width: 1500
            )
            description
          }
        }
      }
    }
  }
Peter
  • 21
  • 3
  • is `description` a field of `image`? Have you tried it in the `localhost:8000/__graphql` playground? – Ferran Buireu Mar 14 '21 at 11:17
  • Thank you for the answer, @FerranBuireu. I typed in the description directly in the "Asset" in Contentful. When using the GraphQL playground, it shows the description. Also it happened, that the error was: `There was an error in your GraphQL query: Cannot query field "gatsbyImageData" on type "Node".` – Peter Mar 15 '21 at 10:55
  • I don't see the point of using the plugin of optional fields (which seems that is causing the issue). Have you tried removing it and assuming that is a mandatory field (just to test)? If the field is optional in the CMS, you can always avoid the printing of the asset in the JSX – Ferran Buireu Mar 15 '21 at 11:10
  • 1
    Yes, so basically I removed the image field for now, so my code doesn't throw any errors. Just thought the problem is an obvious mistake by me. But seems to bit more tricky, so not worth it to lose more time on it. Thanks a lot for your support anyways. – Peter Mar 15 '21 at 11:26

0 Answers0