Skip to content

Commit d67eac8

Browse files
Adds contextual guidance for lack of $select. Closes #51
1 parent 0d928a6 commit d67eac8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

msgraph-developer-proxy/ChaosEngine.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ async Task OnRequest(object sender, SessionEventArgs e) {
232232

233233
// Chaos happens only for graph requests which are not OPTIONS
234234
if (method is not "OPTIONS" && e.HttpClient.Request.RequestUri.Host.Contains(_config.HostName)) {
235-
Console.WriteLine($"saw a graph request: {e.HttpClient.Request.Method} {e.HttpClient.Request.RequestUri.AbsolutePath}");
235+
Console.WriteLine($"saw a graph request: {e.HttpClient.Request.Method} {e.HttpClient.Request.RequestUriString}");
236236
HandleGraphRequest(e);
237237
}
238238
}
@@ -245,6 +245,13 @@ private void HandleGraphRequest(SessionEventArgs e) {
245245
}
246246
else {
247247
var failMode = ShouldFail(e.HttpClient.Request);
248+
249+
if (WarnNoSelect(e.HttpClient.Request)) {
250+
Console.ForegroundColor = ConsoleColor.Yellow;
251+
Console.Error.WriteLine($"\tWARNING: {BuildUseSelectMessage(e.HttpClient.Request)}");
252+
Console.ForegroundColor = _color;
253+
}
254+
248255
if (failMode == FailMode.PassThru && _config.FailureRate != 100) {
249256
Console.WriteLine($"\tPassed through {e.HttpClient.Request.RequestUri.AbsolutePath}");
250257
return;
@@ -263,6 +270,8 @@ private void HandleGraphRequest(SessionEventArgs e) {
263270

264271
private static string BuildUseSdkMessage(Request r) => $"To handle API errors more easily, use the Graph SDK. More info at {GetMoveToSdkUrl(r)}";
265272

273+
private static string BuildUseSelectMessage(Request r) => $"To improve performance of your application, use the $select parameter. More info at {GetSelectParameterGuidanceUrl(r)}";
274+
266275
private void FailResponse(SessionEventArgs e, ResponseComponents r, FailMode failMode) {
267276
if (failMode == FailMode.Throttled) {
268277
r.ErrorStatus = HttpStatusCode.TooManyRequests;
@@ -279,11 +288,19 @@ private static bool IsSdkRequest(Request request) {
279288
return request.Headers.HeaderExists("SdkVersion");
280289
}
281290

291+
private static bool WarnNoSelect(Request request) {
292+
return request.Method == "GET" && !request.Url.Contains("$select", StringComparison.OrdinalIgnoreCase);
293+
}
294+
282295
private static string GetMoveToSdkUrl(Request request) {
283296
// TODO: return language-specific guidance links based on the language detected from the User-Agent
284297
return "https://aka.ms/move-to-graph-js-sdk";
285298
}
286299

300+
private static string GetSelectParameterGuidanceUrl(Request request) {
301+
return "https://learn.microsoft.com/graph/query-parameters#select-parameter";
302+
}
303+
287304
private static void ProcessMockResponse(SessionEventArgs e, ResponseComponents responseComponents, ProxyMockResponse matchingResponse) {
288305
if (matchingResponse.ResponseCode is not null) {
289306
responseComponents.ErrorStatus = (HttpStatusCode)matchingResponse.ResponseCode;

0 commit comments

Comments
 (0)