Skip to content

Commit dde1aea

Browse files
authored
Adds more overloads to AddGraphQLOptions including additional converters (#481)
* Added more overloads to `AddGraphQLOptions` including additional converters * Removed setting `PropertyNameCaseInsensitive` to `true` as we configure `Microsoft.AspNetCore.Http.Json.JsonOptions` too * Fixed null additional converters issue
1 parent c522c60 commit dde1aea

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

Diff for: src/FSharp.Data.GraphQL.Server.AspNetCore/Serialization/JSON.fs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ let [<Literal>] UnionTag = "kind"
99
#nowarn "0058"
1010
let configureSerializerOptions (jsonFSharpOptions: JsonFSharpOptions) (additionalConverters: JsonConverter seq) (options : JsonSerializerOptions) =
1111
options.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase
12-
options.PropertyNameCaseInsensitive <- true
1312
let converters = options.Converters
1413
converters.Add (new JsonStringEnumConverter ())
1514
//converters.Add (JsonSerializerOptionsState (options)) // Dahomey.Json

Diff for: src/FSharp.Data.GraphQL.Server.AspNetCore/StartupExtensions.fs

+91-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace FSharp.Data.GraphQL.Server.AspNetCore
33
open System
44
open System.Runtime.InteropServices
55
open System.Runtime.CompilerServices
6+
open System.Text.Json.Serialization
67
open Microsoft.AspNetCore.Builder
78
open Microsoft.AspNetCore.Http
89
open Microsoft.Extensions.DependencyInjection
@@ -12,11 +13,11 @@ open FSharp.Data.GraphQL
1213
[<AutoOpen; Extension>]
1314
module ServiceCollectionExtensions =
1415

15-
let createStandardOptions executor rootFactory endpointUrl = {
16+
let createStandardOptions executor rootFactory additionalConverters endpointUrl = {
1617
SchemaExecutor = executor
1718
RootFactory = rootFactory
1819
ReadBufferSize = GraphQLOptionsDefaults.ReadBufferSize
19-
SerializerOptions = Json.getWSSerializerOptions Seq.empty
20+
SerializerOptions = Json.getWSSerializerOptions additionalConverters
2021
WebsocketOptions = {
2122
EndpointUrl = endpointUrl
2223
ConnectionInitTimeout = TimeSpan.FromMilliseconds (GraphQLOptionsDefaults.WebSocketConnectionInitTimeoutInMs)
@@ -39,26 +40,34 @@ module ServiceCollectionExtensions =
3940
/// </para>
4041
/// </summary>
4142
[<Extension; CompiledName "AddGraphQLOptions">]
42-
member services.AddGraphQLOptions<'Root>
43+
member internal services.AddGraphQLOptions<'Root>
4344
(
4445
executorFactory : Func<IServiceProvider, Executor<'Root>>,
4546
rootFactory : HttpContext -> 'Root,
47+
[<Optional>] additionalConverters : JsonConverter seq,
4648
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
4749
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
4850
) =
51+
52+
let additionalConverters =
53+
additionalConverters
54+
|> ValueOption.ofObj
55+
|> ValueOption.defaultValue Seq.empty
56+
4957
let getOptions sp =
5058
let executor = executorFactory.Invoke sp
51-
let options = createStandardOptions executor rootFactory webSocketEndpointUrl
59+
let options = createStandardOptions executor rootFactory additionalConverters webSocketEndpointUrl
5260
match configure with
5361
| null -> options
5462
| _ -> configure.Invoke options
63+
5564
services
5665
// We need this for output serialization purposes as we use <see href="IResult" />
5766
// Surprisingly minimal APIs use Microsoft.AspNetCore.Http.Json.JsonOptions
5867
// Use if you want to return HTTP responses using minmal APIs IResult interface
5968
.Configure<HttpClientJsonOptions>(
6069
Action<HttpClientJsonOptions>(fun o ->
61-
Json.configureDefaultSerializerOptions Seq.empty o.SerializerOptions
70+
Json.configureDefaultSerializerOptions additionalConverters o.SerializerOptions
6271
)
6372
)
6473
.AddSingleton<IOptionsFactory<GraphQLOptions<'Root>>>(
@@ -87,10 +96,62 @@ module ServiceCollectionExtensions =
8796
(
8897
executor : Executor<'Root>,
8998
rootFactory : HttpContext -> 'Root,
90-
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
91-
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
99+
[<Optional>] additionalConverters : JsonConverter seq
92100
) =
93-
services.AddGraphQLOptions ((fun _ -> executor), rootFactory, webSocketEndpointUrl, configure)
101+
services.AddGraphQLOptions ((fun _ -> executor), rootFactory, additionalConverters, null, null)
102+
103+
/// <summary>
104+
/// Adds GraphQL options to the service collection. Requires an executor instance to be provided.
105+
/// <para>
106+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
107+
/// to support serialization of GraphQL responses.
108+
/// </para>
109+
/// </summary>
110+
[<Extension; CompiledName "AddGraphQLOptions">]
111+
member services.AddGraphQLOptions<'Root>
112+
(
113+
executor : Executor<'Root>,
114+
rootFactory : HttpContext -> 'Root,
115+
webSocketEndpointUrl : string,
116+
[<Optional>] additionalConverters : JsonConverter seq
117+
) =
118+
services.AddGraphQLOptions ((fun _ -> executor), rootFactory, additionalConverters, webSocketEndpointUrl, null)
119+
120+
/// <summary>
121+
/// Adds GraphQL options to the service collection. Requires an executor instance to be provided.
122+
/// <para>
123+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
124+
/// to support serialization of GraphQL responses.
125+
/// </para>
126+
/// </summary>
127+
[<Extension; CompiledName "AddGraphQLOptions">]
128+
member services.AddGraphQLOptions<'Root>
129+
(
130+
executor : Executor<'Root>,
131+
rootFactory : HttpContext -> 'Root,
132+
configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>,
133+
[<Optional>] additionalConverters : JsonConverter seq
134+
) =
135+
services.AddGraphQLOptions ((fun _ -> executor), rootFactory, additionalConverters, null, configure)
136+
137+
/// <summary>
138+
/// Adds GraphQL options to the service collection. It gets the executor from the service provider.
139+
/// <para>
140+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
141+
/// to support serialization of GraphQL responses.
142+
/// </para>
143+
/// </summary>
144+
/// <remarks>
145+
/// The executor must be registered as a singleton service.
146+
/// </remarks>
147+
[<Extension; CompiledName "AddGraphQLOptions">]
148+
member services.AddGraphQLOptions<'Root>
149+
(
150+
rootFactory : HttpContext -> 'Root,
151+
[<Optional>] additionalConverters : JsonConverter seq
152+
) =
153+
let getExecutorService (sp : IServiceProvider) = sp.GetRequiredService<Executor<'Root>>()
154+
services.AddGraphQLOptions (getExecutorService, rootFactory, additionalConverters, null, null)
94155

95156
/// <summary>
96157
/// Adds GraphQL options to the service collection. It gets the executor from the service provider.
@@ -107,10 +168,30 @@ module ServiceCollectionExtensions =
107168
(
108169
rootFactory : HttpContext -> 'Root,
109170
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
110-
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
171+
[<Optional>] additionalConverters : JsonConverter seq
172+
) =
173+
let getExecutorService (sp : IServiceProvider) = sp.GetRequiredService<Executor<'Root>>()
174+
services.AddGraphQLOptions (getExecutorService, rootFactory, additionalConverters, webSocketEndpointUrl, null)
175+
176+
/// <summary>
177+
/// Adds GraphQL options to the service collection. It gets the executor from the service provider.
178+
/// <para>
179+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
180+
/// to support serialization of GraphQL responses.
181+
/// </para>
182+
/// </summary>
183+
/// <remarks>
184+
/// The executor must be registered as a singleton service.
185+
/// </remarks>
186+
[<Extension; CompiledName "AddGraphQLOptions">]
187+
member services.AddGraphQLOptions<'Root>
188+
(
189+
rootFactory : HttpContext -> 'Root,
190+
configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>,
191+
[<Optional>] additionalConverters : JsonConverter seq
111192
) =
112193
let getExecutorService (sp : IServiceProvider) = sp.GetRequiredService<Executor<'Root>>()
113-
services.AddGraphQLOptions (getExecutorService, rootFactory, webSocketEndpointUrl, configure)
194+
services.AddGraphQLOptions (getExecutorService, rootFactory, additionalConverters, null, configure)
114195

115196

116197
[<AutoOpen; Extension>]

0 commit comments

Comments
 (0)