I’m looking for a GraphQL query example on how to get the value of custom attributes on Catalog Items. Is this supported or do I have to use the standard API?
Yes, custom attributes are supported with GraphQL. What error’s are you getting when trying to retrieve the catalog objects custom attributes?
What I’d like to do is to search the catalog for my menu items, and have the returned objects have their custom attributes included. For example, I have an attribute for the number “Servings” the item provides and another for whether it is “Popular.”
Working with the Square GraphQL schema a bit more I realized the customAttributes field hangs off the variations field of a CatalogItem. Even though there is a customAttributes field at the Item level it seems to always be null.
Additionally custom attributes in the list is one of CatalogCustomAttributeUnion type and each attribute type has to be handled separately and aliased. No problem so far, except documentation around this would save devs time (see my queries below).
However there seems to be some bugs.
BUG #1) I’d like to get the name of the attribute not just the value so I can disambiguate it from others of the same type. However the definition field for the attribute returns null for the name field. That said, I can get the IDs for all the custom attribute objects and do a post query join in the code before returning my GraphQL response to my client (this is the point of the second query below in the combined query). However, this is kludgy and should be unnecessary.
BUG #2) Secondly if a Selection type attribute has multiple distinct values on a variation the returned uids list have all the same values. For example, I have a Dietary Preferences custom attribute with Gluten Free, Vegan and Vegatarian as allowed selections. In the Square web UI I set an item to say, vegan and vegetarian. But when I make a GraqhAPI call for it I get two duplicate uids for the selections. (I’m happy to share an example response if that helps). The V2 REST API does not exhibit this bug.
query CatalogItemsQuery {
catalogItems(
filter: {merchantId: {equalToAnyOf: "..."},
enabledLocationId: "...",
customAttribute: {id: "...", bool: true}
}
) {
nodes {
id
name
variations {
customAttributes {
definition {
id
name # <<< this always comes back NULL
}
value {
... on CatalogCustomAttributeBoolean {
boolValue: value
}
... on CatalogCustomAttributeString {
stringValue: value
}
... on CatalogCustomAttributeSelection {
uids
}
... on CatalogCustomAttributeNumber {
numberValue: value
}
}
}
priceMoney {
amount
}
}
}
}
catalog (
filter: {
merchantId: {
equalToAnyOf: "..."
},
type: {
equalToAnyOf: [CUSTOM_ATTRIBUTE_DEFINITION]
}
}
) {
nodes {
... on CatalogCustomAttributeDefinition {
__typename
id
type
name
config {
... on CatalogCustomAttributeSelectionConfig {
allowedSelections {
uid
name
}
}
}
}
}
}
}
Hey @kevmo9000! Do you mind sharing that example response? I’ll try to reproduce this with my own account.
Hi @josh-square,
Here is a fragment of the response that shows the described bugs:
{
"id": "FGPDLGII346YUGCVY6TWYJOP",
"name": "Cornbread (24 count)",
"description": "Moist and healthy vegan cornbreads made with coconut oil instead of butter.",
"category": {
"name": "Catering",
"id": "LRBXWTKZ46U6CDCSWT74NG23"
},
"images": null,
"variations": [
{
"customAttributes": [
{
"definition": {
"id": "VVIZIP4T7DS3LXMFDV57KFLP",
"type": null,
"key": null,
"name": null
},
"value": {
"boolValue": true
}
},
{
"definition": {
"id": "XJ5LBWG5OYEOMDLNGE74IEWM",
"type": null,
"key": null,
"name": null
},
"value": {
"uids": [
"66QP5JAFFKU37ZIAX6WLPPBG",
"66QP5JAFFKU37ZIAX6WLPPBG"
]
}
},
{
"definition": {
"id": "6RGSVJQXWMGSDXSO6QWL2OLN",
"type": null,
"key": null,
"name": null
},
"value": {
"numberValue": "24.0"
}
},
{
"definition": {
"id": "ROQ7VPKLCL3LHZ6VNQEGSPXB",
"type": null,
"key": null,
"name": null
},
"value": {
"boolValue": true
}
}
],
"priceMoney": {
"amount": ...
}
}
]
}
Let me know if you need anything else.
Do you mind sharing the Merchant ID that you’re querying as well?
I sent it to you via DM on Discord.
Thanks for the heads up! We’ll look into it.
Hi Josh,
Any update here?
Thanks,
Kevin
Okay, so it looks like Bug#1 is fixed, but Bug#2 is still extant. I’m getting all duplicate UIDs for custom attributes of type `CatalogCustomAttributeSelection when there are multiple selections.