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
             ]
          }
     }
}
​​

​​​

​​​​

​​​​​
0 Likes
Saadkun
New Member
5 0 0

I have the same problem

0 Likes
Greg_Kujawa
Shopify Partner
965 80 204

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

0 Likes
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.

0 Likes
SARAHJODAN1
New Member
1 0 0

OK HHHHHHHHHHHHHHHHHHHHHHH

0 Likes
_JB
Shopify Staff
Shopify Staff
750 87 165

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 | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes