0

Good Evening, I am new to Gatsby and Graphql. I am experimenting with the CMS system, but I have no idea what this error means. Any help would be appreciated. The marked below is the error that I have been getting. I do have the folder graphcms-fragmetns in my root folder. I have followed the documentation that's out there, but it hasn't worked.

Cannot query field "image" on type "Query" Graphiql also is only showing queries for my local machine. I am not sure what I am missing.

index.js

import * as React from "react"
import {useState} from "react";
import {useStaticQuery, graphql} from "gatsby"





const IndexPage = () => {
  const [query, setQuery] = useState([{}])

   const testQuery =  graphql`
  {
    image {
      images{
        id
      }
    }
  }
  
    `
   const test1 = useStaticQuery(testQuery);
   console.log(test1);

  return (
    <main style={pageStyles}>
      <title>Home Page</title>
      <h1 style={headingStyles}>
        Congratulations
        <br />
        <span style={headingAccentStyles}>— you just made a Gatsby site! </span>
        <span role="img" aria-label="Party popper emojis">
          
        </span>
      </h1>
      <p style={paragraphStyles}>
        Edit <code style={codeStyles}>src/pages/index.js</code> to see this page
        update in real-time.{" "}
        <span role="img" aria-label="Sunglasses smiley emoji">
          
        </span>
      </p>
      <ul style={listStyles}>
        <li style={docLinkStyle}>
          <a
            style={linkStyle}
            href={`${docLink.url}?utm_source=starter&utm_medium=start-page&utm_campaign=minimal-starter`}
          >
            {docLink.text}
          </a>
        </li>
        {}
      </ul>
      <img
        alt="Gatsby G Logo"
        src="data:image/svg+xml,%3Csvg width='24' height='24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12 2a10 10 0 110 20 10 10 0 010-20zm0 2c-3.73 0-6.86 2.55-7.75 6L14 19.75c3.45-.89 6-4.02 6-7.75h-5.25v1.5h3.45a6.37 6.37 0 01-3.89 4.44L6.06 9.69C7 7.31 9.3 5.63 12 5.63c2.13 0 4 1.04 5.18 2.65l1.23-1.06A7.959 7.959 0 0012 4zm-8 8a8 8 0 008 8c.04 0 .09 0-8-8z' fill='%23639'/%3E%3C/svg%3E"
      />
    </main>
  )
}

export default IndexPage

gatsby-config

module.exports = {
  siteMetadata: {
    siteUrl: "https://www.yourdomain.tld",
    title: "Playground",
  },
  plugins: [
    "gatsby-plugin-sass",
    "gatsby-plugin-image",
    "gatsby-plugin-react-helmet",
    "gatsby-plugin-sitemap",
    "gatsby-plugin-sharp",
    "gatsby-transformer-sharp",
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "images",
        path: "./src/images/",
      },
      __key: "images",
    },
    {
      resolve: 'gatsby-source-graphcms',
      options: {
        typeName: 'GraphCMS',
        fieldName: 'image',
        endpoint: "URL endpoint,
      }
    }
  ],
};

GraphiQL

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
AS10
  • 271
  • 1
  • 15

1 Answers1

1

Cannot query field "image" on type "Query"

This basically means that you don't have an image node in your query so you can't use the GraphQL query.

According to the documentation, the way of using images should be a query like:

{
  allGraphCmsAsset {
    nodes {
      gatsbyImageData(layout: FULL_WIDTH)
    }
  }
}

In this case, allGraphCmsAsset is the main node created by gatsby-source-graphcms plugin where you have your images stored. If you test it in the GraphiQL playground (localhost:8000/___graphql) you'll see all the available nodes to query so give it a try and prepare your query there.

In your case, since you are customizing a little bit your node, I think your query should look like this:

{
  allGraphCmsAsset {
    nodes {
      image {
         gatsbyImageData(layout: FULL_WIDTH)
      }
    }
  }
}

But as I said, it will be strictly related to your GraphCMS fields, customization, and implementation. In other words, to get your nodes and data, use the GraphiQL playground. Build it there.

Your final structure should look like this:

import React from "react" import { graphql } from "gatsby"

function IndexPage({ data }) {
  return (
    <ul>
      {data.allGraphCmsAsset.nodes.map(image=> (
        <li key={image.id}>
          <GatsbyImage image={image.gatsbyImageData} alt="" />
        </li>
      ))}
    </ul>
  )
}

export const pageQuery = graphql`
  query IndexPageQuery {
    allGraphCmsAsset {
      nodes {
        image { 
           id
           gatsbyImageData(layout: FULL_WIDTH)
        }
      }
    }
  }
`

export default IndexPage

Check how pageQuery is outside the IndexPage component, unlike your provided snippet.

Useful resources:

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Thank you for your detailed response, but why does the query have to go outside of the component? – AS10 Aug 01 '21 at 15:31
  • I also am editing my response from above to include what the explorer has because I am having the same issue, but now its referring to allGraphCMSAsset. I should add that my schema in the CMS is just identified as an assetpicker and when I use their API playground it returns the height, width, and URL (which is what I want) – AS10 Aug 01 '21 at 15:52
  • 1- Because, unless it's a static query, it always go outside (otherwise it will be retriggered in each component hydration). 2- You need more than that, you need the node that Gatsby generates from the image which contain the GatsbyImageData that you need to display the image (by passing it to the component). Can you provide a Codesandbox? It's difficult to guess what's missing just with few pieces – Ferran Buireu Aug 01 '21 at 16:27
  • 1
    Yes let me see if I can put a gatsby one together. – AS10 Aug 01 '21 at 16:29
  • https://codesandbox.io/s/stupefied-wilson-43u0w?file=/gatsby-config.js – AS10 Aug 01 '21 at 16:53
  • I created a new project from a template from Gatsby and that works so I'm not sure what the difference is TBH. – AS10 Aug 01 '21 at 18:09
  • 1
    I was asking to create a sandbox for you to test a new environment under new conditions. That gives me the clue that your issue can be strictly related to the Node version, so the installed packages versions. Try use the same version locally that the working one (removing `node_modules` and reinstalling everything again). Hopefully that will fix your issue – Ferran Buireu Aug 01 '21 at 18:33
  • 1
    That was a good step. I have to put that into my workflow for debugging. I appreciate all of your help. – AS10 Aug 01 '21 at 18:56