GraphQL And Ajax Issue

Jackson_Behan1
Shopify Partner
28 3 2

I am experiencing a very strange problem and I can't begin to understand why it's happening.

 

I have multiple custom built websites (not built on shopify) that use the shopify api to pull in data. On the frontend of my sites I make an Ajax call to a php file on my server that grabs collections and their products using curl and shopify's GraphQL API.

 

This has been working for years and was literally working yesterday 1/18/23. Today, every site I do this for (4 sites, 4 separate shopify stores) they are all broken.

 

The strangest thing is if I run the file in the terminal it returns all the data. For some reason starting today, when the file is called through Ajax from the front end - all the data is null.

 

Here is an example of the php file. I removed the sensitive data:

 

<?php

$collections = array('sauces', 'apparel', 'special');

$all_products_array = array();

foreach($collections as $collection_handle) {

$query = <<<'JSON'
query($collection_handle: String!) {
  collectionByHandle(handle: $collection_handle) {
    handle
    products(first: 50) {
      edges {
        cursor
        node {
          id
          title
          status
          description
          tags
          images(first: 1) {
            edges {
              node {
                originalSrc
              }
            }
          }
          variants(first: 10) {
            edges {
              node {
                id
                price
                inventoryQuantity
              }
            }
          }
        }
      }
    }
  }
}
JSON;


  $curl = curl_init('https://xxxxxxx.myshopify.com/admin/api/graphql.json');

  $post_data = array();

  $vars = array();

  $vars['collection_handle'] = $collection_handle;

  $post_data['query'] = $query;

  $post_data['variables'] = $vars;

  $post_data = json_encode($post_data);

  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'X-Shopify-Access-Token: XXXXXXXXXXXX')
  );

  $results = curl_exec($curl);

  curl_close($curl);

  $results = json_decode($results);

  $results = $results->data->collectionByHandle;

  array_push($all_products_array, $results);
}

echo json_encode($all_products_array);

?>

Again, running this code in terminal works exactly how it should and always has - line 80 echos out an array of arrays of product data.

 

When I call this file from the front end using Ajax, starting today, it returns an array of nulls. I have not touched anything from yesterday to today and I have no idea how to troubleshoot this.

 

Any insight would be a greatly appreciated.

Replies 0 (0)