|
1 | 1 | namespace FSharp.Data.GraphQL.Samples.StarWarsApi |
2 | 2 |
|
| 3 | +open System |
| 4 | +open System.Collections.Immutable |
3 | 5 | open System.IO |
4 | | -open System.Text |
5 | | -open Giraffe |
| 6 | +open System.Text.Json |
| 7 | +open System.Text.Json.Serialization |
| 8 | +open System.Threading.Tasks |
6 | 9 | open Microsoft.AspNetCore.Http |
7 | | -open Newtonsoft.Json |
8 | | -open Newtonsoft.Json.Linq |
9 | | -open FSharp.Data.GraphQL.Execution |
| 10 | +open Microsoft.AspNetCore.Http.Json |
| 11 | +open Microsoft.Extensions.DependencyInjection |
| 12 | +open Microsoft.Extensions.Logging |
| 13 | +open Microsoft.Extensions.Options |
10 | 14 | open FSharp.Data.GraphQL |
11 | 15 | open FSharp.Data.GraphQL.Types |
| 16 | +open Giraffe |
12 | 17 |
|
13 | 18 | type HttpHandler = HttpFunc -> HttpContext -> HttpFuncResult |
14 | 19 |
|
15 | | -module HttpHandlers = |
| 20 | +type private GQLRequestContent = |
| 21 | + { Query : string |
| 22 | + OperationName : Skippable<string> |
| 23 | + Variables : Skippable<ImmutableDictionary<string, JsonElement>> } |
16 | 24 |
|
17 | | - let private converters : JsonConverter[] = [| OptionConverter () |] |
18 | | - let private jsonSettings = jsonSerializerSettings converters |
| 25 | +[<Struct>] |
| 26 | +type private OperationType = |
| 27 | + | IntrospectionQuery of introspection: string voption |
| 28 | + | OperationQuery of operation: GQLRequestContent |
19 | 29 |
|
20 | | - let internalServerError : HttpHandler = setStatusCode 500 |
| 30 | +module HttpHandlers = |
21 | 31 |
|
22 | | - let okWithStr str : HttpHandler = setStatusCode 200 >=> text str |
| 32 | + let rec private moduleType = getModuleType <@ moduleType @> |
23 | 33 |
|
| 34 | + let ofTaskIResult ctx (taskRes: Task<IResult>) : HttpFuncResult = task { |
| 35 | + let! res = taskRes |
| 36 | + do! res.ExecuteAsync(ctx) |
| 37 | + return Some ctx |
| 38 | + } |
| 39 | + |
| 40 | + /// Set CORS to allow external servers (React samples) to call this API |
24 | 41 | let setCorsHeaders : HttpHandler = |
25 | 42 | setHttpHeader "Access-Control-Allow-Origin" "*" |
26 | 43 | >=> setHttpHeader "Access-Control-Allow-Headers" "content-type" |
27 | 44 |
|
28 | | - let setContentTypeAsJson : HttpHandler = setHttpHeader "Content-Type" "application/json" |
29 | | - |
30 | | - // TODO: Rewrite completely to async code |
31 | | - let private graphQL (next : HttpFunc) (ctx : HttpContext) = task { |
32 | | - let serialize d = JsonConvert.SerializeObject (d, jsonSettings) |
33 | | - |
34 | | - let deserialize (data : string) = |
35 | | - let getMap (token : JToken) = |
36 | | - let rec mapper (name : string) (token : JToken) = |
37 | | - match name, token.Type with |
38 | | - | "variables", JTokenType.Object -> token.Children<JProperty> () |> Seq.map (fun x -> x.Name, mapper x.Name x.Value) |> Map.ofSeq |> box |
39 | | - | name , JTokenType.Array -> token |> Seq.map (fun x -> mapper name x) |> Array.ofSeq |> box |
40 | | - | _ -> (token :?> JValue).Value |
41 | | - |
42 | | - token.Children<JProperty> () |
43 | | - |> Seq.map (fun x -> x.Name, mapper x.Name x.Value) |
44 | | - |> Map.ofSeq |
45 | | - |
46 | | - if System.String.IsNullOrWhiteSpace (data) |
47 | | - then None |
48 | | - else data |> JToken.Parse |> getMap |> Some |
49 | | - |
50 | | - let json = |
51 | | - function |
52 | | - | Direct (data, _) -> |
53 | | - JsonConvert.SerializeObject (data, jsonSettings) |
54 | | - | Deferred (data, _, deferred) -> |
55 | | - deferred |> Observable.add (fun d -> printfn "Deferred: %s" (serialize d)) |
56 | | - JsonConvert.SerializeObject (data, jsonSettings) |
57 | | - | Stream data -> |
58 | | - data |> Observable.add (fun d -> printfn "Subscription data: %s" (serialize d)) |
59 | | - "{}" |
60 | | - |
61 | | - let removeWhitespacesAndLineBreaks (str : string) = str.Trim().Replace ("\r\n", " ") |
62 | | - |
63 | | - let readStream (s : Stream) = |
64 | | - use ms = new MemoryStream (4096) |
65 | | - s.CopyTo (ms) |
66 | | - ms.ToArray () |
67 | | - |
68 | | - let data = Encoding.UTF8.GetString (readStream ctx.Request.Body) |> deserialize |
69 | | - |
70 | | - let query = |
71 | | - data |
72 | | - |> Option.bind (fun data -> |
73 | | - if data.ContainsKey ("query") |
74 | | - then |
75 | | - match data.["query"] with |
76 | | - | :? string as x -> Some x |
77 | | - | _ -> failwith "Failure deserializing repsonse. Could not read query - it is not stringified in request." |
78 | | - else |
79 | | - None) |
80 | | - |
81 | | - let variables = |
82 | | - data |
83 | | - |> Option.bind (fun data -> |
84 | | - if data.ContainsKey ("variables") |
85 | | - then |
86 | | - match data.["variables"] with |
87 | | - | null -> None |
88 | | - | :? string as x -> deserialize x |
89 | | - | :? Map<string, obj> as x -> Some x |
90 | | - | _ -> failwith "Failure deserializing response. Could not read variables - it is not a object in the request." |
| 45 | + let private graphQL (next : HttpFunc) (ctx : HttpContext) = |
| 46 | + task { |
| 47 | + let logger = ctx.RequestServices.CreateLogger moduleType |
| 48 | + let jsonSerializerOptions = ctx.RequestServices.GetRequiredService<IOptions<JsonOptions>>().Value.SerializerOptions |
| 49 | + let request = ctx.Request |
| 50 | + |
| 51 | + let isGet = request.Method = HttpMethods.Get |
| 52 | + |
| 53 | + // TODO: validate the result |
| 54 | + /// Resolve response type and wrap it into an appropriate object |
| 55 | + let toResponse { DocumentId = documentId; Content = content; Metadata = metadata } = |
| 56 | + match content with |
| 57 | + | Direct (data, errs) -> |
| 58 | + logger.LogInformation($"Produced direct GraphQL response with documentId = '{{documentId}}' and metadata:{Environment.NewLine}{{metadata}}", documentId, metadata) |
| 59 | + if logger.IsEnabled LogLevel.Trace then |
| 60 | + logger.LogTrace($"GraphQL response data:{Environment.NewLine}:{{data}}", JsonSerializer.Serialize(data, jsonSerializerOptions)) |
| 61 | + GQLResponse.Direct (documentId, data, errs) |
| 62 | + |
| 63 | + | Deferred (data, errs, deferred) -> |
| 64 | + logger.LogInformation($"Produced deferred GraphQL response with documentId = '{{documentId}}' and metadata:{Environment.NewLine}{{metadata}}", documentId, metadata) |
| 65 | + if logger.IsEnabled LogLevel.Information then |
| 66 | + deferred |> Observable.add |
| 67 | + (function |
| 68 | + | DeferredResult (data, path) -> |
| 69 | + logger.LogInformation("Produced GraphQL deferred result for path: {path}", path |> Seq.map string |> Seq.toArray |> Path.Join) |
| 70 | + if logger.IsEnabled LogLevel.Trace then |
| 71 | + logger.LogTrace($"GraphQL deferred data:{Environment.NewLine}{{data}}", (JsonSerializer.Serialize(data, jsonSerializerOptions))) |
| 72 | + | DeferredErrors (null, errors, path) -> |
| 73 | + logger.LogInformation("Produced GraphQL deferred errors for path: {path}", path |> Seq.map string |> Seq.toArray |> Path.Join) |
| 74 | + if logger.IsEnabled LogLevel.Trace then |
| 75 | + logger.LogTrace($"GraphQL deferred errors:{Environment.NewLine}{{errors}}", errors) |
| 76 | + | DeferredErrors (data, errors, path) -> |
| 77 | + logger.LogInformation("Produced GraphQL deferred result with errors for path: {path}", path |> Seq.map string |> Seq.toArray |> Path.Join) |
| 78 | + if logger.IsEnabled LogLevel.Trace then |
| 79 | + logger.LogTrace($"GraphQL deferred errors:{Environment.NewLine}{{errors}}{Environment.NewLine}GraphQL deferred data:{Environment.NewLine}{{data}}", errors, (JsonSerializer.Serialize(data, jsonSerializerOptions))) |
| 80 | + ) |
| 81 | + GQLResponse.Direct (documentId, data, errs) |
| 82 | + |
| 83 | + | Stream stream -> |
| 84 | + logger.LogInformation($"Produced stream GraphQL response with documentId = '{{documentId}}' and metadata:{Environment.NewLine}{{metadata}}", documentId, metadata) |
| 85 | + if logger.IsEnabled LogLevel.Information then |
| 86 | + stream |> Observable.add |
| 87 | + (function |
| 88 | + | SubscriptionResult data -> |
| 89 | + logger.LogInformation("Produced GraphQL subscription result") |
| 90 | + if logger.IsEnabled LogLevel.Trace then |
| 91 | + logger.LogTrace($"GraphQL subscription data:{Environment.NewLine}{{data}}", (JsonSerializer.Serialize(data, jsonSerializerOptions))) |
| 92 | + | SubscriptionErrors (null, errors) -> |
| 93 | + logger.LogInformation("Produced GraphQL subscription errors") |
| 94 | + if logger.IsEnabled LogLevel.Trace then |
| 95 | + logger.LogTrace($"GraphQL subscription errors:{Environment.NewLine}{{errors}}", errors) |
| 96 | + | SubscriptionErrors (data, errors) -> |
| 97 | + logger.LogInformation("Produced GraphQL subscription result with errors") |
| 98 | + if logger.IsEnabled LogLevel.Trace then |
| 99 | + logger.LogTrace($"GraphQL subscription errors:{Environment.NewLine}{{errors}}{Environment.NewLine}GraphQL deferred data:{Environment.NewLine}{{data}}", errors, (JsonSerializer.Serialize(data, jsonSerializerOptions))) |
| 100 | + ) |
| 101 | + GQLResponse.Stream documentId |
| 102 | + |
| 103 | + | RequestError errs -> |
| 104 | + logger.LogInformation( |
| 105 | + $"Produced request error GraphQL response with documentId = '{{documentId}}' and metadata:{Environment.NewLine}{{metadata}}", |
| 106 | + documentId, |
| 107 | + metadata |
| 108 | + ) |
| 109 | + GQLResponse.RequestError (documentId, errs) |
| 110 | + |
| 111 | + let removeWhitespacesAndLineBreaks (str : string) = str.Trim().Replace ("\r\n", " ") |
| 112 | + |
| 113 | + /// Check if the request contains body or not |
| 114 | + let checkIfHasBody () = task { |
| 115 | + match request.Body.CanSeek with |
| 116 | + | true -> return (request.Body.Length > 0L) |
| 117 | + | false -> |
| 118 | + // EnableBuffering allows us to read the Body even if it's been read already somewhere else. |
| 119 | + // See https://devblogs.microsoft.com/dotnet/re-reading-asp-net-core-request-bodies-with-enablebuffering/ |
| 120 | + request.EnableBuffering() |
| 121 | + let body = request.Body |
| 122 | + let buffer = Array.zeroCreate 1 |
| 123 | + let! bytesRead = body.ReadAsync(buffer, 0, 1) |
| 124 | + body.Seek(0, SeekOrigin.Begin) |> ignore |
| 125 | + return bytesRead > 0 |
| 126 | + } |
| 127 | + |
| 128 | + let detectIntrospectionQuery () = task { |
| 129 | + /// Check for the conditions that would make this an introspection query |
| 130 | + if isGet then return IntrospectionQuery ValueNone |
91 | 131 | else |
92 | | - None) |
93 | | - |
94 | | - match query, variables with |
95 | | - | Some query, Some variables -> |
96 | | - printfn "Received query: %s" query |
97 | | - printfn "Received variables: %A" variables |
98 | | - let query = removeWhitespacesAndLineBreaks query |
99 | | - let root = { RequestId = System.Guid.NewGuid().ToString () } |
100 | | - let result = Schema.executor.AsyncExecute (query, root, variables) |> Async.RunSynchronously |
101 | | - printfn "Result metadata: %A" result.Metadata |
102 | | - return! okWithStr (json result) next ctx |
103 | | - | Some query, None -> |
104 | | - printfn "Received query: %s" query |
105 | | - let query = removeWhitespacesAndLineBreaks query |
106 | | - let result = Schema.executor.AsyncExecute (query) |> Async.RunSynchronously |
107 | | - printfn "Result metadata: %A" result.Metadata |
108 | | - return! okWithStr (json result) next ctx |
109 | | - | None, _ -> |
110 | | - let result = Schema.executor.AsyncExecute (Introspection.IntrospectionQuery) |> Async.RunSynchronously |
111 | | - printfn "Result metadata: %A" result.Metadata |
112 | | - return! okWithStr (json result) next ctx |
113 | | - } |
114 | | - |
115 | | - let webApp : HttpHandler = setCorsHeaders >=> graphQL >=> setContentTypeAsJson |
| 132 | + let! hasBody = checkIfHasBody() |
| 133 | + if not hasBody then return IntrospectionQuery ValueNone |
| 134 | + else |
| 135 | + let! request = ctx.BindJsonAsync<GQLRequestContent>() |
| 136 | + if Introspection.IntrospectionQuery.Contains request.Query |
| 137 | + then return ValueSome request.Query |> IntrospectionQuery |
| 138 | + else return OperationQuery request |
| 139 | + } |
| 140 | + |
| 141 | + /// Execute default or custom introspection query |
| 142 | + let executeIntrospectionQuery (query: string voption) = task { |
| 143 | + let! result = |
| 144 | + match query with |
| 145 | + | ValueNone -> |
| 146 | + logger.LogInformation("Executing default GraphQL introspection query") |
| 147 | + Schema.executor.AsyncExecute(Introspection.IntrospectionQuery) |
| 148 | + | ValueSome query -> |
| 149 | + logger.LogInformation($"Executing GraphQL introspection query:{Environment.NewLine}{query}", query) |
| 150 | + Schema.executor.AsyncExecute query |
| 151 | + let response = result |> toResponse |
| 152 | + return Results.Ok response |
| 153 | + } |
| 154 | + |
| 155 | + /// Execute the operation for given request |
| 156 | + let executeOperation request = task { |
| 157 | + // let! request = ctx.BindJsonAsync<GQLRequestContent>() |
| 158 | + let query = request.Query |
| 159 | + |
| 160 | + logger.LogTrace($"Executing GraphQL query:{Environment.NewLine}{query}", query) |
| 161 | + let operationName = request.OperationName |> Skippable.toOption |
| 162 | + operationName |> Option.iter (fun on -> logger.LogTrace($"GraphQL operation name: '{operationName}'", on)) |
| 163 | + |
| 164 | + let variables = request.Variables |> Skippable.toOption |
| 165 | + variables |> Option.iter (fun vars -> logger.LogTrace($"GraphQL variables:{Environment.NewLine}{variables}", variables)) |
| 166 | + |
| 167 | + let root = { RequestId = System.Guid.NewGuid() |> string } |
| 168 | + let query = removeWhitespacesAndLineBreaks query |
| 169 | + let! result = |
| 170 | + Schema.executor.AsyncExecute (query, root, ?variables = variables, ?operationName = operationName) |
| 171 | + let response = result |> toResponse |
| 172 | + return Results.Ok response |
| 173 | + } |
| 174 | + |
| 175 | + match! detectIntrospectionQuery() with |
| 176 | + | IntrospectionQuery query -> return! executeIntrospectionQuery query |
| 177 | + | OperationQuery gqlRequestContent -> return! executeOperation gqlRequestContent |
| 178 | + } |> ofTaskIResult ctx |
| 179 | + |
| 180 | + let webApp : HttpHandler = setCorsHeaders >=> choose [ POST; GET ] >=> graphQL |
0 commit comments