Im trying to get shofy products from a store using the sortKey BEST_SELLING however im getting an error:
GraphQL Response: {
errors: [
{
message: 'Variable $sortKey of type ProductSortKeys was provided invalid value',
locations: [Array],
extensions: [Object]
}
]
}
This also does not work with PRICE either, error:
GraphQL Response: {
errors: [
{
message: 'Variable $sortKey of type ProductSortKeys was provided invalid value',
locations: [Array],
extensions: [Object]
}
]
}
my implemention:
static async getShopifyProducts(
did: string,
{
first = 6,
after,
before,
searchTerm,
sortKey = 'BEST_SELLING',
reverse = true,
}: {
first?: number;
after?: string | null;
before?: string | null;
searchTerm?: string;
sortKey?: ShopifySortKey;
reverse?: boolean;
},
): Promise<{ products: ShopifyProduct[]; pageInfo: PageInfo } | null> {
const credentials = await this.getShopifyCredentials(did);
if (!credentials) return null;
const { shopDomain, accessToken } = credentials;
const decryptedToken = decryptAccesstoken(accessToken);
try {
// Build GraphQL query
const query = `
query GetProducts(
$first: Int
$after: String
$before: String
$query: String
$sortKey: ProductSortKeys
$reverse: Boolean
) {
products(
first: $first
after: $after
before: $before
query: $query
sortKey: $sortKey
reverse: $reverse
) {
edges {
...
}
pageInfo {
...
}
}
}
`;
const variables = {
first,
after,
before,
query: searchTerm,
sortKey,
reverse,
};
const response = await fetch(
`https://${shopDomain}/admin/api/2025-04/graphql.json`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': decryptedToken,
},
body: JSON.stringify({ query, variables }),
},
);