HomeDev guideAPI ReferenceGraphQL
Dev guideUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Fragments

How to create and query fragments using the FragmentBuilder class.

πŸ‘

Beta

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

You can query fragments using theFragmentBuilder class. You can create and store the fragments in classes and then use them in your query.

Add multiple fragments to a typed query:

var fragment1= new FragmentBuilder<ProxyModels.Content>("FirstFragment").Fields(x => x.ContentLink.Id, x => x.SiteId);
var fragment2= new FragmentBuilder<ProxyModels.Content>("SecondFragment").Fields(x => x.Name);

Add fragment to your typed query:

var query = _client
.ForType<Content>()
.Fields(x=>x.MetaTitle)
.Fragments(fragment1, fragment2)
.Total()
.ToQuery()
.BuildQueries();

Then the query should look like:

query Sample_Query {
  Content{
    items {
      MetaTitle
      ...FirstFragment
      ...SecondFragment
    }
    total
  }
}

fragment SecondFragment on Content {
  Name
}

fragment FirstFragment on Content {
  ContentLink{
  	Id
  }
  SiteId
}

Create nested fragments

You can also build and then add nested fragments similar to typed query fragments:

var subFragment= new FragmentBuilder<ProxyModels.Language>("SubFragment").Fields(x => x.Name, x => x.DisplayName);
var mainFragment= new FragmentBuilder<ProxyModels.Content>("MainFragment").Fields(x => x.Name).Fragments(subFragment);

var query = _client
.ForType<Content>()
.Fields(x=>x.MetaTitle)
.Fragments(mainFragment)
.Total()
.ToQuery()
.BuildQueries();

This will generate a query that looks like this:

query Sample_Query {
  Content{
    items {
      MetaTitle
      ...MainFragment
    }
    total
  }
}

fragment MainFragment on Content {
  Name
  ...SubFragment
}

fragment SubFragment on Language {
  Name
  DisplayName
}

πŸ“˜

Note

Fragment names must be unique.