Skip to content

Commit

Permalink
[v3] Fixed graphiql ignoring options if GraphiQLOptionFactory returns…
Browse files Browse the repository at this point in the history
… Promise (#2246)
  • Loading branch information
izumin5210 committed Dec 26, 2022
1 parent 9f8ca4e commit d3d0004
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eighty-crabs-stare.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/common': patch
---

Fixed graphiql ignoring options if `GraphiQLOptionFactory` returns Promise
34 changes: 34 additions & 0 deletions packages/graphql-yoga/src/plugins/useGraphiQL.spec.ts
@@ -0,0 +1,34 @@
import { createYoga } from '../server'

describe('GraphiQL', () => {
describe('when received an option factory that returns Promise', () => {
it('should respect graphiql option', async () => {
const yoga = createYoga({
graphiql: () => Promise.resolve({ title: 'Test GraphiQL' }),
})
const response = await yoga.fetch('http://localhost:3000/graphql', {
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),
})
const response = await yoga.fetch('http://localhost:3000/graphql', {
method: 'GET',
headers: {
Accept: 'text/html',
},
})
expect(response.headers.get('content-type')).not.toEqual('text/html')
expect(response.status).toEqual(406)
})
})
})
2 changes: 1 addition & 1 deletion packages/graphql-yoga/src/plugins/useGraphiQL.ts
Expand Up @@ -107,7 +107,7 @@ export function useGraphiQL<TServerContext extends Record<string, any>>(
config.graphqlEndpoint === url.pathname
) {
logger.debug(`Rendering GraphiQL`)
const graphiqlOptions = graphiqlOptionsFactory(
const graphiqlOptions = await graphiqlOptionsFactory(
request,
serverContext as TServerContext,
)
Expand Down

0 comments on commit d3d0004

Please sign in to comment.