Can Shopify Admin API(GraphQL) get Product's "multi-line" description?

Hi.

I would like to use the Admin API (GraphQL) to retrieve the bodyHtml of a Product (that has product’s description) and display the retrieved HTML format string embedded as a multi-line string in my custom app textarea.

However, to do so requires converting HTML formatting to multi-line text, which is inefficient if one were to edit this textarea and then convert its value back to HTML formatting for the payload to mutate to the Admin API. The following is an example of the inefficient way to do this.

It would be nice to be able to retrieve the product description not in HTML format but as a string containing line feed characters such as “\n”, which can then be embedded directly into the textarea.

Any solution? Thanks!

Hello,

I understand your concern about the inefficiency of converting HTML formatting to multi-line text and vice versa. Retrieving the product description with line feed characters for direct embedding in a text area can indeed streamline the process. Unfortunately, as of my last update in September 2021, Shopify’s Admin API (GraphQL) returns product descriptions in HTML format, and there isn’t a built-in way to fetch them with line feed characters.

One approach to consider would be to implement a preprocessing step in your custom app:

  1. Retrieval: Fetch the product description using the Admin API (GraphQL) in HTML format as you normally would.

  2. Conversion: In your app, implement a converter that can convert HTML to plain text while preserving line breaks. There are libraries available, like Beautiful Soup in Python or DOMParser in JavaScript, that can help extract text content while keeping line breaks.

  3. Embedding: Once you have the plain text version with line breaks, you can embed it directly into your textarea for display to the user.

  4. Editing: If the user edits the description in the textarea, you’d convert it back to HTML format using a similar approach but in reverse. This might involve converting line breaks to
    tags and handling other formatting accordingly.

  5. Mutation: When sending the edited content back to the Admin API, you would convert it back to HTML format as required by the API.

While this approach does involve an additional conversion step, it can help provide a smoother editing experience for users while interacting with your app. Keep in mind that APIs and features might have evolved since my last update, so I recommend checking Shopify’s documentation or any recent updates for any new solutions that might have been introduced.

Remember always to consider best practices for handling HTML content and user inputs to ensure security and data integrity in your app.

I hope this helps, and I wish you success in developing your custom Shopify app!

Thanks for your answer!

1 Like