GraphQL with Headless CMS (AEMaaCS)

22 / Aug / 2022 by mirza.baig 0 comments

What is Headless CMS 

CMS consist of Head and Body. Body is where the content is stored and head is where it is presented. In Headless CMS the body remains constant i.e. in our case it will be AEM but there is no head, meaning we can decide the head on our own. This does not mean that you don’t want or need a head (presentation), it’s that you get to pick and choose which head/s you send your body (content) to. It could be one or many.

Benefits of Headless CMS 

There are many benefits of using a headless CMS. If you benefit from the below points then you might want to consider using it:

  1. Head (Presentation) can be decided freely.
  2. Can use technologies which do not have easy editing of content features.
  3. Content developers are not bothered about the presentation layer and vice versa.
  4. Wider reach: You can present your content to different platforms which in return target a wider audience.
  5. Security : As content and presentation are on different platforms access to those can be well managed and segregated
  6. More revenue if you monetise your content.
  7. Allows you to build a technology stack that can easily and quickly adapt to future digital experience demands.

What is GraphQL  

GraphQL is a query and manipulation language for APIs. GraphQL is a query language and server-side runtime for APIs that prioritizes giving clients exactly the data they request and no more. 

Below are some of the basic queries:

Configuration to Enable GraphQL on AEMCaaS 

Make sure you have the below configurations done in order to consume GraphQL:

  1. Go to Tools → General → Configuration Browser → Open properties of your project. Enable content fragment models & GraphQL persistent queries.

  2. Go to Tools → General → GraphQl.
    Create a new endpoint. Give any name and select your project for which you have done the configuration in the previous step. After creation you will see the endpoint which you will be using for connections.

 

3. You also need to allow the endpoint in the CSRF Filter config. As on AEMaaCS you have to write all your configs in your code. Below is the config:

Creating Content Fragments 

GraphQL only works with content fragments in AEM. For creating one please follow the steps below:

  1. Go to Tools → Assets → Content fragments models (If you don’t have Content Fragments Models here then that means your instance is not cloud sdk. You can find it under Tools → General).

  2. Create a new model. Open the model in editor mode. You can drag the data types from the right side to the editor pane. You can select different properties like label, placeholder, default value, max length, required, unique, translatable, validation, error message, description etc.

  3. Once you are done creating your model then go to Navigation → Assets → Files → Folder of your project. Open properties of the project folder and allow your model in policies. 

  4. Create a content fragment using your content fragment model.

Testing GraphQL Queries 

You are now all set to use GraphQL to interact with AEM. But we should first test it using the GraphQL query editor. Go to Tools → General → GraphQL Query Editor (GQE). If you don’t see the option here then you need to install GraphiQL package from software center. But I would highly recommend having a cloud sdk instance because the GQE which comes with the cloud sdk provides you the option to save and access persistent queries easily. Nevertheless irrespective of what your instance is, all persistent queries are saved at your project configuration folder like:  /conf/ttn-cart-products/settings/graphql/persistentQueries

On the GQE screen on the top right side of the screen you can select the endpoint on which you want to write queries for.

If you get any error then do make sure you have performed all the steps in the configuration section above.

In order to pass query parameters you can use Query Variables section

In order to access this outside AEM you can get the curl request of the queries you create in GQE and implement them using any other technology.

References 

  • https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/headless/journeys/developer/overview.html?lang=en
  • https://www.sanity.io/blog/headless-cms-explained
  • GraphQL : https://www.youtube.com/watch?v=ZQL7tL2S0oQ

 

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *