| | | 1 | | using System.Net.Http; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | using Spectre.Console; |
| | | 6 | | using Spectre.Console.Json; |
| | | 7 | | |
| | | 8 | | namespace NGql.Tool; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Sends a rendered GraphQL operation to a remote endpoint and reports the response. |
| | | 12 | | /// </summary> |
| | | 13 | | internal static class Executor |
| | | 14 | | { |
| | | 15 | | public static async Task<int> ExecuteAsync( |
| | | 16 | | string graphql, |
| | | 17 | | string endpoint, |
| | | 18 | | IReadOnlyList<KeyValuePair<string, string>> headers, |
| | | 19 | | IReadOnlyDictionary<string, object?> variables) |
| | | 20 | | { |
| | 39 | 21 | | var body = new Dictionary<string, object?> { ["query"] = graphql }; |
| | 39 | 22 | | if (variables.Count > 0) |
| | | 23 | | { |
| | 3 | 24 | | body["variables"] = variables; |
| | | 25 | | } |
| | | 26 | | |
| | 39 | 27 | | var bodyJson = JsonSerializer.Serialize(body, JsonWriteOptions); |
| | | 28 | | |
| | 39 | 29 | | var content = new StringContent(bodyJson, Encoding.UTF8, "application/json"); |
| | | 30 | | |
| | 39 | 31 | | using var http = new HttpClient(); |
| | 39 | 32 | | using var req = new HttpRequestMessage(HttpMethod.Post, endpoint) { Content = content }; |
| | | 33 | | |
| | 90 | 34 | | foreach (var (name, value) in headers) |
| | | 35 | | { |
| | | 36 | | // Try request-scoped first; fall back to content-scoped for content headers. |
| | 6 | 37 | | if (!req.Headers.TryAddWithoutValidation(name, value)) |
| | | 38 | | { |
| | 3 | 39 | | content.Headers.TryAddWithoutValidation(name, value); |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | HttpResponseMessage response; |
| | | 44 | | try |
| | | 45 | | { |
| | 39 | 46 | | response = await http.SendAsync(req); |
| | 36 | 47 | | } |
| | 3 | 48 | | catch (HttpRequestException ex) |
| | | 49 | | { |
| | 3 | 50 | | await Console.Error.WriteLineAsync($"ngql: HTTP request failed: {ex.Message}"); |
| | 3 | 51 | | return ExitCodes.HttpFailure; |
| | | 52 | | } |
| | | 53 | | |
| | 36 | 54 | | var responseBody = await response.Content.ReadAsStringAsync(); |
| | | 55 | | |
| | 36 | 56 | | WriteResponse(responseBody); |
| | | 57 | | |
| | 36 | 58 | | if (!response.IsSuccessStatusCode) |
| | | 59 | | { |
| | 3 | 60 | | await Console.Error.WriteLineAsync($"ngql: HTTP {(int)response.StatusCode} {response.ReasonPhrase}"); |
| | 3 | 61 | | return ExitCodes.HttpFailure; |
| | | 62 | | } |
| | | 63 | | |
| | 33 | 64 | | if (HasGraphQlErrors(responseBody)) |
| | | 65 | | { |
| | 3 | 66 | | return ExitCodes.GraphQlErrors; |
| | | 67 | | } |
| | | 68 | | |
| | 30 | 69 | | return ExitCodes.Ok; |
| | 39 | 70 | | } |
| | | 71 | | |
| | | 72 | | private static void WriteResponse(string responseBody) |
| | | 73 | | { |
| | | 74 | | // If the body parses as JSON, hand it to AnsiConsole as a JsonText so we get syntax |
| | | 75 | | // highlighting in interactive terminals. Spectre auto-detects redirection and falls |
| | | 76 | | // back to plain text when stdout isn't a TTY. If parsing fails (HTML error page, |
| | | 77 | | // plain text, etc.) write the raw body. |
| | | 78 | | try |
| | | 79 | | { |
| | 36 | 80 | | using var _ = JsonDocument.Parse(responseBody); |
| | 30 | 81 | | var console = AnsiConsole.Create(new AnsiConsoleSettings { Out = new AnsiConsoleOutput(Console.Out) }); |
| | 30 | 82 | | console.Write(new JsonText(responseBody)); |
| | 30 | 83 | | console.WriteLine(); |
| | 30 | 84 | | } |
| | 6 | 85 | | catch (JsonException) |
| | | 86 | | { |
| | 6 | 87 | | Console.WriteLine(responseBody); |
| | 6 | 88 | | } |
| | 36 | 89 | | } |
| | | 90 | | |
| | | 91 | | private static bool HasGraphQlErrors(string responseBody) |
| | | 92 | | { |
| | | 93 | | try |
| | | 94 | | { |
| | 33 | 95 | | using var doc = JsonDocument.Parse(responseBody); |
| | 30 | 96 | | return doc.RootElement.ValueKind == JsonValueKind.Object |
| | 30 | 97 | | && doc.RootElement.TryGetProperty("errors", out var errors) |
| | 30 | 98 | | && errors.ValueKind == JsonValueKind.Array |
| | 30 | 99 | | && errors.GetArrayLength() > 0; |
| | | 100 | | } |
| | 3 | 101 | | catch (JsonException) |
| | | 102 | | { |
| | 3 | 103 | | return false; |
| | | 104 | | } |
| | 33 | 105 | | } |
| | | 106 | | |
| | 3 | 107 | | private static readonly JsonSerializerOptions JsonWriteOptions = new() |
| | 3 | 108 | | { |
| | 3 | 109 | | WriteIndented = true, |
| | 3 | 110 | | }; |
| | | 111 | | } |