How to query products

In this example we take you through how to query product data from Marketplacer

What you’ll learn

  • The simplified product data model
  • The use-case for querying products
  • Factors impacting product state
  • The different queries you can use to work with products
  • Alternatives to querying products

The Advert Data Model

For the purposes of this conversation, (querying products), the key relationships of an Advert are shown below:


Advert Schema


The key takeaways from this model are:

  • Adverts belong to a Seller
    • Sellers products are unique to them (the Marketplacer product ID will be unique)
  • Adverts must have a least 1 variant, but can have more
  • Variants have fields such as:
    • SKU
    • Barcode
    • Inventory position (called countOnHand in the Marketplacer APIs)
  • It is ultimately the Variant that gets purchased by the customer (not the Advert)

You can explore the Marketplacer object model in more depth by taking a look at GraphQL Voyager


Before discussing the advert queries, it’s important to cover the following fields on the Advert object as their values can impact both the “state” of the advert, and therefore the queries can be used to retrieve those adverts:

Field NameTypeDescription
publishedbooleanDescribes the intent of the Seller. A Product may be correctly defined and have inventory (at least 1 variant has a positive value for countOnHand), however the Seller may choose not to Sell this item, e.g. it has been withdrawn because a newer version is available. In this example published would be false.

Setting the value of published to false has a cascading effect on the values of the remaining fields outlined in this section, i.e. they too will have the value of false.
displayablebooleanSystem defined value, (unlike published which is set by the Seller), this value represents whether it is possible to Sell this Product in its current state. The value of displayable is determined by Marketplacer and is based on a number of criteria, including but not limited to:

1. whether there is positive inventory (countOnHand) for at least 1 variant,

2. whether all mandatory attributes are configured,

3. if product vetting is enabled, that the vetting process has been successfully completed,

4. whether published is true
onlinebooleanThis state is determined by the status of the Seller of the Advert. I.e. if the Seller has been taken offline by the Operator, then the value of online will be false.

As you will appreciate, the values for these fields can have a number of different combinations, impacting the overall “state” of the Advert. A summary of this is provided below:

PublishedDisplayableOnlineDescription“State”
truetruetrueAdvert is available to sell. The seller has the intent to sell (published is true), the advert has been successfully constructed, with inventory (displayable is true), and the Seller account is online (online is true)Online /
Published
truefalsefalseAdvert is not available to sell. While the Seller has the intent to sell (published is true), for some other reason the advert cannot be displayed, (displayable is false), e.g. vetting is required. In this scenario the value of online is also impacted by the value of displayableOffline /
Unpublished
truetruefalseAdvert is not available to sell. While the Seller has the intent to sell (published is true), and the advert is technically valid (displayable is true), the Operator has taken the Seller offline.Offline /
Unpublished
falsefalsefalseAdvert is not available to sell. The Seller does not have the intent to sell this item (published is false). The value of published also impacts the values of displayable and onlineOffline /
Unpublished

To summarize the above table, only when all 3 fields are true, can the Advert be said to be available for purchase. Adverts in this “state” are usually referred to as either online or published.

For the remainder of this article we’ll use the terms Published and Unpublished.

The Advert Queries

There are 3 Advert queries that can be employed via the Operator API, they each have different characteristics which we will discuss in each section.


advertsWhere

  • Object Types Returned:
  • Advert States Returned:
    [x] Published
    [x] Unpublished
    [x] Deleted (When executed as an Operator)

advertsWhere is a good candidate query when you need to return products based on some filter criteria. This query can also return Published, Unpublished and Deleted Adverts. It should be noted that if no filter criteria is supplied to advertsWhere it will return an empty set. An example query is provided below:

query {
  advertsWhere(retailerIds: ["U2VsbGVyLTE2NQ=="]) 
  {
	nodes {
      id
      legacyId
      title
      published
      online
      displayable
      variants {
        nodes {
          id
          label
          buyable
          published
          displayable
          countOnHand
        }
      }
    }
  }
}

The advertsWhere query returns all Adverts (irrespective of state) as Advert objects. While this allows you to distinguish Published Adverts (all 3 status fields are true), you cannot distinguish between Unpublished and Deleted Adverts using this query.


allAdverts

allAdverts is another potential choice when you want to return Published, Unpublished and Deleted adverts, however it has reduced set of filter criteria:

  • updatedSince: Last time the Adverts were updated
  • vetted: whether the Advert has been vetted
  • requiresVetting: whether the Advert needs to go through the vetting process

In another difference with advertsWhere, instead of returning an Advert object representing Published, Unpublished and Deleted Adverts, allAdverts returns concrete types representing the different states that an Advert can have:

  • Advert - representing Published Adverts
  • UnpublishedAdverts - representing Unpublished Adverts
  • DeletedAdvert - representing Deleted Adverts

An example query is provided below:

query {
	allAdverts(
		first: 1000, 
		updatedSince: "2023-05-29") {
		nodes {
			__typename
			... on Advert {
				legacyId
				updatedAt
				title
				seller {
					businessName
				}
			}
			... on UnpublishedAdvert {
				legacyId
				updatedAt
				details {
					title
					seller {
						businessName
					}
				}
			}
			... on DeletedAdvert {
				legacyId
				id
				updatedAt
			}
		}
	}
}

advertSearch

  • Object Types Returned:

  • Advert States Returned:
    [x] Published
    [ ] Unpublished
    [ ] Deleted

    The primary use-case of advertSearch to be called directly by a frontend eCommerce web application, in providing Product Search functionality directly to end users. It was not designed for system to system integrations.

    advertSearch has the following characteristics:

    • Optimized for User Interactivity, e.g. someone performing a Product Search
    • Returns only Published products (you would not want to return products directly to the user that could not be purchased)
    • Has a large set of filter criteria, to support interactive product search

    An example query is provided below:

query {
	advertSearch(attributes: 
		{ 
			keywordQuery: "shoes"
			brandIds: ["QnJhbmQtNA=="]
		}) {
		adverts(first: 500) {
			totalCount
			nodes {
				id
				featureOptionValues {
					nodes {
						id
						displayName
						optionType {
							category
						}
					}
				}
				productDetails {
					key
					value
				}
				variants {
					nodes {
						administrativeBodyWarning
					}
				}
			}
		}
	}
}

Alternatives to Querying

In terms of moving Product data from Marketplacer into your domain you can use Webhooks instead. In contrast to an API based approach, where the integration “pulls” data at regular intervals, Webhooks push product (and other types of data) to the integration any time Create, Update or Delete events occur on Products. This allows for the near realtime movement of Product data to your integration.