Inline fragments for content schema
The GraphQL querying service supports using inline fragments in querying items in the content schema. This functionality is only supported for content. This not only lets you get common fields of content but also lets you get specific fields of different content types based on content in a single query.
The GraphQL querying service also supports inline fragments in querying the Expanded field. Its type is inherited from the Content. You can query specific content types with Expanded, for example, getting values from Content Area items
The following example shows the inline fragments used in the query with content. The common fields are in this example __typename and Name. You can get the MainContentArea of the specific schema type StartPage. For a StandardPage schema, Optimizely projects ContentLink and Language.
{
Content(locale: en, limit: 100) {
items {
__typename
Name
... on StartPage {
MainContentArea {
DisplayOption
}
}
...testFragment
}
}
}
fragment testFragment on StandardPage {
ContentLink {
Id
}
Language {
Link
DisplayName
Name
}
}
Response
{
"data": {
"Content": {
"items": [
{
"__typename": "StartPage",
"Name": "Start",
"MainContentArea": [
{
"DisplayOption": ""
},
{
"DisplayOption": "narrow"
},
{
"DisplayOption": "narrow"
},
{
"DisplayOption": "narrow"
}
]
},
{
"__typename": "StandardPage",
"Name": "Reporting Made Simple",
"ContentLink": {
"Id": 13
},
"Language": {
"Link": "http://localhost:63574/en/about-us/news-events/events/reporting-made-simple/",
"DisplayName": "English",
"Name": "en"
}
},
{
"__typename": "StandardPage",
"Name": "Collaboration Made Simple",
"ContentLink": {
"Id": 14
},
"Language": {
"Link": "http://localhost:63574/en/about-us/news-events/events/collaboration-made-simple/",
"DisplayName": "English",
"Name": "en"
}
},
{
"__typename": "StandardPage",
"Name": "Risk Management",
"ContentLink": {
"Id": 15
},
"Language": {
"Link": "http://localhost:63574/en/about-us/news-events/events/risk-management-in-complex-projects/",
"DisplayName": "English",
"Name": "en"
}
},
{
"__typename": "ArticlePage",
"Name": "Alloy Saves Bears"
}
]
}
},
"extensions": {
"correlationId": "e23a2494-1db7-4799-a3ad-e6863f635d89"
}
}
The following example shows where you use inline fragments in combination with the special _children field to get the child items of the type Content belonging to StandardPage:
{
Content(locale: en) {
items {
__typename
Name
... on StartPage {
_children {
Content {
items {
__typename
RouteSegment
Url
}
}
}
}
...testFragment
}
}
}
fragment testFragment on StandardPage {
_children {
Content {
items {
__typename
Name
Changed
Created
}
}
}
}
The following example shows where you use inline fragments with the Expanded field to get the data of the EditorialBlock inside Expanded field of the StandardPage
{
StandardPage(locale: en) {
items {
Name
MainContentArea {
ContentLink {
Expanded {
... on EditorialBlock {
__typename
Name
Language {
Link
DisplayName
Name
}
}
}
}
}
}
}
}
Response
{
"data": {
"StandardPage": {
"items": [
{
"Name": "Collaboration Made Simple",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {
"__typename": "EditorialBlock",
"Name": "Collaboration made simple - When and Where",
"Language": {
"Link": null,
"DisplayName": "English",
"Name": "en"
}
}
}
}
]
},
{
"Name": "Risk Management",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {
"__typename": "EditorialBlock",
"Name": "Collaboration made simple - When and Where",
"Language": {
"Link": null,
"DisplayName": "English",
"Name": "en"
}
}
}
}
]
},
{
"Name": "Management",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {}
}
}
]
},
{
"Name": "Contact us",
"MainContentArea": null
},
{
"Name": "Become a reseller",
"MainContentArea": null
},
{
"Name": "Whitepaper",
"MainContentArea": null
},
{
"Name": "News & Events 1",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {}
}
}
]
},
{
"Name": "Find a reseller",
"MainContentArea": null
},
{
"Name": "Okela 1",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {}
}
}
]
},
{
"Name": "About us 1",
"MainContentArea": [
{
"ContentLink": {
"Expanded": {}
}
}
]
}
]
}
},
"extensions": {
"correlationId": "27d8117e-ba8b-4546-a9b0-2c57004402e2"
}
}
Updated 8 months ago