@@ -3,6 +3,7 @@ namespace FSharp.Data.GraphQL.Server.AspNetCore
3
3
open System
4
4
open System.Runtime .InteropServices
5
5
open System.Runtime .CompilerServices
6
+ open System.Text .Json .Serialization
6
7
open Microsoft.AspNetCore .Builder
7
8
open Microsoft.AspNetCore .Http
8
9
open Microsoft.Extensions .DependencyInjection
@@ -12,11 +13,11 @@ open FSharp.Data.GraphQL
12
13
[<AutoOpen; Extension>]
13
14
module ServiceCollectionExtensions =
14
15
15
- let createStandardOptions executor rootFactory endpointUrl = {
16
+ let createStandardOptions executor rootFactory additionalConverters endpointUrl = {
16
17
SchemaExecutor = executor
17
18
RootFactory = rootFactory
18
19
ReadBufferSize = GraphQLOptionsDefaults.ReadBufferSize
19
- SerializerOptions = Json.getWSSerializerOptions Seq.empty
20
+ SerializerOptions = Json.getWSSerializerOptions additionalConverters
20
21
WebsocketOptions = {
21
22
EndpointUrl = endpointUrl
22
23
ConnectionInitTimeout = TimeSpan.FromMilliseconds ( GraphQLOptionsDefaults.WebSocketConnectionInitTimeoutInMs)
@@ -39,26 +40,34 @@ module ServiceCollectionExtensions =
39
40
/// </para>
40
41
/// </summary>
41
42
[<Extension; CompiledName " AddGraphQLOptions" >]
42
- member services.AddGraphQLOptions < 'Root >
43
+ member internal services.AddGraphQLOptions < 'Root >
43
44
(
44
45
executorFactory : Func < IServiceProvider , Executor < 'Root >>,
45
46
rootFactory : HttpContext -> 'Root ,
47
+ [<Optional>] additionalConverters : JsonConverter seq ,
46
48
[<Optional; DefaultParameterValue ( GraphQLOptionsDefaults.WebSocketEndpoint) >] webSocketEndpointUrl : string ,
47
49
[<Optional>] configure : Func < GraphQLOptions < 'Root >, GraphQLOptions < 'Root >>
48
50
) =
51
+
52
+ let additionalConverters =
53
+ additionalConverters
54
+ |> ValueOption.ofObj
55
+ |> ValueOption.defaultValue Seq.empty
56
+
49
57
let getOptions sp =
50
58
let executor = executorFactory.Invoke sp
51
- let options = createStandardOptions executor rootFactory webSocketEndpointUrl
59
+ let options = createStandardOptions executor rootFactory additionalConverters webSocketEndpointUrl
52
60
match configure with
53
61
| null -> options
54
62
| _ -> configure.Invoke options
63
+
55
64
services
56
65
// We need this for output serialization purposes as we use <see href="IResult" />
57
66
// Surprisingly minimal APIs use Microsoft.AspNetCore.Http.Json.JsonOptions
58
67
// Use if you want to return HTTP responses using minmal APIs IResult interface
59
68
.Configure< HttpClientJsonOptions>(
60
69
Action< HttpClientJsonOptions>( fun o ->
61
- Json.configureDefaultSerializerOptions Seq.empty o.SerializerOptions
70
+ Json.configureDefaultSerializerOptions additionalConverters o.SerializerOptions
62
71
)
63
72
)
64
73
.AddSingleton< IOptionsFactory< GraphQLOptions< 'Root>>>(
@@ -87,10 +96,62 @@ module ServiceCollectionExtensions =
87
96
(
88
97
executor : Executor < 'Root >,
89
98
rootFactory : HttpContext -> 'Root ,
90
- [<Optional; DefaultParameterValue ( GraphQLOptionsDefaults.WebSocketEndpoint) >] webSocketEndpointUrl : string ,
91
- [<Optional>] configure : Func < GraphQLOptions < 'Root >, GraphQLOptions < 'Root >>
99
+ [<Optional>] additionalConverters : JsonConverter seq
92
100
) =
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 )
94
155
95
156
/// <summary>
96
157
/// Adds GraphQL options to the service collection. It gets the executor from the service provider.
@@ -107,10 +168,30 @@ module ServiceCollectionExtensions =
107
168
(
108
169
rootFactory : HttpContext -> 'Root ,
109
170
[<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
111
192
) =
112
193
let getExecutorService ( sp : IServiceProvider ) = sp.GetRequiredService< Executor< 'Root>>()
113
- services.AddGraphQLOptions ( getExecutorService, rootFactory, webSocketEndpointUrl , configure)
194
+ services.AddGraphQLOptions ( getExecutorService, rootFactory, additionalConverters , null , configure)
114
195
115
196
116
197
[<AutoOpen; Extension>]
0 commit comments