When you need to pull assets (images, video, documents, etc.) from Optimizely DAM, you often select a handful of core fields (like ID and title) by default. However, you may also need additional metadata specific to each asset type—fields not present in the base type. Optimizely DAM’s GraphQL-style fragments let you define those extra field selections in reusable blocks and splice them into your query alongside the core fields.
In this post, we’ll build an asset‐search query against Optimizely’s DAM using the Content Graph API and your _graphClient helper. We’ll:
Define three fragments—ImageFields, VideoFields, and FileFields—for extra metadata.
Before you start, make sure:
_graphClient
) configured to point at your DAM endpoint.imageFragment
, videoFragment
, and fileFragment
.Fragments encapsulate commonly‐used field selections in Optimizely DAM:
var imageFragment = new FragmentBuilder<DAMPublicImageAsset>("ImageFields").Fields(a => a.Url, a => a.AltText, a => a.Width, a => a.Height);var videoFragment = new FragmentBuilder<DAMPublicVideoAsset>("VideoFields").Fields(a => a.Url, a => a.AltText);var fileFragment = new FragmentBuilder<DAMPublicFileAsset>("FileFields").Fields(a => a.Url);* **`new FragmentBuilder<T>("Name")`** instantiates a fragment builder for the specific DAM asset subtype T (e.g. DAMPublicImageAsset) and assigns it the given fragment name.* Calling `.Fields(...)` defines exactly which properties each fragment will return.
With your fragments defined, assemble the search operation against Optimizely DAM:
var assetSearch = _graphClient// 1) Give the GraphQL operation a clear name.OperationName("SearchAssets")// 2) Target the Asset content type in Optimizely DAM.ForType<DAMAsset>()// 3) Select core asset fields.Fields(x => x.Id, x => x.Title, x => x.MimeType, x => x.DateModified)// 4) Include your reusable field sets.AddFragments(imageFragment, videoFragment, fileFragment)// 5) Create facets for UI filters (MIME types).Facet(x => x.MimeType)// 6) Request the total matching count (ignore pagination).Total();
.OperationName("SearchAssets")
Labels the underlying GraphQL operation as SearchAssets
in Optimizely’s API.
.ForType<DAMAsset>()
Indicates you’re querying the built‑in Asset
content type.
.Fields(...)
Pulls back flat properties: ID, title, MIME type, and last modified timestamp from the base class DAMAsset.
.AddFragments(...)
Embeds your ImageFields
, VideoFields
, and FileFields
fragments directly into the query to get the additional metadata fields from the respective fragments.
.Facet(...)
Generates facet buckets for MIME types, perfect for building filter panels.
.Total()
Requests a count of all matching assets so you can display “Showing X of Y results.”
This code generates a query like the following when sent to Optimizely DAM’s GraphQL endpoint:
query SearchAssets($page: Int!, $size: Int!,) {searchAssets(page: $page, size: $size) {totalCountfacets {mimeType { value, count }}items {idtitlemimeTypedateModified...ImageFields...VideoFields...FileFields}}}fragment ImageFields on Asset {urlaltTextwidthheight}fragment VideoFields on Asset {urlaltText}fragment FileFields on Asset {url}
Run the query and bind the response to your UI model:
var response = await assetSearch.ExecuteAsync();var assets = response.Items;var facets = response.Facets;var total = response.TotalCount;// Bind to your view (e.g. MVC, React, Blazor)ViewBag.Total = total;ViewBag.Facets = facets;return View("SearchResults", assets);
Leveraging GraphQL fragments with Optimizely DAM’s Query API keeps your asset searches maintainable, performant, and ready for extension. In Part 2, we’ll dive into how to manage nested data—like tags, categories, and linked assets—to further enrich your DAM queries.
Quick Links
Legal Stuff