GraphQL in Liferay DXP

 

Checkout the flow diagram here 

What is GraphQL ?

GraphQL is basically a query language to read or mutate data in API . In simple words its a way to call API for read and write purpose using query like syntax . Using GraphQL feels like you are querying an API service instead of a database . This was developed by Facebook .

Challenges with REST

REST API calls has lot of advantages which is well known to developers but at the same time there are few drawbacks which REST API calls doesn’t address . Lets talk about few of them

Over fetching: This is when the API endpoint provides way more information than required by the client.

Under fetching: This is when the API endpoint doesn’t provide all of the required information. So, the client has to make multiple requests (API Calls) to get everything the application needs.

When to use GraphQL ?

 

Architectural Scenarios

  • While writing code for mobile devices, smartwatches, and IOT Platforms , where bandwidth usage is critical parameter.
  • Application requirements where nested data needs to be fetched in a single call ( For example a case when we need USER details then PRODUCTS details bought by USER ).
  • A composite pattern, where an application retrieves data from multiple, different storage APIs.

 

How to use GraphQL in Liferay ?

Liferay by default comes with Out of the box List of APIs which you can access

  1. First Login as Admin
  2. Use the same browser to call the following URL
http://[host]:[port]/o/api

For example, if you’re running Liferay DXP locally on port 8080, the URL for discovering the GraphQL API is

3) Once you are on this page you can click GraphQL link on the top Right of the page

Example to Read

Here are steps to read all the blog content through GraphQL API

  1. Login as Admin
  2. Hit the URL http://localhost:8080/o/api
  3. Go to Graphql Tab
  4. write the Following query

query

{

blogPostings(filter:””,page:1,pageSize:10,search:””,siteKey:”47311″,sort:””)

{

page

items

{

id

headline

creator

{

name

}

}

}

}

Get the siteKey from Site configuration page of the site as shown in the Image 3 and replace in your query. If you have any blogs in that site then the query will give back some results else you can do the next step (To enter a blog entry via API) First and then retry this query again to get back some results . In my case since I had an entry you can see the API output on the Right hand side in the screenshot .

Image 1

Image 2

Example to Mutate (Write)

Here are steps to read all the blog content through GraphQL API

  1. Login as Admin
  2. Hit the URL http://localhost:8080/o/api
  3. Go to Graphql Tab
  4. write the Following query

mutation CreateBlog($blog: InputBlogPosting)

{

createSiteBlogPosting(blogPosting: $blog, siteKey: “47311” )

{

headline

articleBody

id

friendlyUrlPath

}

}

}

In the Query Variables you can enter the entry like following

{

“blog”:

{

“articleBody”: “Contact us for EU based Liferay Consultants at Liferayconsulting.com !”,

“headline”: “Liferayconsulting.com”

}

}

now run the query and the API call to write or mutate is done . You can now rerun the read query to find your entry appearing there .

Diagram Links

Language
Scroll to Top