Skip to content

Commit

Permalink
feat: adding validation to verify if baseUrl is already present in th…
Browse files Browse the repository at this point in the history
…e GRAPHQL_ENDPOINT variable to prevent GrahphIql's request get error 404 Not Found (#1052)
  • Loading branch information
nandorodrigu3s committed Nov 19, 2023
1 parent f4dcc3a commit e58db21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mercurius",
"version": "13.3.0",
"version": "13.3.1",
"description": "Fastify GraphQL adapter with subscription support",
"main": "index.js",
"types": "index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ function fetcherWrapper (fetcher, cbs = []) {
}
}

/**
* Verify if the baseUrl is already present in the first part of GRAPHQL_ENDPOINT url
* to avoid unexpected duplication of paths
* @param {string} baseUrl [comes from {@link render} function]
* @returns boolean
*/
function isDuplicatedUrlArg (baseUrl) {
const checker = window.GRAPHQL_ENDPOINT.split('/')
return (checker[1] === baseUrl)
}

function render () {
const host = window.location.host
const websocketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
Expand All @@ -109,6 +120,10 @@ function render () {
if (baseUrl !== 'graphiql') {
url = `${window.location.protocol}//${host}/${baseUrl}${window.GRAPHQL_ENDPOINT}`
subscriptionUrl = `${websocketProtocol}//${host}/${baseUrl}${window.GRAPHQL_ENDPOINT}`
if (isDuplicatedUrlArg(baseUrl)) {
url = `${window.location.protocol}//${host}${window.GRAPHQL_ENDPOINT}`
subscriptionUrl = `${websocketProtocol}//${host}${window.GRAPHQL_ENDPOINT}`
}
} else {
url = `${window.location.protocol}//${host}${window.GRAPHQL_ENDPOINT}`
subscriptionUrl = `${websocketProtocol}//${host}${window.GRAPHQL_ENDPOINT}`
Expand Down

0 comments on commit e58db21

Please sign in to comment.