Type query
How to use the type query builder and its important methods.
Optimizely Graph contains types in the schema, and you must specify which type of data you want to retrieve. TheTypeQueryBuilder class covers most of the important functionalities for querying documents of a type.
var query = queryBuilder.ForType<MyDocument>() //start to build a type query .Total() //selected field.ToQuery() //end of a type.BuildQueries(); //combine all types into GraphQL queryTheForType<T> method will create an instance ofTypeQueryBuilder for the typed query builder. TheToQuery method will mark that you have ended this typed query. These two methods are required for the beginning and ending of a typed query.
You can add the typed query to your query builder if you want to retrieve more types. An example for counting documents inMyDocument type andMyOtherType:
var query = queryBuilder.ForType<MyDocument>() //required for beginning .Total().ToQuery() //required at the end of a typed query.ForType<MyOtherType>() .Total().ToQuery().BuildQueries();TheBuildQueries method combines all typed queries and the full body of the request. It must be called before invoking theGetResultAsync() method.
Built-in functions for type query
Search()
Search()Search is a built-in query for executing full-text search on a type. The parameter is your search string. The operation for this function is aMatch operation to get the best matching result.
An example to get total items that matching for "shoes" in typeMyDocument:
var query = queryBuilder.ForType<MyDocument>().Search(“shoes”) .Total().ToQuery().BuildQueries();Full-text search with more complex filtering
An overloading method,Search(IFilterOperator), is used on a type. The parameter,IFilterOperator, lets you pass one or more operations for filtering.
An example for countingMyDocument items containing "shoes" AND starting with "sneak":
var filterOperator = new StringFilterOperators().Contains(“shoes”).StartWith(“sneak”);var query = queryBuilder.ForType<MyDocument>().Search(filterOperator) .Total().ToQuery().BuildQueries();FilterForVisitor
FilterForVisitorFilterForVisitor is a built-in filter for CMS that removes any pages that the current user does not have access to or is not currently published. The default implementation is to filter out documents that are not currently published.
An example that returns only published Article pages from CMS:
var query = queryBuilder.ForType<ArticlePage>() .FilterForVisitor() // only published docs are returned .Field(x=>x.Name).ToQuery().BuildQueries();var docs = await query.GetResultAsync<MyDocument>();You can also implement your own filter from theIFilterForVisitor interface and then inject it into the service. The Optimizely Graph .NET Client gets all instances ofIFilterForVisitor and then applies the filters.
For example, you can create a filter for visitors that returns only the pages that have the fieldSiteId set as "123-456-789":
[ServiceConfiguration(typeof(IFilterForVisitor))] //automatic injection by initialization modulepublic class RemoveSecretePageForVisitor : IFilterForVisitor public void FilterForVisitor<T>(TypeQueryBuilder<T> typeQueryBuilder) { typeQueryBuilder.Where("SiteId", new StringFilterOperators().Eq("123-456-789")); } }Then when you call theFilterForVisitor method, it will apply the filters in yourRemoveSecretePageForVisitor class.
The other overloading method lets you inject your implementations ofIFilterForVisitor into theFilterForVisitor method. It will only apply the passed filter arguments, not instances from service collection.
The following example appliesmyfilter1 andmyfilter2 but does not applyRemoveSecretePageForVisitor, even if you have initialized this class before.
var myfilter1 = //create your instance of IFilterForVisitorvar myfilter2 = //create your other instance of IFilterForVisitor var query = queryBuilder.ForType<MyDocumentType>() .Field(x=>x.Name) .FilterForVisitor(myfilter1, myFilter2).ToQuery().BuildQueries();Updated 22 days ago
