Troubleshooting a GROQ Query for a Collection Carousel Component
12 replies
Last updated: Jan 12, 2023
T
Hi all,
I am trying to build out a collection carousel component and I’m using the Shopify Connect plugin to sync the data from the Shopify store which is all good and the data is coming through, but the problem I am having is when I am retrieving the data in my Hydrogen front-end, the actual products inside the collection that has been selected are not coming back in the request and I’m struggling to find out how to get the product data to come back
My current GROQ query is:
And the resulting data from that GROQ query is the following:
My schema is as follows:
Would appreciate any help with this
I am trying to build out a collection carousel component and I’m using the Shopify Connect plugin to sync the data from the Shopify store which is all good and the data is coming through, but the problem I am having is when I am retrieving the data in my Hydrogen front-end, the actual products inside the collection that has been selected are not coming back in the request and I’m struggling to find out how to get the product data to come back
My current GROQ query is:
*[_type == "home"]{ modules[]{ _key, _type, ......, defined(collection) => {collection}, defined(button) => {button}, defined(heading) => {heading}, defined(productsToShow) => {productsToShow}, ......, } }
{ _key:c7bc0977eabdfde721a019b2fedee81f _type:collectionCarrousel button: { _type:ctaButton link:/collections/annes-picks text:Shop now } collection: { _ref:shopifyCollection-26024345637 _type:reference } heading:Layering Necklaces productsToShow:8 }
import {BlockElementIcon} from '@sanity/icons' export default { title: 'Collection carrousel', name: 'collectionCarrousel', type: 'object', icon: BlockElementIcon, preview: { prepare({media}) { return { title: 'Collection carrousel', media: BlockElementIcon, } }, }, fields: [ { title: 'Collection products to be displayed', name: 'collection', type: 'reference', to: [{type: 'collection'}], }, { title: 'Heading', name: 'heading', type: 'string', description: 'Heading for the collection carrousel', }, { title: 'Amount of products to show', name: 'productsToShow', type: 'number', description: 'Amount of products to show in the collection carrousel', validation: (Rule) => Rule.required().min(1).max(50), }, { title: 'Button', name: 'button', type: 'ctaButton', description: 'Button displayed under carrousel', }, ], }
Jan 12, 2023, 12:30 AM
N
have you tried
defined(collection) -> {collection}?
Jan 12, 2023, 12:56 AM
N
or
defined(collection)[] ->
Jan 12, 2023, 12:57 AM
T
Hi
user G
, just tried those suggestions and neither workJan 12, 2023, 1:00 AM
S
defined(collection) => {collection[]->}
Jan 12, 2023, 12:45 PM
S
I just tried it on my schema and it returned all the information. The defined function just takes an argument and evaluates if its true. You will still have to expand the reference in between the curly braces once its true
Jan 12, 2023, 12:52 PM
S
I hope it works for you as well 🙂
Jan 12, 2023, 12:54 PM
T
hey
Every thing I’ve just tried comes back as
ie:
user C
thanks for this, I did think this might need to be done but I don’t know what property it is I need to output?Every thing I’ve just tried comes back as
null
ie:
defined(collection) => {collection[] -> { ..., products }},
Jan 12, 2023, 1:09 PM
S
There shouldn't be a need to add an output when you try
collection[]->. If it returns null right away, then that means it is not retrieving data from that document at all. I just realized that you have type set to reference and not array, so I kinda made a mistake 😅. Could you try
collection ->inside the curly braces
Jan 12, 2023, 1:17 PM
T
Ooo, that has given me a lot more information back and specifically returns me the
gidwhich I can use shopify hydrogens useShopQuery hook i think to return me the products from the set collection
Jan 12, 2023, 1:20 PM
T
thanks
Jan 12, 2023, 1:20 PM
S
If that worked then you can start choosing what you would like to output
defined(collection) => {collection -> { ..., products }}
Jan 12, 2023, 1:21 PM
S
You're welcome
Jan 12, 2023, 1:21 PM
Sanity.io – build remarkable experiences at scale
Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.