Make GraphQL requests with Node and Express

Highlighted
Shopify Staff
Shopify Staff
11 0 3

Learning GraphQL and implementing it into your app can be difficult when first starting out. Please see the simplified tutorial below on how you can quickly start making calls using Node and Express.

 

If you are having issues with the tutorial, please post your question here for others in the community to respond.

 

https://shopify.dev/tutorials/graphql-with-node-and-express

2 Likes
Highlighted
Shopify Partner
5 0 0

Hi all,

 

I'm pretty new to using GraphQL and Node, and I had a quick question regarding the posted tutorial. For the fetch part (fetch("https://<shop>.myshopify.com/admin/api/graphql.json") you are supposed to replace <shop> with your shop, but if I wanted to make a fetch call for the shop name itself because I didn't know the shop url(I'll put the query below), how would I go about doing that? Basically, we are using the store's url (i.e. example.myshopify.com) as the top folder in our database and we want to be able to pass the url as a param to make changes to the database. We need to be able to dynamically change shop names but in order to get the information from GraphQL we need to provide a route, which kind of defeats the purpose of making the call in the first place since we would already know the information we needed.

 

Any advice/help would be greatly appreciated. Thanks in advance!

query: `{
    shop {
      name
      primaryDomain {
        host
      }
    }
  }`
0 Likes
Highlighted
Shopify Staff
Shopify Staff
11 0 3

Hi, if the shop name changed to example123, you'd make the call to the original example.myshopify.com and the name returned would be example123.

0 Likes
Highlighted
Shopify Partner
5 0 0

Hi,

 

I really appreciate the response, but I think I didn't make myself clear that first time. We are writing a public app and thus will not know the 'example.myshopify.com' url in advance. From the front end you can make a query like this that will return the desired url without knowing what it is in advance:

 

const getUrl = gql`
  query {
    shop {
      name
      primaryDomain {
        host
      }
    }
  }
`;


export function GetShopUrl() {
  return ( 
    <Query query={getUrl}>
      {({ data, loading, error }) => {
        if (loading) return <div>Loading…</div>;
        if (error) return <div>{error.message}</div>;
        console.log("Query Results", data);
        console.log("Url", data.shop.primaryDomain.host);
      return <p>{data.shop.primaryDomain.host}</p>;
      }}
    </Query>
  );
}

We only want the data.shop.primaryDomain.host value (i.e. example.myshopify.com) , but it is wrapped inside of <Query/> tags and when we try to get convert the jsx element into say a string we get errors. Is there a way on the server side to accomplish the same thing without knowing the url beforehand that returns a json value rather than a component.

 

Thank you!

0 Likes