Retreiving a cash tracking session by pos device id

Hi,

I am trying to get the cash tracking session for the session of the current pos device in my pos ui extension. I have a graphql query that filters by device id of the current device. I get the current device id with await shopify.device.getDeviceId();. The query is not returning any sessions, even though they exist for this device. If I remove the query filter and get all cash tracking sessions, then I see sessions for my current device.

This is the entire function: What am I doing wrong?

async function getExpectedCashAmt(setDebugMsg) {
  var deviceID = await shopify.device.getDeviceId();
  setDebugMsg('device: ' + deviceID);
  const queryString =  \`
        query getCashSession{
            cashTrackingSessions(first: 1, sortKey:OPENING_TIME_DESC, query:"point_of_sale_device_ids:${deviceID}") {
            edges {
              node {
                id,
                registerName
                closingBalance { amount currencyCode }
                expectedBalance { amount currencyCode }
              }
            }
    }} \`
  try {
    if(deviceID) {
    const res = await fetch('shopify:admin/api/graphql.json', {
      method: 'POST',
      body: JSON.stringify({
        query:queryString
    })
    });
    return res.json();
  }
  else return Promise.reject(new Error('no register name provided'));
  }
  catch (e) {
    return Promise.reject(e);
  }
}
Thank you!

Hi Wilen,

For string queries, your should be able to simply use the variable directly.
The API Search Syntax page has an example for a similar use case at Shopify API search syntax
with query=first_name:Bob.

So, in your case,query:“point_of_sale_device_ids:”+deviceID+” (with traditional string concat instead of ${})seems like it should be correct.

example with variants, looking for title “2x”:
productVariants(first:10 query:“title:2x” ){}

Thank you for your input! My real issue is this.

var deviceID = await shopify.device.getDeviceId(); 
//returns 7a16af8ef26bf5e6

When I enter the full query in graphiql testing window, it returns no sessions. But, if I leave out the filter then it does return a few sessions for my device. This is the query I enter in the graphiql playground:

query getCashSession {
          cashTrackingSessions(first: 1, sortKey:OPENING_TIME_DESC,
          query:“point_of_sale_device_ids:‘7a16af8ef26bf5e6’”) {
          edges {
          node {
          id,
          registerName
          closingBalance{amount}
          expectedBalance{ amount currencyCode }
          }
     }
}}

Basically, what I want to know is this:
does await shopify.device.getDeviceId() return the value expected by the filter 'point_of_sale_device_ids' when retreiving a CashTrackingSession?
If not, how do I get the device id of the current pos device?

Hi @Wilen_Consulting, thanks for reaching out. At the moment, this is a current limitation with the POS UI Extensions API.

The shopify.device.getDeviceId() method returns a UUID (like 7a16af8ef26bf5e6), which is designed for device-specific data storage and analytics. However, the point_of_sale_device_ids filter on the cashTrackingSessions query expects a GID (Global Identifier) in the format gid://shopify/PointOfSaleDevice/12345.

These are two different identifier types, and there’s currently no API available to convert the UUID to a GID or retrieve the device GID directly from a POS UI Extension. This is something our team is aware of though - I can’t guarantee a fix or what it may look like, but it’s something that’s on our radar.

In the meantime, one workaround is to filter by location_id instead. You can get the location ID from the Session API and construct the GID like this:

const locationGid = `gid://shopify/Location/${api.session.currentSession.locationId}`;

Then use location_id in your query filter instead of point_of_sale_device_ids. This won’t give you the exact device, but it could work if your location only has one active cash session at a time, or if you combine it with status or time-based filters like opening_time to narrow things down. I definitely understand it’s not the most ideal reply, but hope this helps a bit. Let me know if I can clarify anything else on our end here.