phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
570 stars 210 forks source link

collectionByHandle is not working with variable #214

Closed atikju closed 3 years ago

atikju commented 3 years ago

What am I missing here? See my codes below:

$collection_handle = "my-handle";

$graphQL = <<<Query
query {
  collectionByHandle(handle: \$handle) {
    products(first: 30) {
      edges {
        node {
          images(first: 1) {
            edges {
              node {
                src
              }
            }
          }
          handle
          variants(first: 10) {
            edges {
              node {
                compareAtPrice
                price
              }
            }
          }
        }
      }
    }
  }
}
Query;

$variables = [
    "handle" => $collection_handle
];
tareqtms commented 3 years ago

@atikju Any detail what error you are getting?

atikju commented 3 years ago

@tareqtms

Fatal error: Uncaught PHPShopify\Exception\ApiException: message - Variable $handle is used by anonymous query but not declared, locations - line - 2, column - 30, path - query, collectionByHandle, handle, extensions - code - variableNotDefined, variableName - handle in phpclassic/php-shopify/lib/ShopifyResource.php:554 Stack trace: #0 phpclassic/php-shopify/lib/GraphQL.php(49): PHPShopify\ShopifyResource->processResponse(Array) #1 xcollections/index.php(66): PHPShopify\GraphQL->post('query {\n colle...', 'https://f78bf26...', NULL, Array) #2 {main} thrown in phpclassic/php-shopify/lib/ShopifyResource.php on line 554

tareqtms commented 3 years ago

@atikju Can you show the rest of the code, where you are making the final call?

atikju commented 3 years ago

@tareqtms

I'm just doing this:

$data = $shopify->GraphQL->post($graphQL, null, null, $variables);

echo "<pre>";
var_dump($data["data"]);
echo "</pre>";
tareqtms commented 3 years ago

Try this

$graphQL = <<<Query
query(\$handle: String!) {
  collectionByHandle(handle: \$handle) {
    products(first: 30) {
      edges {
        node {
          images(first: 1) {
            edges {
              node {
                src
              }
            }
          }
          handle
          variants(first: 10) {
            edges {
              node {
                compareAtPrice
                price
              }
            }
          }
        }
      }
    }
  }
}
Query;
atikju commented 3 years ago

@tareqtms

This one works mate! Thanks very much!