-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1rtl
committed
Aug 5, 2021
1 parent
2851952
commit abd7211
Showing
6 changed files
with
36 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.suggest.autoImports": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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) | ||
|
@@ -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) }) | ||
} | ||
} | ||
} | ||
|