Skip to content

Commit

Permalink
Merge pull request #7 from alexsteinerde/include-credentials
Browse files Browse the repository at this point in the history
Add option to send credentials with each GraphQL request.
  • Loading branch information
alexsteinerde authored Jan 2, 2023
2 parents c1ae661 + d698ebb commit f793518
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 42 deletions.
100 changes: 68 additions & 32 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,143 +6,179 @@
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "a72c5adce3986ff6b5092ae0464a8f2675087860",
"version": "1.2.3"
"revision": "5bee16a79922e3efcb5cea06ecd27e6f8048b56b",
"version": "1.13.1"
}
},
{
"package": "async-kit",
"repositoryURL": "https://github.com/vapor/async-kit.git",
"state": {
"branch": null,
"revision": "5760c79afb8ebc24fd3251fdd9724af225fdf1f9",
"version": "1.3.0"
"revision": "929808e51fea04f01de0e911ce826ef70c4db4ea",
"version": "1.15.0"
}
},
{
"package": "console-kit",
"repositoryURL": "https://github.com/vapor/console-kit.git",
"state": {
"branch": null,
"revision": "08f36a30e0893e6a52fefbf1c2db4a6bc1288ba2",
"version": "4.2.5"
"revision": "a7e67a1719933318b5ab7eaaed355cde020465b1",
"version": "4.5.0"
}
},
{
"package": "multipart-kit",
"repositoryURL": "https://github.com/vapor/multipart-kit.git",
"state": {
"branch": null,
"revision": "73706f1883f2ba950d41f18aec7e3a53766d4a6d",
"version": "4.0.0"
"revision": "0d55c35e788451ee27222783c7d363cb88092fab",
"version": "4.5.2"
}
},
{
"package": "routing-kit",
"repositoryURL": "https://github.com/vapor/routing-kit.git",
"state": {
"branch": null,
"revision": "4cf052b78aebaf1b23f2264ce04d57b4b6eb5254",
"version": "4.2.0"
"revision": "ffac7b3a127ce1e85fb232f1a6271164628809ad",
"version": "4.6.0"
}
},
{
"package": "swift-algorithms",
"repositoryURL": "https://github.com/apple/swift-algorithms.git",
"state": {
"branch": null,
"revision": "b14b7f4c528c942f121c8b860b9410b2bf57825e",
"version": "1.0.0"
}
},
{
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
"state": {
"branch": null,
"revision": "ff3d2212b6b093db7f177d0855adbc4ef9c5f036",
"version": "1.0.3"
}
},
{
"package": "swift-backtrace",
"repositoryURL": "https://github.com/swift-server/swift-backtrace.git",
"state": {
"branch": null,
"revision": "f2fd8c4845a123419c348e0bc4b3839c414077d5",
"version": "1.2.0"
"revision": "f25620d5d05e2f1ba27154b40cafea2b67566956",
"version": "1.3.3"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version": "1.0.4"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "8f4bfa5bc1951440c15710e9e893721aa4b2765c",
"version": "1.1.3"
"revision": "92a04c10fc5ce0504f8396aac7392126033e547c",
"version": "2.2.2"
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
"version": "1.4.0"
"revision": "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version": "1.4.4"
}
},
{
"package": "swift-metrics",
"repositoryURL": "https://github.com/apple/swift-metrics.git",
"state": {
"branch": null,
"revision": "e382458581b05839a571c578e90060fff499f101",
"version": "2.1.1"
"revision": "9b39d811a83cf18b79d7d5513b06f8b290198b10",
"version": "2.3.3"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "43931b7a7daf8120a487601530c8bc03ce711992",
"version": "2.25.1"
"revision": "7e3b50b38e4e66f31db6cf4a784c6af148bac846",
"version": "2.46.0"
}
},
{
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "e5b5d191a80667a14827bfeb0ae4a511f7677942",
"version": "1.7.0"
"revision": "91dd2d61fb772e1311bb5f13b59266b579d77e42",
"version": "1.15.0"
}
},
{
"package": "swift-nio-http2",
"repositoryURL": "https://github.com/apple/swift-nio-http2.git",
"state": {
"branch": null,
"revision": "d4060ac4d056a48d946298f04968f6f6080cc618",
"version": "1.16.2"
"revision": "d6656967f33ed8b368b38e4b198631fc7c484a40",
"version": "1.23.1"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "62bf5083df970e67c886210fa5b857eacf044b7c",
"version": "2.10.2"
"revision": "4fb7ead803e38949eb1d6fabb849206a72c580f3",
"version": "2.23.0"
}
},
{
"package": "swift-nio-transport-services",
"repositoryURL": "https://github.com/apple/swift-nio-transport-services.git",
"state": {
"branch": null,
"revision": "5a352330c09a323e59ebd99afdf4ca3964c217bc",
"version": "1.9.1"
"revision": "c0d9a144cfaec8d3d596aadde3039286a266c15c",
"version": "1.15.0"
}
},
{
"package": "swift-numerics",
"repositoryURL": "https://github.com/apple/swift-numerics",
"state": {
"branch": null,
"revision": "0a5bc04095a675662cf24757cc0640aa2204253b",
"version": "1.0.2"
}
},
{
"package": "vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "bb87dcff4cf6d86d386308b4cb1393cefa4891a4",
"version": "4.39.2"
"revision": "eb2da0d749e185789970c32f7fd9c114a339fa13",
"version": "4.67.5"
}
},
{
"package": "websocket-kit",
"repositoryURL": "https://github.com/vapor/websocket-kit.git",
"state": {
"branch": null,
"revision": "2b06a70dfcfa76a2e5079f60e3ae911511f09db0",
"version": "2.1.2"
"revision": "2d9d2188a08eef4a869d368daab21b3c08510991",
"version": "2.6.1"
}
}
]
Expand Down
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 f793518

Please sign in to comment.