Skip to content

Commit

Permalink
send bad request if Accept value doesn't include type/html with `grap…
Browse files Browse the repository at this point in the history
…hiql: true`
  • Loading branch information
v1rtl committed Jul 17, 2021
1 parent 28f67f4 commit 4e2338c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
10 changes: 6 additions & 4 deletions examples/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ const typeDefs = gql`

const resolvers = {
Query: {
hello: () => `Hello World!`
hello: (_root: any, _args: any, ctx: { request: ServerRequest }) => {
return `Hello World! from ${ctx.request.url}`
}
}
}

const schema = makeExecutableSchema({ resolvers, typeDefs })

const app = new Application()

app.use((ctx) =>
GraphQLHTTP<ServerRequest>({
app.use(async (ctx) => {
return await GraphQLHTTP<ServerRequest>({
schema,
graphiql: true
})(ctx.request.originalRequest as ServerRequest)
)
})

console.log(`☁ Started on http://localhost:3000`)

Expand Down
25 changes: 16 additions & 9 deletions http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ export function GraphQLHTTP<Req extends Request = Request, Ctx extends { request
options: GraphQLOptions<Ctx, Req>
) {
return async (request: Req) => {
if (options.graphiql && request.method === 'GET' && request.headers.get('Accept')?.includes('text/html')) {
const { renderPlaygroundPage } = await import('./graphiql/render.ts')
const playground = renderPlaygroundPage({ endpoint: '/graphql' })
if (options.graphiql && request.method === 'GET') {
if (request.headers.get('Accept')?.includes('text/html')) {
const { renderPlaygroundPage } = await import('./graphiql/render.ts')
const playground = renderPlaygroundPage({ endpoint: '/graphql' })

await request.respond({
headers: new Headers({
'Content-Type': 'text/html'
}),
body: playground
})
await request.respond({
headers: new Headers({
'Content-Type': 'text/html'
}),
body: playground
})
} else {
request.respond({
status: 400,
body: '"Accept" header value must include text/html'
})
}
} else {
if (!['PUT', 'POST', 'PATCH'].includes(request.method)) {
return await request.respond({
Expand Down

0 comments on commit 4e2338c

Please sign in to comment.