Search code examples
shopifyshopify-api

I get "invalid id" when using the new fileDelete to remove a file from the admin page


I've been able to make a script to save a file using the new fileCreate but when I try to delete a file with fileDelete, I can't seems to get the right ID.

mutation fileDelete($fileIds: [ID!]!) {
     fileDelete(fileIds: $fileIds) {
          userErrors {
            field
            message
          }
          deletedFileIds
    }
}

I pass the ID from my last image: "gid://shopify/ImageSource/20805776113730", called with a new private app key (which should use the v2021-10)

If anybody got that mutation to work, I would appreciate any help.

Response from Shopify:

{
    "data": {
        "fileDelete": null
    },
    "errors": [{
        "message": "invalid id",
        "locations": [{
            "line": 3,
            "column": 4
        }],
        "path": ["fileDelete"]
    }],
    "extensions": {
        "cost": {
            "requestedQueryCost": 10,
            "actualQueryCost": 1,
            "throttleStatus": {
                "maximumAvailable": 1000,
                "currentlyAvailable": 999,
                "restoreRate": 50
            }
        }
    }
}

Here is what's send:

{
    "query": "mutation fileDelete($fileIds: [ID!]!) {\r\n\t\t\tfileDelete(fileIds: $fileIds) {\r\n\t\t\t  userErrors {\r\n\t\t\t\tfield\r\n\t\t\t\tmessage\r\n\t\t\t  }\r\n\t\t\t  deletedFileIds\r\n\t\t\t}\r\n\t\t  }",
    "variables": {
        "fileIds": "gid:\/\/shopify\/ImageSource\/20825330909250"
    }
 }

Solution

  • You need to send the ImageMedia ID:

    {
        "fileIds": "gid://shopify/MediaImage/20835931816073"
    }
    

    To get that ID, you need to send your request like this:

    {
      files(first: 99, reverse:true){
        edges{
          cursor
          node{
            ... on MediaImage {
              id
              mimeType
              image {
                originalSrc
              }
            }
            __typename
            createdAt
            fileStatus
            preview{
              image{
                altText
                id
                transformedSrc
                originalSrc
              }
              status
            }
          }
        }
      }
    }
    

    Response:

    {
      "data": {
        "files": {
          "edges": [
            {
              "cursor": "xxxxxx",
              "node": {
                "id": "gid:\/\/shopify\/MediaImage\/xxxx",
                "mimeType": "image\/png",
                "image": {
                  "originalSrc": "xxxx"
                },
                "__typename": "MediaImage",
                "createdAt": "2021-10-21T17:39:58Z",
                "fileStatus": "READY",
                "preview": {
                  "image": {
                    "altText": "xxxx",
                    "id": "gid:\/\/shopify\/ImageSource\/xxxx",
                    "transformedSrc": "xxxx",
                    "originalSrc": "xxxx"
                  },
                  "status": "READY"
                }
              }
            },
          ]
        }
      },
      "extensions": {
        "cost": {
          "requestedQueryCost": 398,
          "actualQueryCost": 18,
          "throttleStatus": {
            "maximumAvailable": 1000.0,
            "currentlyAvailable": 982,
            "restoreRate": 50.0
          }
        }
      }
    }