Shopify CLI GraphQL returns status 400

Topic summary

Issue: A developer encountered a 400 status error when attempting to fetch products using GraphQL queries through Shopify CLI, despite following tutorial instructions.

Initial Troubleshooting Attempts:

  • Verified authentication setup in shopify.server.js file
  • Confirmed the query worked in the Shopify GraphQL app but failed in the actual code
  • Tested mutations successfully, ruling out broader authentication issues
  • Investigated environment variables and token configuration in shopify.app.toml

Root Cause & Resolution:
The problem was identified as incorrect query string formatting. The solution involved:

  • Using the #graphql directive at the start of the query string
  • Properly structuring the GraphQL query with correct syntax for nested fields (products, variants, images, selectedOptions)

Key Takeaway: The issue was not related to authentication or token validity, but rather the query string format itself. Multiple users experienced this same error, suggesting it’s a common pitfall when setting up Shopify CLI GraphQL queries.

Summarized with AI on November 15. AI used: claude-sonnet-4-5-20250929.
export async function loader({ request, params }) {
  try {
    const { admin, session } = await authenticate.admin(request);

    const queryString = `query {
      products () {
        edges {
          node {
            id
            title
          }
        }
      }
    }`

    const response = await admin.graphql({
      data: queryString
    })
    
  } catch (error) {
    console.error("Authentication or GraphQL error:", error);
}

I am trying to get products with GraphQL and getting the following error. I am using the Shopify CLI, I tried to follow everything in the tutorial, but I spent 6 hours trying to solve this issue. Please help! LOL.

This is my configuration:

Hi Oozbasaran,

Can you check to make sure that your access token is valid? If you’re unsure, you can regenerate a new token and replace it in your code.

Another option to troubleshoot would be to run the same query in the Shopify GraphiQL app to see if it’s working there. Depending on if it works or not you can try to rule out what’s causing this.

Hope this helps,

My shopify.server.js file is like the above. Does not it handle the Auth automatically?

The query is working in the Shopify GraphQL app, but not in my code, I tried a mutation and it is working in my code, but not the query.

I’m having the same error:

In an app set up with Shopify CLI, a simple graphql query (near identical format to OP) is returning a 400.

This set up seems to match the docs, so any input on how to get a simple query running in a shopify app would be a huge help.

Thanks!

Been doing a bit more digging myself, in the shopify.server.js file it’s referencing process.env, so I think we may need save the token in an env file somewhere; however, some of these values seem to be automatically set in the shopify.app.toml file

This however, is still giving me a 400 error

Hi there! I played around with this some more and ultimately found that the issue was with my query string. This ended up working for me:

`#graphql
      query getProducts{
      products(first: 10, reverse: true) {
        edges {
          node {
            id
            title
            handle
            images(first: 1) {
              edges {
                node {
                  originalSrc
                }
              }
            }
            variants(first: 1) {
              edges {
                node {
                  price
                  selectedOptions {
                    name
                    value
                  }
                }
              }
            }
          }
        }
      }
    }`
1 Like

Great to hear this is working for you! Thanks for posting up your solution!