Contextual History

In this example we take you through how query for the contextual history of a number of different entity types.

What you’ll learn

In this you’ll learn:

  • What is contextual history
  • What items have a contextual history
  • How to query contextual history using the Operator API

What is Contextual History

Think of contextual history as an audit log of events that have occurred on a given entity. For example you can:

  • Look at the update events on a Variant to look at the changes to sku, barcode, countOnHand etc.
  • Look ar the create event for Adverts to understand when they were first ingested into Marketplacer

The fields available for the ContextualHistory object are listed below:

FieldData TypeDescription
changesJSONA collection of changes on a given object field (e.g. Sku ot Barcode) with a previous and new values.
createdAtISO8601DateTime!The timestamp when the event occured
eventString!1 of 3 event types: create, update or destroy
idID!The unique identifier for the event (in Base 64)
itemIdInt!The unique identifier of the entity / item impacted by the change
itemTypeString!The type of entity that was impacted refer to the What items have a contextual history for a full list of the items
originStringWhat emitted the event, e.g. was it a system component, and API call or a user interaction.
whodunnitStringIn the case of a human generated event, the id of the user
whodunnitTypeStringIn the case of a human generated event, the type of user account

What items have a contextual history

The following items can have a contextual history:

  • Address
  • Admin
  • Advert
  • AccountSettings
  • AdvertSettings
  • CustomisationSettings
  • EcommerceSettings
  • ExternalAccountSettings
  • Invoice
  • InvoiceAmendment
  • InvoiceAmendmentLineItem
  • InvoiceAnnotation
  • LineItem
  • RefundRequest
  • RefundRequestLineItem
  • RefundRequestNote
  • Email
  • PricingDetail
  • Shipment
  • Subscription
  • User
  • Variant

You can supply these values as a filter to the contextualHistory query when you want to narrow down your search.

Using the contextualHistory query

Please refer to our GraphQL Best Practices Guide when writing queries for production systems.

Below we have provided a few examples of how to use the contextualHistory query.

Example 1: Query for all events and entities created after a given time

query ContextualHistorySince {
  contextualHistory(
    first: 100
    after: null
    createdAt: { gt: "2024-03-06T02:30:12+11:00" }
  ) {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      createdAt
      changes
      event
      id
      itemId
      itemType
      origin
      whodunnit
      whodunnitType
    }
  }
}


Example 2: Query for all update events on the Variant item type

query ContextualHistorySince {
  contextualHistory(
    first: 100
    after: null
    itemType: "Variant"
    event: "update"
  ) {
    totalCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      createdAt
      changes
      event
      id
      itemId
      itemType
      origin
      whodunnit
      whodunnitType
    }
  }
}