Skip to content

Commit

Permalink
Moved Giraffe request handlers out of the GraphQL handler
Browse files Browse the repository at this point in the history
  • Loading branch information
xperiandri committed Mar 11, 2024
1 parent a3ce3c6 commit 8e4943d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
7 changes: 5 additions & 2 deletions samples/star-wars-api/Startup.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ type Startup private () =
.UseWebSockets()
.UseWebSocketsForGraphQL<Root>()
.UseGiraffe (
HttpHandlers.graphQL<Root>
>=> (setHttpHeader "Request-Type" "Classic")
// Set CORS to allow external servers (React samples) to call this API
setHttpHeader "Access-Control-Allow-Origin" "*"
>=> setHttpHeader "Access-Control-Allow-Headers" "content-type"
>=> (setHttpHeader "Request-Type" "Classic") // For integration testing purposes
>=> HttpHandlers.graphQL<Root>
)

member val Configuration : IConfiguration = null with get, set
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ module HttpHandlers =
|> TaskResult.defaultWith id
|> ofTaskIResult ctx

/// Set CORS to allow external servers (React samples) to call this API
let setCorsHeaders : HttpHandler =
setHttpHeader "Access-Control-Allow-Origin" "*"
>=> setHttpHeader "Access-Control-Allow-Headers" "content-type"

let private handleGraphQL<'Root> (next : HttpFunc) (ctx : HttpContext) =
let sp = ctx.RequestServices

Expand Down Expand Up @@ -258,11 +253,10 @@ module HttpHandlers =

taskResult {
let executor = options.SchemaExecutor
ctx.Response.Headers.Add("Request-Type", "Classic") // For integration testing purposes
match! checkOperationType ctx with
| IntrospectionQuery optionalAstDocument -> return! executeIntrospectionQuery executor optionalAstDocument
| OperationQuery content -> return! executeOperation executor content
}
|> ofTaskIResult2 ctx

let graphQL<'Root> : HttpHandler = setCorsHeaders >=> choose [ POST; GET ] >=> handleGraphQL<'Root>
let graphQL<'Root> : HttpHandler = choose [ POST; GET ] >=> handleGraphQL<'Root>

0 comments on commit 8e4943d

Please sign in to comment.