Skip to content

Commit

Permalink
fix(server): use file extension in import, fix graphql route register…
Browse files Browse the repository at this point in the history
…ing (redwoodjs#9984)

This PR fixes two things related to the server file and the GraphQL
function.

- we have to specify file extensions with `await import` for relative
paths; there was one missing for importing the graphql plugin
- out of the box the graphql function was registered with double slashes
since `graphiQLEndpoint` defaults to `'/graphql'` and `apiRootPath`
defaults to `'/'`, resulting in `'//graphql'`
  • Loading branch information
redwood-bot committed Feb 9, 2024
1 parent 3148e6d commit 2b40457
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/api-server/src/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function createServer(options: CreateServerOptions = {}) {
})

if (graphqlFunctionPath) {
const { redwoodFastifyGraphQLServer } = await import('./plugins/graphql')
const { redwoodFastifyGraphQLServer } = await import('./plugins/graphql.js')
// This comes from a babel plugin that's applied to api/dist/functions/graphql.{ts,js} in user projects
const { __rw_graphqlOptions } = await import(
`file://${graphqlFunctionPath}`
Expand Down
14 changes: 10 additions & 4 deletions packages/api-server/src/plugins/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ export async function redwoodFastifyGraphQLServer(
return reply
}

const graphqlEndpoint = trimSlashes(yoga.graphqlEndpoint)

const routePaths = ['', '/health', '/readiness', '/stream']
for (const routePath of routePaths) {
fastify.route({
url: `${redwoodOptions.apiRootPath}${yoga.graphqlEndpoint}${routePath}`,
url: `${redwoodOptions.apiRootPath}${graphqlEndpoint}${routePath}`,
method,
handler: (req, reply) => graphQLYogaHandler(req, reply),
})
}

fastify.addHook('onReady', (done) => {
console.info(`GraphQL Yoga Server endpoint at ${yoga.graphqlEndpoint}`)
console.info(`GraphQL Yoga Server endpoint at ${graphqlEndpoint}`)
console.info(
`GraphQL Yoga Server Health Check endpoint at ${yoga.graphqlEndpoint}/health`
`GraphQL Yoga Server Health Check endpoint at ${graphqlEndpoint}/health`
)
console.info(
`GraphQL Yoga Server Readiness endpoint at ${yoga.graphqlEndpoint}/readiness`
`GraphQL Yoga Server Readiness endpoint at ${graphqlEndpoint}/readiness`
)

done()
Expand All @@ -126,3 +128,7 @@ export async function redwoodFastifyGraphQLServer(
console.log(e)
}
}

function trimSlashes(path: string) {
return path.replace(/^\/|\/$/g, '')
}

0 comments on commit 2b40457

Please sign in to comment.