Blogs > GraphQL in AEM

AEM Sites

GraphQL in AEM

Infodales Tech Solutions | February 14, 2023

Welcome all, let's begin learning GraphQL.

Introduction

Adobe Experience Manager is a headless Content Management System. The content from backend is delivered by two ways

  • Application Programming Interface

  • GraphQL

GraphQL is a query based content delivery medium that is used to deliver precise data from any structured yet flexible source like Content Fragment to third party.

GraphQL

  • Single end point.

  • Status response is always 200.

  • Precise content delivery.

  • Precise content delivery.

API

  • Multiple end points.

  • Status response is 2nn, 3nn, 4nn, 5nn

  • Complete content delivery.

  • Caching is performed by HTTP Caching Headers.

Content Fragment

It is a feature of AEM that facilitates the delivery, creation plus management of flexible and scalable content in modular and reusable approach to third party.

Creation of Content Fragment

  • Navigate to Configuration Browser from tools console check the checkboxes Cloud Configurations, Content Fragment Models and GraphQL Persistent Queries.


  • Content Fragment

  • You will find your created folder in crx/de console.


  • Content Fragment

  • From Navigation console, navigate to Assets then files and again create a folder and in cloud services add the path of our created folder.


  • Content Fragment

  • Now inside that folder create content fragment.


  • Content Fragment

  • Add fields of your choice for your model.


  • Content Fragment

  • Now from Tools console navigate to Assets then Content Fragment Models and then choose your model to create various content fragments.

  • Here in this example Content Fragment India model is created and from it Maharashtra, Andhra Pradesh, Madhya Pradesh and Rajasthan content fragments are created each specified with state, language, population and district.


  • Content Fragment

    Content Fragment

  • You will find your created content fragments in crx/de.


  • Content Fragment

  • Now your content fragments are ready.

GraphQL Implementation

  • Here we are using GraphQL in AEM 6.5 with service pack of 6.5.18.

  • In GraphQL we set up an endpoint which is resource specific in order to retrieve data from that particular resource. For that navigate to Tools then Assets and click GraphQL.


  • GraphQL

  • Create an endpoint and provide the schema of your content fragment model.


  • GraphQL

  • Now from Tools go to Generals and hit GraphQL editor.


  • GraphQL

  • You will find a console in front of you. Select your end point and now you are ready to fire queries to get your data.


  • GraphQL

  • Now in order to get all your schema available in your defined endpoint fire the provided query:

                       
                           {  __schema 
                               { types 
                                   { 
                                       name
                                       description 
                                   } 
                               }
                           }  
                       
                       
                   

  • GraphQL

  • You can retrieve your created schema either by path or by list. Here name of your schema is your model name which is the endpoint.

  • When you retrieve data by path you can select only one content fragment at a time and accordingly display the required data.

                       
                    query { indiaByPath (_path : "/content/dam/graphql-case-study/andhra-pradesh")  
                                   {item {
                                           state,
                                           language,
                                           population,
                                           districts 
                                       }
                                   } 
                               }
                       
                       
                   

    Here we are selecting Andhra Pradesh by passing its path and retrieving the data.


  • GraphQL

  • We can also get all the available content fragments if we pass the schema by list.

                       
                           query {
                               indiaList {
                                 items {
                                       _path,
                                       _variation,
                                       state,
                                       population,
                                       language,
                                       districts
                                   }
                               }
                             }
                       
                       
                   
  • GraphQL

  • Also in order to retrieve the only selected data you can add filters.

                       
                           query {
                               indiaList (filter : {
                                   state : {
                                   _expressions : {
                                       value : "Maharashtra",
                                       _operator : EQUALS_NOT
                                   }
                               }
                               }) {
                                   items {
                                   state,
                                   language,
                                   population,
                                   districts
                                   }
                               }
                               }
                       
                       
                   

    Here we are retrieving all the content fragments excluding Maharashtra.


  • GraphQL

  • Now if we want to cache our queries then just add Persisted Queries and provide a name to it.


  • GraphQL

  • This will cache your query and then you can copy the url of selected query and hit on any browser to consume the data.


  • GraphQL
    GraphQL

  • You can also publish the query directly and you will find the replication on your published instance with all your data associated with your respective end point.

  • You can easily fetch the previous queries from History and reuse them.

I hope you find this blog helpful and interesting. Please share and for more blogs surf our website.


Shruti Meshram | AEM Developer
LinkedIn Email