Last active
November 16, 2023 07:07
-
-
Save jorgemasta/fc5b29c59bd66d26ce3d28c1b71130b3 to your computer and use it in GitHub Desktop.
Custom GraphQL query to get products with related products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { productPrices, multipleChoiceOptionFragment } from '@framework/api/fragments/product'; | |
const productInfoFragment = /* GraphQL */ ` | |
fragment productInfo on Product { | |
entityId | |
name | |
path | |
brand { | |
entityId | |
} | |
description | |
prices { | |
...productPrices | |
} | |
images { | |
edges { | |
node { | |
urlOriginal | |
altText | |
isDefault | |
} | |
} | |
} | |
variants { | |
edges { | |
node { | |
entityId | |
defaultImage { | |
urlOriginal | |
altText | |
isDefault | |
} | |
} | |
} | |
} | |
productOptions { | |
edges { | |
node { | |
__typename | |
entityId | |
displayName | |
...multipleChoiceOption | |
} | |
} | |
} | |
relatedProducts { | |
edges { | |
node { | |
name | |
} | |
} | |
} | |
localeMeta: metafields(namespace: $locale, keys: ["name", "description"]) | |
@include(if: $hasLocale) { | |
edges { | |
node { | |
key | |
value | |
} | |
} | |
} | |
} | |
${productPrices} | |
${multipleChoiceOptionFragment} | |
` | |
const productConnectionFragment = /* GraphQL */ ` | |
fragment productConnnection on ProductConnection { | |
pageInfo { | |
startCursor | |
endCursor | |
} | |
edges { | |
cursor | |
node { | |
...productInfo | |
} | |
} | |
} | |
${productInfoFragment} | |
` | |
export const customGetAllProductsQuery = /* GraphQL */ ` | |
query getAllProducts( | |
$hasLocale: Boolean = false | |
$locale: String = "null" | |
$entityIds: [Int!] | |
$first: Int = 10 | |
$products: Boolean = false | |
$featuredProducts: Boolean = false | |
$bestSellingProducts: Boolean = false | |
$newestProducts: Boolean = false | |
) { | |
site { | |
products(first: $first, entityIds: $entityIds) @include(if: $products) { | |
...productConnnection | |
} | |
featuredProducts(first: $first) @include(if: $featuredProducts) { | |
...productConnnection | |
} | |
bestSellingProducts(first: $first) @include(if: $bestSellingProducts) { | |
...productConnnection | |
} | |
newestProducts(first: $first) @include(if: $newestProducts) { | |
...productConnnection | |
} | |
} | |
} | |
${productConnectionFragment} | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment