close

Filter

loading table of contents...

Headless Server Developer Manual / Version 2301

Table Of Contents

10.2 JSLT Transformation

Depending on the requirements of a REST client, it may be desirable to transform the rather generic GraphQL JSON response into a custom JSON structure. You can do this, using JSLT transformations.

JSLT is a transformation language for JSON, inspired by jq, XPath, and XQuery. For more information and reference about it, please refer to the JSLT documentation.

JSLT transformation templates must be stored in this path: resources/transformations. Example transformation templates for all persisted queries are delivered:

      article.jslt
      _default.jslt
      errors.jslt
      page.jslt
      picture.jslt
      site.jslt
      search.jslt

The delivered default transformations are very basic. They simply unwrap the outer two elements of the standard GraphQL response to the pure result data. Furthermore, they showcase how to include a centralized error handling using the JSLT import directive.

A JSLT transformation file is invoked transparently using the name of the invoked persisted query. Whenever a corresponding transformation file is not found, a fallback transformation defined in default.jslt is invoked instead, if it exists. CoreMedia provides a fallback transformation template in the file _default.jslt, which simply returns the input as the output (= no transformation). To enable this fallback mechanism, rename _default.jslt to default.jslt. If the fallback template is missing, the JSLT processor is not invoked at all.

Developing more complex transformations may be time consuming as the transformations are read only once when invoked for the first time. Changes on the transformation files only take place after a restart of the Headless Server. To overcome this, the online JSLT evaluator is very useful. Just copy the original GraphQL response to the 'input' textarea and use the 'JSLT' textarea to develop any JSLT transformation and see result directly by clicking the Run! button.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.