HomeDev guideRecipesAPI Reference
Dev guideUser GuidesLegal TermsNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

InlineFragment

How to build an inline fragment on a type query.

👍

Beta

The Optimizely Graph .NET Client is in beta. Contact your Customer Success Manager for information.

InlineFragment

Your query type may be an abstract type, sometimes, you want to get some properties on the concrete type that implemented abstract type. You can use the InlineFragment<TSub>() method to select the fields in the concrete type.

The types returned are Content, but the fields in the response are Name and MetaTitle. If you want to get the NewsPage content, you should use the GetResultAsync and GetContent<TOriginalType,TDestinationType> methods to cast results from the Content type to the NewsPage type.

The following example gets Name from the Content abstract type and gets MetaTitle in the NewsPage type:

var query = queryBuilder
.ForType<Content>()
  .Search(“mycontent”)
  .Fields(x=>x.Name)
  .InlineFragment<NewsPage>(x=>x.MetaTitle)
.ToQuery()
.BuildQueries();

var result = await query.GetResultAsync();
var newsPage = result.GetContent<Content,NewsPage>().Hits; // the result is an IEnumerable of NewsPage.

Then the generated graphql query should looks like:

query SampleQuery {
  Content(where: { _fulltext: { match: "mycontent" } }) {
    items {
      Name
      ... on NewsPage {
        MetaTitle
      }
    }
  }
}

Recursive directive

Recursive directive is a sort way to retreive properties of a type without specify any property. It is supported on Graph Client in FragmentBuilder class. Example for build simple build:

var fragment= new FragmentBuilder<IContent>("MyFragment")
  .Fields(x => x.Name)
  .Recursive<ProxyModels.Content>(x => x.ContentLink.Expanded, 10)
  //add more recursion field with Recursive method
  ;

var query = _client
.ForType<ProxyModels.Content>()
.Fields(x=>x.Name)
.AddFragments(fragment)
.Total()
.ToQuery()
.BuildQueries();

Or just using Inline and Recursive in EPiServer.ContentGraph.Extensions

var fragment= new FragmentBuilder<IContent>("MyFragment")
  .Fields(x => x.Name)
  .Inline<ProxyModels.Content>(x => x.ContentLink.Expanded.Recursive(10));

var query = _client
.ForType<ProxyModels.Content>()
.Fields(x=>x.Name)
.AddFragments(fragment)
.Total()
.ToQuery()
.BuildQueries();

📘

Note

When using EPiServer.ContentGraph.Extensions

  • If you want an auto depth for recursive directive, set the length to -1.
  • If you wish to build multiple recursive on multiple properties, select more fields in Inline() method.

It should create a GraphQL query like the following:

query FragmentTest {
  Content {
    items {
      Name
      ...MyFragment
    }
  }
}

fragment MyFragment on IContent {
  ... on Content {
    ContentLink {
      Expanded @recursive(depth: 10)
    }
  }
}