The Graph.Community library now (a/o v3.32) contains an extension method to add the OData cast to the url.
The test method mocks the request for a members groups, filtered to only include directory roles:
[Fact]
public void WithODataCastUpdatesUrl()
{
// ARRANGE
var oDataCast = "microsoft.graph.directoryRole";
var expectedUrl = "/me/memberOf/microsoft.graph.directoryRole";
// ACT
using (var gsc = GraphServiceTestClient.Create())
{
var request = gsc.GraphServiceClient.Me.MemberOf.WithODataCast(oDataCast).Request();
var actualUrl = request.RequestUrl;
// ASSERT
Assert.True(actualUrl.IndexOf(expectedUrl) > -1, "RequestUrl does not include odata cast value");
}
}
The library is on Nuget: https://www.nuget.org/packages/Graph.Community/