Menu

AEM Content Fragment with GraphQL

In this post we will learn AEM content fragment creation with graph ql. In the step by step process we can create an AEM content fragment from scratch and then explore the authored content as an endpoint using GraphQL


1. Go to AEM start > Tools > General > configuration browser

GraphQL with AEM content Fragment step 1


2. Create a config under the folder project folder where you want to create configurations.

Add the title and select teh following checkboxes.

GraphQL with AEM content Fragment step 2

You have done setting your configuration folder.

3. Now, go AEM > tools > Assets > Content fragment model

GraphQL with AEM content Fragment step 3


4. Select your configuration folder, and craete a content fragment model. Open the model and add the elements and set the respective data type. We have created a model here that have one field with data type JSON. 

GraphQL with AEM content Fragment step 4
Save the changes. You have done with the creation of CF model.

5. Now, create content fragment in Assets. for that;

Goto AEM > Assets > and now select the folder where you want to create model. Here we are creating under We.Retail > en > cf directory.

GraphQL with AEM content Fragment step 5

6. We have created a content fragment with name 032025. Open this and add your data. As you an see here we have added a json object here.

GraphQL with AEM content Fragment step 6


Save the content fragment.

7. Now lets expose this content fragmnet in GraphQL. For that we will required create a graph QL endpoint. Go to AEM > Tools > Aseets> graph ql

GraphQL with AEM content Fragment step 7

Here give a name of your endpoint and select the project folder as path.
GraphQL with AEM content Fragment step 7.2


8. Now go to graphql editor to query this CF content. For that go to AEM start > Tools > General > GraphQL query editor or browse http://localhost:4502/aem/graphiql.html

GraphQL with AEM content Fragment step 8


Note: If you are using AEM 6.5 then, this option will not visible to you, and you need to install a package "content fragment with graphql". this package you can download frm software distribution portal.

GraphQL with AEM content Fragment step 8.1

 9. In GraphQL explorer, write a query and select the endpoint in top-right dropdown. This is the same endpoint which we have created in above step 7.
GraphQL with AEM content Fragment step 9
Query 

{

  sdgList { #content fragment name with List 

    items {

      sdgdata #field name from content fragment model

    }

  }

}


References:
 
Want to see a video tutorial on GraphQL and Content Fragment? refer to Adobe official site; Deliver Headless Experiences with Adobe Experience Manager | Adobe Experience Manager
Learn more about the graph ql query adn syntax from official graphql site: https://graphql.org/

No comments:

Post a Comment