FROM CACHE - en_header

I tried to make a graphql request using fetch but it always return an empty objects

J_Valencia
New Member
6 0 0
const GRAPHQL_URL = 'https://{store}.myshopify.com/api/2020-10/graphql.json'

const productQuery = () => `
  query {
    shop {
      products( first: 10) {
        edges{
          node {
            id
            handle
            title
	
            variants(first: 10){
              edges{
                node{
                  title
              }
            }
          }
        }
      }  
    }
  }
}
`;


const GRAPHQL_BODY  = () => {
	return {
	'async': true,
	'crossDomain': true,
	'method': 'POST',
	'headers': {
		'X-Shopify-Storefront-Access-Token': STOREFRONT_ACCESS_TOKEN,
		'Content-Type': 'application/graphql',
	},
	'body': productQuery()
	};
}


fetch(GRAPHQL_URL, GRAPHQL_BODY())
        .then(res => res.json())
	.then(console.log)

I tried the code above and this is what I get always

{data: 
    shop: {
         products: {
             edges: [
              length: 0
             ]
          }
     }
}
​​

​​​

​​​​

​​​​​
Replies 5 (5)
Saadkun
New Member
5 0 0
Greg_Kujawa
Shopify Partner
1023 84 268

What happens if you change the Content-Type to application/json?

J_Valencia
New Member
6 0 0

It is still empty. I tried a different query the one that query the store name and it is successful (it showed the name in the object). I think the problem is in the 

products(first: 50){
}

I think this is not working.

I only just want to return all of the products in our store.

SARAHJODAN1
New Member
1 0 0

OK HHHHHHHHHHHHHHHHHHHHHHH

_JB
Shopify Staff
Shopify Staff
836 99 214

Hey @J_Valencia,

Can you confirm the query is working as expected when you send it through an http client like Insomnia? If you haven't tried this yet, please do so.

If you're receiving the expected response using Insomnia but you're still getting an empty response in your app, I can check our logs for more details. In order to do this, I'll ask that you make 2 API calls within a few minutes of each other, one from your app, and one from Insomnia. Grab the X-Request-ID value from the response headers in Insomnia and provide that here, I'll use that to find your calls in our logs.

JB | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog