SDL Tridion Sites 9 – GraphQL

SDL released Tridion Sites 9 in Nov 2018, and the idea of using GraphQL for Public Content API (PCA) for their SDL-Tridion-DX framework came in picture, which enabled SDL to support Content Mashup and that was a wonderful step.

The story behind GraphQL is related to facebook. Back in 2011, with the growing craze of mobile users, for their mobile app, facebook needed a mechanism which can retrieve raw data for users profile from different sources, they called it newsfeed API (superGraph) then. In Feb 2012 this newsfeed API became reality and eventually became GraphQL today. GraphQL was created by Dan Schefer and was made public in July 2015.

GraphQL is a query language for APIs. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time.

In nutshell, GraphQL facilitates you with the flexibility to request only those data you needed.

For example –

If for a Tridion Page, I just need its Title, I will request below and the response it gives will only have that Title:

graphql-page-title

Similarly, If I want Title, tcmId, and Url :

graphql-page-title-tcm-url

 

Having said that, GraphQL goes from above simple query to mesh of complex query (further) and all those are driven by the need you have. So you get only what you asked. Now, before I get into futher GraphQL queries, few things we should know about how GraphQL can be accessed or used in SDL Sites 9.

First, I hope everyones remember – http://xx.xxx.xx.xx:8081/content.svc, with Sites 9, you will see a new API Url – http://xx.xxx.xx.xx:8081/cd/api.

contnet-svc-url

This is the new PCA. You can configure the API url ‘cd/api’ in –

<Tridion Package>\Content Delivery\roles\content\standalone\config\application.properties

at - 
# Content API properties
content.api.endpoint=/cd/api

 

Second, GraphQL request uses JSON-like query format. Unlike content service, GraphQL uses namespaceIds to differenciate between different content source from where data will be fetched.

For example:

  • namespeceId: 1 denotes data to be fetched from Tridion and
  • namespaceId: 2 denotes data to be fetched ftom Docs and in future many more.

This is the beauty of GraphQL – One query, multiple source (definetly connected), one response.

Third, there are two ways to connect to GraphQL – with OAuth, without OAuth.

With OAuth

By default, Content Delivery installs microservices with OAuth enabled. In that case, you need to do below after successfull Installation:

1. Use any SOAP UI tool which can leverage you OAuth requests with a bearer token – I am using Postman here. Postman v7.7.2 onwards supports GraphQL in Beta

 

2. Collect Bearer token from – http://xx.xxx.xx.xx:8082/token.svc

postman-graphql-token

 

3. Use this Bearer token to access http://xx.xxx.xx.xx:8081/cd/api

postman-graphql-1

4. Go to  “Body” and choose GraphQL Beta and make POST request

postman-graphql-2

Without OAuth

If you want to access PCA without OAuth, you need to do below –

1. Disable OAuth in Microservices /config/cd_ambient_conf.xml. Mark below as false

<Security OAuthEnabled="false">" 
<Rules Enabled="false"/>

 

2. Access the http://xx.xxx.xx.xx:8081/cd/api in Browser, If you get below response then you are good to go –

cd-api-browser

3. Install GraphiQL (you can  use Chrome Extension as well) and make your query

Below example shows a complex query but you can start with a simple query as simple as fetching a title for a page.

graphql-complex-query

 

You can Read more about Public Content API on SLD Docs and try many other ways of accessing data using GraphQL.

Happy leraning!!

If you still need help, Please post your question on Tridion Stack Exchange (Trex).

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.