InlineFragment
How to build an inline fragment on a type query.
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 theInlineFragment<TSub>() method to select the fields in the concrete type.
The types returned areContent, but the fields in the response areName andMetaTitle. If you want to get theNewsPage content, you should use theGetResultAsync andGetContent<TOriginalType,TDestinationType> methods to cast results from theContent type to theNewsPage type.
The following example getsName from theContent abstract type and gets MetaTitle in theNewsPage 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 inEPiServer.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();📘
NoteWhen usingEPiServer.ContentGraph.Extensions
- If you want an autodepth for recursive directive, set the length to -1.
- If you wish to build multiplerecursive on multiple properties, select more fields inInline() 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) } }}Updated 22 days ago
