London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Loading...

Inline Fragments for Content Schema

The GraphQL querying service supports the use of Inline Fragments in querying items in the Content schema. This functionality is only supported for Content. This not only allows getting common fields of Content, but also allows getting specific fields of different content types based on ​Content in a single query.

Example:

Here the Inline Fragments are used in the query with Content. The common fields are in this example __typename and Name. We can get MainContentArea of specific schema type StartPage. For StandardPage schema, we project ContentLink and Language.

{
  Content(limit: 100) {
    items {
      __typename
      Name     
      ... on StartPage {
        MainContentArea {
          DisplayOption
        }
      }
      ...testFragment
    }
  }
}

fragment testFragment on StandardPage {
  ContentLink {
    Id
  }
  Language {
    Link
    DisplayName
    Name
  }
}

And this query has this 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"
  }
}

Another example where we use Inline Fragments in combination with the special _children field to get the child items of type Content belonging to StandardPage:

{
  Content {
    items {
      __typename
      Name     
      ... on StartPage {
        _children {
          Content {
            items {
              __typename
              RouteSegment
              Url
            }
          }
        }
      }
      ...testFragment
    }
  }
}

fragment testFragment on StandardPage {
  _children {
    Content {
      items {
        __typename
        Name
        Changed
        Created
      }
    }
  }
}

Last updated: Apr 08, 2022