Skip to content

Commit

Permalink
do not return 404 when using query params or trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
nescalante committed Jun 26, 2023
1 parent 2e103b4 commit 185d470
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/graphql-yoga/src/plugins/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Yoga Plugins', () => {
plugins: [testPluginToAdd],
schema,
})
const response = await yoga.fetch('http://localhost:3000/graphql', {
const response = await yoga.fetch('http://localhost:3000/graphql/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
18 changes: 18 additions & 0 deletions packages/graphql-yoga/src/plugins/use-graphiql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ describe('GraphiQL', () => {
expect(result).toMatch(/<title>Test GraphiQL<\/title>/)
})

it('renders graphiql when passing query params and trailing slash', async () => {
const yoga = createYoga({
graphiql: () => Promise.resolve({ title: 'Test GraphiQL' }),
})
const response = await yoga.fetch(
'http://localhost:3000/graphql/?query=something+awesome',
{
method: 'GET',
headers: {
Accept: 'text/html',
},
},
)
expect(response.headers.get('content-type')).toEqual('text/html')
const result = await response.text()
expect(result).toMatch(/<title>Test GraphiQL<\/title>/)
})

it('returns error when graphiql is disabled', async () => {
const yoga = createYoga({
graphiql: () => Promise.resolve(false),
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-yoga/src/plugins/use-graphiql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import graphiqlHTML from '../graphiql-html.js'
import { YogaLogger } from '@graphql-yoga/logger'
import { FetchAPI } from '../types.js'
import { Plugin } from './types.js'
import { isGraphqlEndpoint } from '../utils/url.js'

export function shouldRenderGraphiQL({ headers, method }: Request): boolean {
return method === 'GET' && !!headers?.get('accept')?.includes('text/html')
Expand Down Expand Up @@ -100,7 +101,7 @@ export function useGraphiQL<TServerContext extends Record<string, any>>(
async onRequest({ request, serverContext, fetchAPI, endResponse, url }) {
if (
shouldRenderGraphiQL(request) &&
(request.url.endsWith(config.graphqlEndpoint) ||
(isGraphqlEndpoint(request.url, config.graphqlEndpoint) ||
url.pathname === config.graphqlEndpoint ||
getUrlPattern(fetchAPI).test(url))
) {
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-yoga/src/plugins/use-unhandled-route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import landingPageBody from '../landing-page-html.js'
import { isGraphqlEndpoint } from '../utils/url.js'
import { FetchAPI } from '../types.js'
import type { Plugin } from './types.js'

Expand All @@ -16,7 +17,7 @@ export function useUnhandledRoute(args: {
return {
onRequest({ request, fetchAPI, endResponse, url }) {
if (
!request.url.endsWith(args.graphqlEndpoint) &&
!isGraphqlEndpoint(request.url, args.graphqlEndpoint) &&
url.pathname !== args.graphqlEndpoint &&
!getUrlPattern(fetchAPI).test(url)
) {
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql-yoga/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function isGraphqlEndpoint(url: string, graphqlEndpoint: string) {
const urlWithoutQuery = url.split('?')[0]
const normalizedUrl = urlWithoutQuery.replace(/\/$/, '')

return normalizedUrl.endsWith(graphqlEndpoint)
}

0 comments on commit 185d470

Please sign in to comment.