Skip to content

Commit c63746d

Browse files
authored
Improved adding GraphQLOptions (#479)
* Improved adding `GraphQLOptions` * Simplified `AddGraphQLOptions` usage
1 parent 5db0bfa commit c63746d

File tree

9 files changed

+90
-24
lines changed

9 files changed

+90
-24
lines changed

.github/workflows/publish_ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131

3232
- name: Add version to global.json
3333
run: |
34-
$version = "7.0.406"
34+
$version = "7.0.408"
3535
$globalJsonPath = "global.json"
3636
$globalJson = Get-Content -Raw -Path $globalJsonPath | ConvertFrom-Json
3737
if ($null -eq $globalJson.sdk.version) {
@@ -45,7 +45,7 @@ jobs:
4545
- name: Install .NET Core
4646
uses: actions/setup-dotnet@v4
4747
with:
48-
dotnet-version: 7.0.406
48+
dotnet-version: 7.0.408
4949

5050
- name: Add the GitHub source
5151
run: dotnet nuget add source --username USERNAME --password ${{secrets.GITHUB_TOKEN}} --store-password-in-clear-text --name "github.com" "https://nuget.pkg.github.com/fsprojects/index.json"

.github/workflows/publish_release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333

3434
- name: Add version to global.json
3535
run: |
36-
$version = "7.0.406"
36+
$version = "7.0.408"
3737
$globalJsonPath = "global.json"
3838
$globalJson = Get-Content -Raw -Path $globalJsonPath | ConvertFrom-Json
3939
if ($null -eq $globalJson.sdk.version) {
@@ -47,7 +47,7 @@ jobs:
4747
- name: Install .NET Core
4848
uses: actions/setup-dotnet@v4
4949
with:
50-
dotnet-version: 7.0.406
50+
dotnet-version: 7.0.408
5151

5252
- name: Install local tools
5353
run: dotnet tool restore

.github/workflows/pull_request.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ jobs:
2121
fail-fast: false
2222
matrix:
2323
os: [ubuntu-latest, windows-latest, macOS-latest]
24-
dotnet: [7.0.406]
24+
dotnet: [7.0.408]
2525
runs-on: ${{ matrix.os }}
2626

2727
steps:
2828
- uses: actions/checkout@v4
2929

30-
- name: Setup .NET Core
30+
- name: Install .NET Core 7
3131
uses: actions/setup-dotnet@v4
3232
with:
3333
dotnet-version: ${{ matrix.dotnet }}
3434

35+
- name: Install .NET Core 6
36+
uses: actions/setup-dotnet@v4
37+
with:
38+
dotnet-version: "6.0.421"
39+
3540
- name: Add version to global.json
3641
run: |
3742
$version = "${{ matrix.dotnet }}"

build/Program.fs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ execContext
2525
|> Fake.Core.Context.setExecutionContext
2626

2727
module DotNetCli =
28-
let setVersion (o : DotNet.Options) = { o with Version = Some "7.0.401" }
28+
let setVersion (o : DotNet.Options) = { o with Version = Some "7.0.408" }
2929
let setRestoreOptions (o : DotNet.RestoreOptions) = o.WithCommon setVersion
3030

3131
let configurationString = Environment.environVarOrDefault "CONFIGURATION" "Release"
@@ -68,10 +68,10 @@ Target.create RestoreTarget <| fun _ ->
6868
let [<Literal>] BuildTarget = "Build"
6969
Target.create BuildTarget <| fun _ ->
7070
"FSharp.Data.GraphQL.sln"
71-
|> DotNet.build (fun o -> {
72-
o with
71+
|> DotNet.build (fun options -> {
72+
options with
7373
Configuration = configuration
74-
MSBuildParams = { o.MSBuildParams with DisableInternalBinLog = true }
74+
MSBuildParams = { options.MSBuildParams with DisableInternalBinLog = true }
7575
})
7676

7777
let startGraphQLServer (project : string) port (streamRef : DataRef<Stream>) =
@@ -104,6 +104,7 @@ let runTests (project : string) (args : string) =
104104
DotNet.build
105105
(fun options -> {
106106
options with
107+
Framework = Some DotNetMoniker
107108
Configuration = configuration
108109
MSBuildParams = { options.MSBuildParams with DisableInternalBinLog = true }
109110
})
@@ -115,8 +116,13 @@ let runTests (project : string) (args : string) =
115116
(fun options ->
116117
{
117118
options with
119+
Framework = Some DotNetMoniker
118120
Configuration = configuration
119-
MSBuildParams = { options.MSBuildParams with DisableInternalBinLog = true }
121+
MSBuildParams = {
122+
options.MSBuildParams with
123+
DisableInternalBinLog = true
124+
Verbosity = Some Normal
125+
}
120126
Common = { options.Common with CustomParams = Some customParams }
121127
}
122128
.WithCommon

samples/chat-app/server/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let main args =
2121
let builder = WebApplication.CreateBuilder (args)
2222
builder.Services
2323
.AddGiraffe()
24-
.AddGraphQLOptions<Root> (Schema.executor, rootFactory, "/ws")
24+
.AddGraphQLOptions<Root> (Schema.executor, rootFactory)
2525
|> ignore
2626

2727
let app = builder.Build ()

samples/star-wars-api/Startup.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Startup private () =
2222
member _.ConfigureServices (services : IServiceCollection) =
2323
services
2424
.AddGiraffe()
25-
.AddGraphQLOptions<Root> (Schema.executor, rootFactory, "/ws")
25+
.AddGraphQLOptions<Root> (Schema.executor, rootFactory)
2626
|> ignore
2727

2828
member _.Configure

src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLOptions.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ open Microsoft.AspNetCore.Http
88

99
type PingHandler = IServiceProvider -> JsonDocument voption -> Task<JsonDocument voption>
1010

11+
[<RequireQualifiedAccess>]
12+
module GraphQLOptionsDefaults =
13+
14+
let [<Literal>] ReadBufferSize = 4096
15+
let [<Literal>] WebSocketEndpoint = "/ws"
16+
let [<Literal>] WebSocketConnectionInitTimeoutInMs = 3000
17+
1118
type GraphQLTransportWSOptions = {
1219
EndpointUrl : string
13-
ConnectionInitTimeoutInMs : int
20+
ConnectionInitTimeout : TimeSpan
1421
CustomPingHandler : PingHandler voption
1522
}
1623

@@ -30,3 +37,4 @@ type GraphQLOptions<'Root> = {
3037
interface IGraphQLOptions with
3138
member this.SerializerOptions = this.SerializerOptions
3239
member this.WebsocketOptions = this.WebsocketOptions
40+

src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLWebsocketMiddleware.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type GraphQLWebSocketMiddleware<'Root>
3535
let serializerOptions = options.SerializerOptions
3636
let pingHandler = options.WebsocketOptions.CustomPingHandler
3737
let endpointUrl = PathString options.WebsocketOptions.EndpointUrl
38-
let connectionInitTimeout = options.WebsocketOptions.ConnectionInitTimeoutInMs
38+
let connectionInitTimeout = options.WebsocketOptions.ConnectionInitTimeout
3939

4040
let serializeServerMessage (jsonSerializerOptions : JsonSerializerOptions) (serverMessage : ServerMessage) = task {
4141
let raw =

src/FSharp.Data.GraphQL.Server.AspNetCore/StartupExtensions.fs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module ServiceCollectionExtensions =
1515
let createStandardOptions executor rootFactory endpointUrl = {
1616
SchemaExecutor = executor
1717
RootFactory = rootFactory
18-
ReadBufferSize = 4096
18+
ReadBufferSize = GraphQLOptionsDefaults.ReadBufferSize
1919
SerializerOptions = Json.serializerOptions
2020
WebsocketOptions = {
2121
EndpointUrl = endpointUrl
22-
ConnectionInitTimeoutInMs = 3000
22+
ConnectionInitTimeout = TimeSpan.FromMilliseconds (GraphQLOptionsDefaults.WebSocketConnectionInitTimeoutInMs)
2323
CustomPingHandler = ValueNone
2424
}
2525
}
@@ -31,16 +31,24 @@ module ServiceCollectionExtensions =
3131

3232
type IServiceCollection with
3333

34+
/// <summary>
35+
/// Adds GraphQL options to the service collection.
36+
/// <para>
37+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
38+
/// to support serialization of GraphQL responses.
39+
/// </para>
40+
/// </summary>
3441
[<Extension; CompiledName "AddGraphQLOptions">]
3542
member services.AddGraphQLOptions<'Root>
3643
(
37-
executor : Executor<'Root>,
44+
executorFactory : Func<IServiceProvider, Executor<'Root>>,
3845
rootFactory : HttpContext -> 'Root,
39-
endpointUrl : string,
46+
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
4047
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
4148
) =
42-
let options =
43-
let options = createStandardOptions executor rootFactory endpointUrl
49+
let getOptions sp =
50+
let executor = executorFactory.Invoke sp
51+
let options = createStandardOptions executor rootFactory webSocketEndpointUrl
4452
match configure with
4553
| null -> options
4654
| _ -> configure.Invoke options
@@ -54,9 +62,10 @@ module ServiceCollectionExtensions =
5462
)
5563
)
5664
.AddSingleton<IOptionsFactory<GraphQLOptions<'Root>>>(
57-
{ new IOptionsFactory<GraphQLOptions<'Root>> with
58-
member this.Create name = options
59-
}
65+
fun sp ->
66+
{ new IOptionsFactory<GraphQLOptions<'Root>> with
67+
member this.Create name = (getOptions sp)
68+
}
6069
)
6170
.Configure<GraphQLOptions<'Root>>(Giraffe.HttpHandlers.IndentedOptionsName, (fun o -> o.SerializerOptions.WriteIndented <- true))
6271
.AddSingleton<IOptionsFactory<IGraphQLOptions>>(fun sp ->
@@ -66,6 +75,44 @@ module ServiceCollectionExtensions =
6675
}
6776
)
6877

78+
/// <summary>
79+
/// Adds GraphQL options to the service collection. Requires an executor instance to be provided.
80+
/// <para>
81+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
82+
/// to support serialization of GraphQL responses.
83+
/// </para>
84+
/// </summary>
85+
[<Extension; CompiledName "AddGraphQLOptions">]
86+
member services.AddGraphQLOptions<'Root>
87+
(
88+
executor : Executor<'Root>,
89+
rootFactory : HttpContext -> 'Root,
90+
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
91+
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
92+
) =
93+
services.AddGraphQLOptions ((fun _ -> executor), rootFactory, webSocketEndpointUrl, configure)
94+
95+
/// <summary>
96+
/// Adds GraphQL options to the service collection. It gets the executor from the service provider.
97+
/// <para>
98+
/// It also adds converters to <see href="Microsoft.AspNetCore.Http.Json.JsonOptions" />
99+
/// to support serialization of GraphQL responses.
100+
/// </para>
101+
/// </summary>
102+
/// <remarks>
103+
/// The executor must be registered as a singleton service.
104+
/// </remarks>
105+
[<Extension; CompiledName "AddGraphQLOptions">]
106+
member services.AddGraphQLOptions<'Root>
107+
(
108+
rootFactory : HttpContext -> 'Root,
109+
[<Optional; DefaultParameterValue (GraphQLOptionsDefaults.WebSocketEndpoint)>] webSocketEndpointUrl : string,
110+
[<Optional>] configure : Func<GraphQLOptions<'Root>, GraphQLOptions<'Root>>
111+
) =
112+
let getExecutorService (sp : IServiceProvider) = sp.GetRequiredService<Executor<'Root>>()
113+
services.AddGraphQLOptions (getExecutorService, rootFactory, webSocketEndpointUrl, configure)
114+
115+
69116
[<AutoOpen; Extension>]
70117
module ApplicationBuilderExtensions =
71118

0 commit comments

Comments
 (0)