Skip to content

Commit 41680d1

Browse files
authored
Fixed Oxpecker handler definitions (#494)
* Fixed Oxpecker handler definitions * Applied Oxpecker author suggestions
1 parent 3fe3bb0 commit 41680d1

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

samples/star-wars-api/Startup.fs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
namespace FSharp.Data.GraphQL.Samples.StarWarsApi
22

33
open System
4+
open System.Threading.Tasks
45
open Microsoft.AspNetCore.Builder
56
open Microsoft.AspNetCore.Http
67
open Microsoft.Extensions.Configuration
78
open Microsoft.Extensions.DependencyInjection
89
open Microsoft.Extensions.Logging
910
open Microsoft.Extensions.Hosting
10-
open Giraffe
11+
open Oxpecker
1112
open FSharp.Data.GraphQL.Server.AspNetCore
12-
open FSharp.Data.GraphQL.Server.AspNetCore.Giraffe
13+
open FSharp.Data.GraphQL.Server.AspNetCore.Oxpecker
1314

1415
type Startup private () =
1516

@@ -25,20 +26,13 @@ type Startup private () =
2526
Startup ()
2627
then this.Configuration <- configuration
2728

28-
member _.ConfigureServices (services : IServiceCollection) =
29+
member _.ConfigureServices (services : IServiceCollection) : unit =
2930
services
30-
.AddGiraffe()
31+
.AddOxpecker()
3132
.AddGraphQL<Root> (Schema.executor, rootFactory, configure = configure)
3233
|> ignore
3334

34-
member _.Configure
35-
(
36-
app : IApplicationBuilder,
37-
env : IHostEnvironment
38-
) =
39-
let errorHandler (ex : Exception) (log : ILogger) =
40-
log.LogError (EventId (), ex, "An unhandled exception has occurred while executing the request.")
41-
clearResponse >=> setStatusCode 500
35+
member _.Configure (app : IApplicationBuilder, env : IHostEnvironment) : unit =
4236

4337
if env.IsDevelopment () then
4438
app.UseGraphQLAltair "/altair" |> ignore
@@ -50,15 +44,19 @@ type Startup private () =
5044
|> ignore
5145

5246
app
53-
.UseGiraffeErrorHandler(errorHandler)
47+
//.UseGiraffeErrorHandler(errorHandler)
48+
.UseRouting()
5449
.UseWebSockets()
5550
.UseWebSocketsForGraphQL<Root>()
56-
.UseGiraffe (
57-
// Set CORS to allow external servers (React samples) to call this API
58-
setHttpHeader "Access-Control-Allow-Origin" "*"
59-
>=> setHttpHeader "Access-Control-Allow-Headers" "content-type"
60-
>=> (setHttpHeader "Request-Type" "Classic") // For integration testing purposes
61-
>=> HttpHandlers.graphQL<Root>
62-
)
51+
.UseEndpoints (fun endpoints ->
52+
// Simple declaration
53+
//endpoints.MapOxpeckerEndpoint (HttpEndpoints.graphQL<Root>("/", id))
54+
let handler =
55+
setHttpHeader "Access-Control-Allow-Origin" "*"
56+
>=> setHttpHeader "Access-Control-Allow-Headers" "content-type"
57+
>=> (setHttpHeader "Request-Type" "Classic") // For integration testing purposes
58+
>=> HttpEndpointHandlers.graphQL<Root>
59+
endpoints.MapOxpeckerEndpoint (route "/" handler))
60+
|> ignore
6361

6462
member val Configuration : IConfiguration = null with get, set

samples/star-wars-api/star-wars-api.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
<ItemGroup Label="ProjectReferences">
2727
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.AspNetCore\FSharp.Data.GraphQL.Server.AspNetCore.fsproj" />
28-
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.Giraffe\FSharp.Data.GraphQL.Server.Giraffe.fsproj" />
2928
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.Middleware\FSharp.Data.GraphQL.Server.Middleware.fsproj" />
29+
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.Oxpecker\FSharp.Data.GraphQL.Server.Oxpecker.fsproj" />
3030
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server.Relay\FSharp.Data.GraphQL.Server.Relay.fsproj" />
3131
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Server\FSharp.Data.GraphQL.Server.fsproj" />
3232
<ProjectReference Include="..\..\src\FSharp.Data.GraphQL.Shared\FSharp.Data.GraphQL.Shared.fsproj" />
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace FSharp.Data.GraphQL.Server.AspNetCore.Giraffe
1+
namespace FSharp.Data.GraphQL.Server.AspNetCore.Oxpecker
22

33
open System.Runtime.InteropServices
44
open System.Threading.Tasks
@@ -10,16 +10,19 @@ open Oxpecker
1010

1111
open FSharp.Data.GraphQL.Server.AspNetCore
1212

13-
module HttpEndpoints =
13+
module HttpEndpointHandlers =
1414

15-
let internal writeIResult2 (ctx : HttpContext) (taskRes: Task<Result<IResult, IResult>>) : Task = task {
15+
let internal writeIResult2 (ctx : HttpContext) (taskRes : Task<Result<IResult, IResult>>) : Task = task {
1616
let! result = taskRes |> TaskResult.defaultWith id
1717
do! ctx.Write result
1818
}
1919

20-
let private handleGraphQL<'Root> (ctx : HttpContext) : Task =
20+
let graphQL<'Root> (ctx : HttpContext) : Task =
2121

22-
let request = ctx.RequestServices.GetRequiredService<GraphQLRequestHandler<'Root>>()
22+
let request = ctx.RequestServices.GetRequiredService<GraphQLRequestHandler<'Root>> ()
2323
request.HandleAsync () |> writeIResult2 ctx
2424

25-
let graphQL<'Root> (route, [<Optional>] configure) : Endpoint = SimpleEndpoint(Verbs [HttpVerb.GET; HttpVerb.POST], route, handleGraphQL<'Root>, configure)
25+
module HttpEndpoints =
26+
27+
let graphQL<'Root> (route, configure) : Endpoint =
28+
SimpleEndpoint (Verbs [ HttpVerb.GET; HttpVerb.POST ], route, HttpEndpointHandlers.graphQL<'Root>, configure)

0 commit comments

Comments
 (0)