Passing an enum in GraphQL variable

Solved
PLMServices
Tourist
6 1 1

Hi.

I've had a pretty good search and can't find any documentation or examples to solve an issue I'm having.
I'm trying to call the productCreate mutation (but this problem type would apply to any query really), to add a product to our store but have been unable to figure out what format to pass enums in when they are in a "variables" declaration.

Here is a cutdown version of the mutation:

mutation ProductMutation($add0Input: ProductInput!) {
       add0: productCreate(input: $add0Input) {
               product {
                       id
               }
               shop {
                       id
               }
               userErrors {
                       field
                       message
               }
       }
}

And here is the matching variables declaration:

{
  "add0Input": {
    "handle": "XXXX",
    "title": "Product Title",
    "descriptionHtml": "<p>Product Description</p>",
    "variants": [
      {
        "sku": "XXXX",
        "weight": 1,
        "price": "179.96",
        "taxable": true,
        "inventoryItem": {
          "tracked": false
        }
      }
    ],
    "collectionsToJoin": [
      "gid://shopify/Collection/111111111"
    ],
    "images": [
      {
        "src": "https://example.com/files/shopify-inventory/image.jpg",
        "altText": "product"
      }
    ],
    "vendor": "Us",
    "status": "ACTIVE"
  }
}

This gets me back this error:

{
  "errors": [
    {
      "message": "Variable $add0Input of type ProductInput! was provided invalid value for status (Field is not defined on ProductInput)",
      "locations": [
        {
          "line": 2,
          "column": 26
        }
      ],
      "extensions": {
        "value": {
          "handle": "XXXX",
          "title": "Product Title",
          "descriptionHtml": "<p>Product Description</p>",
          "variants": [
            {
              "sku": "XXXX",
              "weight": 1,
              "price": "179.96",
              "taxable": true,
              "inventoryItem": {
                "tracked": false
              }
            }
          ],
          "collectionsToJoin": [
            "gid://shopify/Collection/111111111"
          ],
          "images": [
            {
              "src": "https://example.com/files/shopify-inventory/image.jpg",
              "altText": "product"
            }
          ],
          "vendor": "Us",
          "status": "ACTIVE"
        },
        "problems": [
          {
            "path": [
              "status"
            ],
            "explanation": "Field is not defined on ProductInput"
          }
        ]
      }
    }
  ]
}

So it appears I can't pass an enum as a JSON String (since it's not a string), but you also can't just use the unquoted value as that's not a valid JSON type.
Here is the documentation for the ProductStatus type:
https://shopify.dev/docs/admin-api/graphql/reference/products-and-collections/productstatus

Can anyone point to some relevant documentation, or better yet show a working example?

Thanks.

0 Likes
PLMServices
Tourist
6 1 1

So further to this, I've done some more research on and GraphQL.org seems to indicate that for JSON payloads perhaps enums should be sent as strings (https://graphql.org/learn/schema/#enumeration-types) so I've tried using 'ACTIVE' and 'ProductStatus.ACTIVE' both of which give the same error:

"message": "Variable $add0Input of type ProductInput! was provided invalid value for status (Field is not defined on ProductInput)"

 

GraphQL also seemed to indicate that perhaps this handling may be up to the individual implementation.

So this leave me with the following thoughts:

  1. Is the Shopify GraphQL handling broken for enum types? I think it's very possible.
  2. The error message is terribly ambiguous. On one hand it says status has an invalid value, which is the avenue I've been investigating. But on the other hand it also says "Field is not defined on ProductInput". You can see from the documentation of ProductInput that status is definitely defined for this type, so is the error message text broken, or is something more broken behind the scenes?

I'd really love to get some feedback from Shopify on this.

0 Likes
PLMServices
Tourist
6 1 1

This is an accepted solution.

So I have a solution for this, albeit a frustrating one, which hints from other posts steered me into, and so I'll post it here in case it helps someone in the future.

It appears to be properly supported already depending on the API that you are using.

I was sending my queries to this URL which I got from some of the Shopify documentation:

 

 

POST /admin/api/graphql.json

 

 

If you use that URL, sending the Product status field doesn't work, probably because whatever API version this default URL is pointing to does NOT include that field on Product.

However sending the status field as a text string, i.e. "ACTIVE", "DRAFT", etc, worked once I changed my URL to:

 

 

POST /admin/api/2021-04/graphql.json

 

 

 

tuhc
New Member
1 0 1

Just wanted to give you my thanks! I was stuck on this for 2-3 days and thought I wrote my query wrong but it really just was the URL