Skip to content

Commit

Permalink
.AspNetCore: using static logger serializer options and fixed typo:
Browse files Browse the repository at this point in the history
  "Identation" -> Indentation
  • Loading branch information
valbers committed Mar 17, 2024
1 parent 0cfd55c commit cd3b1ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/FSharp.Data.GraphQL.Server.AspNetCore/Giraffe/HttpHandlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ open FSharp.Data.GraphQL.Server.AspNetCore
type HttpHandler = HttpFunc -> HttpContext -> HttpFuncResult

module HttpHandlers =

let [<Literal>] internal IdentedOptionsName = "Idented"

let rec private moduleType = getModuleType <@ moduleType @>

let ofTaskIResult ctx (taskRes: Task<IResult>) : HttpFuncResult = task {
Expand All @@ -45,9 +42,8 @@ module HttpHandlers =

let toResponse { DocumentId = documentId; Content = content; Metadata = metadata } =

let serializeIdented value =
let jsonSerializerOptions = options.Get(IdentedOptionsName).SerializerOptions
JsonSerializer.Serialize(value, jsonSerializerOptions)
let serializeIndented value =
JsonSerializer.Serialize(value, Json.loggerSerializerOptions)

match content with
| Direct(data, errs) ->
Expand All @@ -58,7 +54,7 @@ module HttpHandlers =
)

if logger.IsEnabled LogLevel.Trace then
logger.LogTrace($"GraphQL response data:{Environment.NewLine}:{{data}}", serializeIdented data)
logger.LogTrace($"GraphQL response data:{Environment.NewLine}:{{data}}", serializeIndented data)

GQLResponse.Direct(documentId, data, errs)
| Deferred(data, errs, deferred) ->
Expand All @@ -80,7 +76,7 @@ module HttpHandlers =
if logger.IsEnabled LogLevel.Trace then
logger.LogTrace(
$"GraphQL deferred data:{Environment.NewLine}{{data}}",
serializeIdented data
serializeIndented data
)
| DeferredErrors(null, errors, path) ->
logger.LogDebug(
Expand All @@ -100,7 +96,7 @@ module HttpHandlers =
logger.LogTrace(
$"GraphQL deferred errors:{Environment.NewLine}{{errors}}{Environment.NewLine}GraphQL deferred data:{Environment.NewLine}{{data}}",
errors,
serializeIdented data
serializeIndented data
))

GQLResponse.Direct(documentId, data, errs)
Expand All @@ -120,7 +116,7 @@ module HttpHandlers =
if logger.IsEnabled LogLevel.Trace then
logger.LogTrace(
$"GraphQL subscription data:{Environment.NewLine}{{data}}",
serializeIdented data
serializeIndented data
)
| SubscriptionErrors(null, errors) ->
logger.LogDebug("Produced GraphQL subscription errors")
Expand All @@ -134,7 +130,7 @@ module HttpHandlers =
logger.LogTrace(
$"GraphQL subscription errors:{Environment.NewLine}{{errors}}{Environment.NewLine}GraphQL deferred data:{Environment.NewLine}{{data}}",
errors,
serializeIdented data
serializeIndented data
))

GQLResponse.Stream documentId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ let defaultJsonFSharpOptions =
UnionTag,
allowOverride = true)

let loggerSerializerOptions =
let options = JsonSerializerOptions()
options.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase
options.PropertyNameCaseInsensitive <- true
let converters = options.Converters
converters.Add (new JsonStringEnumConverter ())
defaultJsonFSharpOptions.AddToJsonSerializerOptions options
options

let configureDefaultSerializerOptions = configureSerializerOptions defaultJsonFSharpOptions
let configureDefaultWSSerializerOptions = configureWSSerializerOptions defaultJsonFSharpOptions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ module ServiceCollectionExtensions =
member this.Create name = options
}
)
.Configure<GraphQLOptions<'Root>>(Giraffe.HttpHandlers.IdentedOptionsName, (fun o -> o.SerializerOptions.WriteIndented <- true))
.AddSingleton<IOptionsFactory<IGraphQLOptions>>(fun sp ->
{ new IOptionsFactory<IGraphQLOptions> with
member this.Create name =
Expand Down

0 comments on commit cd3b1ce

Please sign in to comment.