Skip to content

Commit

Permalink
0.2.0: new property headers (closes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Aug 5, 2021
1 parent 2851952 commit abd7211
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"deno.suggest.autoImports": true
}
17 changes: 14 additions & 3 deletions common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { graphql, GraphQLSchema, ExecutionResult } from 'https://deno.land/x/[email protected]/mod.ts'
import { GraphQLArgs } from 'https://deno.land/x/[email protected]/lib/graphql.d.ts'
import type { Request } from './types.ts'
export interface GraphQLOptions<Context = any, Req extends Request = Request> extends Omit<GraphQLArgs, 'source'> {

/**
* gql options
*/
export interface GQLOptions<Context = any, Req extends Request = Request> extends Omit<GraphQLArgs, 'source'> {
schema: GraphQLSchema
context?: (val: Req) => Context | Promise<Context>
/**
* GraphQL playground
*/
graphiql?: boolean
/**
* Custom headers for responses
*/
headers?: HeadersInit
}
interface Params {
variables?: Record<string, unknown>
Expand All @@ -26,7 +37,7 @@ export type GraphQLParams = QueryParams | MutationParams
/**
* Execute a GraphQL query
* @param {GraphQLParams} params
* @param {GraphQLOptions} options
* @param {GQLOptions} options
* @param context GraphQL context to use inside resolvers
*
* @example
Expand All @@ -36,7 +47,7 @@ export type GraphQLParams = QueryParams | MutationParams
*/
export async function runHttpQuery<Req extends Request = Request, Context = { request?: Req }>(
params: GraphQLParams,
options: GraphQLOptions<Context, Req>,
options: GQLOptions<Context, Req>,
context?: Context | any
): Promise<ExecutionResult> {
const contextValue = options.context && context?.request ? await options.context?.(context?.request) : context
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"entry": "./mod.ts",
"description": "Universal GraphQL HTTP middleware for Deno",
"homepage": "https://github.com/deno-libs/gql",
"version": "0.1.8",
"version": "0.2.0",
"files": ["./*.ts", "./graphiql/*.ts", "README.md"],
"ignore": ["examples", ".github", "test"],
"checkFormat": false,
Expand Down
2 changes: 1 addition & 1 deletion examples/opine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { opine, Request } from 'https://deno.land/x/opine@1.6.0/mod.ts'
import { opine, Request } from 'https://deno.land/x/opine@1.7.0/mod.ts'
import { GraphQLHTTP } from '../mod.ts'
import { makeExecutableSchema } from 'https://deno.land/x/[email protected]/mod.ts'
import { gql } from 'https://deno.land/x/[email protected]/mod.ts'
Expand Down
2 changes: 1 addition & 1 deletion examples/tinyhttp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Request } from 'https://deno.land/x/[email protected].18/mod.ts'
import { App, Request } from 'https://deno.land/x/[email protected].19/mod.ts'
import { GraphQLHTTP } from '../mod.ts'
import { makeExecutableSchema } from 'https://deno.land/x/[email protected]/mod.ts'
import { gql } from 'https://deno.land/x/[email protected]/mod.ts'
Expand Down
25 changes: 15 additions & 10 deletions http.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Request } from './types.ts'
import { runHttpQuery, GraphQLOptions } from './common.ts'
import { runHttpQuery, GQLOptions } from './common.ts'
import { readAll } from 'https://deno.land/[email protected]/io/util.ts'

const dec = new TextDecoder()

/**
* Create a new GraphQL HTTP middleware with schema, context etc
* @param {GraphQLOptions} options
* @param {GQLOptions} options
*
* @example
* ```ts
Expand All @@ -16,31 +16,35 @@ const dec = new TextDecoder()
* ```
*/
export function GraphQLHTTP<Req extends Request = Request, Ctx extends { request: Req } = { request: Req }>(
options: GraphQLOptions<Ctx, Req>
options: GQLOptions<Ctx, Req>
) {
let headers = options.headers || {}
return async (request: Req) => {
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({
body: playground,
headers: new Headers({
'Content-Type': 'text/html'
}),
body: playground
'Content-Type': 'text/html',
...headers
})
})
} else {
request.respond({
status: 400,
body: '"Accept" header value must include text/html'
body: '"Accept" header value must include text/html',
headers: new Headers(headers)
})
}
} else {
if (!['PUT', 'POST', 'PATCH'].includes(request.method)) {
return await request.respond({
status: 405,
body: 'Method Not Allowed'
body: 'Method Not Allowed',
headers: new Headers(headers)
})
} else {
const body = await readAll(request.body)
Expand All @@ -52,12 +56,13 @@ export function GraphQLHTTP<Req extends Request = Request, Ctx extends { request
body: JSON.stringify(result, null, 2),
status: 200,
headers: new Headers({
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...headers
})
})
} catch (e) {
console.error(e)
await request.respond({ status: 400, body: 'Malformed request body' })
await request.respond({ status: 400, body: 'Malformed request body', headers: new Headers(headers) })
}
}
}
Expand Down

0 comments on commit abd7211

Please sign in to comment.