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

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)
.AddFragments(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)
  .AddFragments(subFragment);

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

This generates 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.

Create fragment for a specific field

If you want to build a query where the SubFragment is inside a Language field and the MainFragment is inside a ContentExtend field like the following example:

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

fragment MainFragment on Content {
  Name
  Language{
  	...SubFragment
  }
}

fragment SubFragment on Language {
  Name
  DisplayName
}

Select the field before adding a fragment in the AddFragment method.

📘

Note

Methods do not have an 's'.

 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)
  .AddFragment(_=> _.Language, subFragment);

var query = _client
.ForType<Content>()
.Fields(x=>x.MetaTitle)
.AddFragment(_=> _.ContentExtend, mainFragment)
.Total()
.ToQuery()
.BuildQueries();

The field selection must have the same type as the fragment's type.