Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more than 250 results #5

Merged
merged 1 commit into from
May 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions src/services/ShopifyAdminApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
{
list($url, $token) = $this->getCreds();
$this->client = new Client([
'base_uri' => $url.'/admin/api/2022-01/',
'base_uri' => $url.'/admin/api/2022-04/',
'headers' => [
'X-Shopify-Access-Token' => $token,
],
Expand Down Expand Up @@ -59,34 +59,68 @@ public function execute($payload)
else throw new Exception($response->getBody());
}

/**
* Execute a GQL query and paginate through the results
*/
public function paginate($payload)
{
$results = [];
do {

// Fetch this page and add to the results
$response = $this->execute($payload);
$results = array_merge(
$results,
$this->flattenEdges($response)['results'],
);

// If there is another page, add the end cursor to the next
// request
$pageInfo = $response['results']['pageInfo'] ?? [];
$hasNextPage = $pageInfo['hasNextPage'] ?? false;
if ($hasNextPage) {
$payload['variables'] = array_merge(
$payload['variables'] ?? [],
['cursor' => $pageInfo['endCursor']],
);
}
} while ($hasNextPage);

// Return the final list of results, fixing string keys
return $results;
}

/**
* Get all products
*/
public function getProducts()
{
$response = $this->execute([
'query' => '{
products(first:250) {
return $this->paginate([
'query' => 'query getProducts($cursor: String) {
results: products(first:250, after:$cursor) {
edges {
node {
title
handle
}
}
pageInfo {
hasNextPage
endCursor
}
}
}'
]);
return $this->flattenEdges($response)['products'];
}

/**
* Get all variants of all products
*/
public function getVariants()
{
$response = $this->execute([
'query' => '{
productVariants(first:250) {
$variants = $this->paginate([
'query' => 'query getVariants($cursor: String) {
results: productVariants(first:250, after:$cursor) {
edges {
node {
title
Expand All @@ -97,45 +131,52 @@ public function getVariants()
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}'
]);

// Get array of variants
$variants = $this->flattenEdges($response)['productVariants'];

// Remove variants that are missing a sku
$variants = array_filter($variants, function($variant) {
return !empty($variant['sku']);
});

// Make a title that is more useful for displaying in the CMS.
return array_map(function($variant) {
$variants = array_map(function($variant) {
$variant['dashboardTitle'] = $variant['product']['title']
.' - '.$variant['title']
.(($sku = $variant['sku']) ? ' ('.$sku.')' : null);
return $variant;
}, $variants);

// Convert string keys to integer
return array_values($variants);
}

/**
* Get all collections
*/
public function getCollections()
{
$response = $this->execute([
'query' => '{
collections(first:250) {
return $this->paginate([
'query' => 'collections($cursor: String) {
results: collections(first:250, after:$cursor) {
edges {
node {
title
handle
}
}
pageInfo {
hasNextPage
endCursor
}
}
}'
]);
return $this->flattenEdges($response)['collections'];
}

/**
Expand Down