Skip to content

Commit

Permalink
Add option to send credentials with each GraphQL request.
Browse files Browse the repository at this point in the history
Including credentials with each request broke with the latest version upgrade.
This has the default `sameOrigin` and can be changed to `include` or `omit`.
  • Loading branch information
alexsteinerde committed Jan 2, 2023
1 parent c1ae661 commit 304394e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Sources/GraphiQLVapor/GraphiQLVapor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ extension Request {
}

public extension Application {
func enableGraphiQL(on pathComponents: PathComponent..., method: HTTPMethod = .GET, serverPath: PathComponent = "/graphql") {
graphQLServerPath = serverPath

func enableGraphiQL(on pathComponents: PathComponent..., method: HTTPMethod = .GET, credentials: GraphiQLCredentialMode = .sameOrigin, serverPath: PathComponent = "/graphql") {
self.on(method, pathComponents) { (request) -> Response in
request.serve(html: grahphiQLHTML)
request.serve(html: grahphiQLHTML(path: serverPath, credentialMode: credentials))
}
}

func enableGraphiQL(method: HTTPMethod = .GET) {
self.enableGraphiQL(on: "", method: .GET, serverPath: "/graphql")
func enableGraphiQL(method: HTTPMethod = .GET, credentials: GraphiQLCredentialMode = .sameOrigin) {
self.enableGraphiQL(on: "", method: .GET, credentials: credentials, serverPath: "/graphql")
}
}

var graphQLServerPath: PathComponent = "/graphql"
public enum GraphiQLCredentialMode: String {
case include = "include"
case sameOrigin = "same-origin"
case omit = "omit"
}

let grahphiQLHTML = """
func grahphiQLHTML(path: PathComponent, credentialMode: GraphiQLCredentialMode) -> String {
return """
<!--
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
Expand Down Expand Up @@ -80,15 +83,15 @@ let grahphiQLHTML = """
<script>
function graphQLFetcher(graphQLParams) {
return fetch(
'\(graphQLServerPath)',
'\(path)',
{
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(graphQLParams),
credentials: 'omit',
credentials: '\(credentialMode.rawValue)',
},
).then(function (response) {
return response.json().catch(function () {
Expand All @@ -108,3 +111,4 @@ let grahphiQLHTML = """
</body>
</html>
"""
}

0 comments on commit 304394e

Please sign in to comment.