Trying to just fetch the name of my store in a React component using the Fetch API and GraphQL endpoint of Shopify.
I created a Shopify store, gave permissions to the Storefront API and crafted this in my componentDidMount() section of my react component.
let query = '{ shop { name } }';
console.log(JSON.stringify(query));
fetch('https://myStoreName.myshopify.com/api/graphql', {
method: "POST",
headers: {
"Content-Type": "application/graphql",
"X-Shopify-Storefront-Access-Token": "<My API Key>"
},
body: JSON.stringify(query),
})
.then(function(myJson) {
console.log('fetched');
console.log(JSON.stringify(myJson));
});
My console log output:
"{ shop { name } }"
App.js:21 fetched
App.js:22 {}
I'm not even getting an error, makes me think it's going through possibly? But can't find my shop's name in this query?
I think overall I don't know best practices to craft a GraphQL query in Javascript. Do I make a large string or just a normal, JSON to represent it?
*Also I'm trying not to use a graphQL client library at the moment, and just to use fetch API.