Skip to content

Commit

Permalink
move all types to types.ts, bump deps, remove tinyhttp example
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Jul 13, 2022
1 parent 3a1ca5c commit 65d1a2f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 89 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Universal [GraphQL](https://www.graphql.com/) HTTP middleware for Deno.
The simplest setup with `std/http`:

```ts
import { Server } from 'https://deno.land/std@0.107.0/http/server.ts'
import { Server } from 'https://deno.land/std@0.148.0/http/server.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
43 changes: 2 additions & 41 deletions common.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
import { graphql, GraphQLSchema, ExecutionResult } from 'https://deno.land/x/[email protected]/mod.ts'
import type { GraphQLArgs } from 'https://deno.land/x/[email protected]/lib/graphql.d.ts'
import type { GQLRequest } from './types.ts'
import type { RenderPageOptions } from './graphiql/render.ts'

/**
* gql options
*/
export interface GQLOptions<Context = any, Req extends GQLRequest = GQLRequest> extends Omit<GraphQLArgs, 'source'> {
schema: GraphQLSchema
context?: (val: Req) => Context | Promise<Context>
/**
* GraphQL playground
*/
graphiql?: boolean
/**
* Custom headers for responses
*/
headers?: HeadersInit
/**
* Custom options for GraphQL Playground
*/
playgroundOptions?: Omit<RenderPageOptions, 'endpoint'>
}

interface Params {
variables?: Record<string, unknown>
operationName?: string
}

interface QueryParams extends Params {
query: string
mutation?: never
}

interface MutationParams extends Params {
mutation: string
query?: never
}

export type GraphQLParams = QueryParams | MutationParams
import { graphql, ExecutionResult } from 'https://deno.land/x/[email protected]/mod.ts'
import type { GQLOptions, GQLRequest, GraphQLParams } from './types.ts'

/**
* Execute a GraphQL query
Expand Down
4 changes: 2 additions & 2 deletions examples/opine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { opine, Request as OpineRequest } from 'https://deno.land/x/opine@2.1.5/mod.ts'
import { opine, OpineRequest } from 'https://deno.land/x/opine@2.2.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'
import { readAll } from 'https://deno.land/std@0.136.0/io/mod.ts'
import { readAll } from 'https://deno.land/std@0.148.0/streams/conversion.ts'

type Request = OpineRequest & { json: () => Promise<any> }

Expand Down
30 changes: 0 additions & 30 deletions examples/tinyhttp.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server } from 'https://deno.land/std@0.136.0/http/server.ts'
import { Server } from 'https://deno.land/std@0.148.0/http/server.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
22 changes: 9 additions & 13 deletions http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runHttpQuery, GQLOptions, GraphQLParams } from './common.ts'
import type { GQLRequest } from './types.ts'
import { runHttpQuery } from './common.ts'
import type { GQLRequest, GQLOptions, GraphQLParams } from './types.ts'

/**
* Create a new GraphQL HTTP middleware with schema, context etc
Expand All @@ -19,13 +19,9 @@ export function GraphQLHTTP<Req extends GQLRequest = GQLRequest, Ctx extends { r
}: GQLOptions<Ctx, Req>) {
return async (request: Req) => {
const accept = request.headers.get('Accept') || ''

const typeList = [
'text/html',
'text/plain',
'application/json',
'*/*'
].map(contentType => ({ contentType, index: accept.indexOf(contentType) }))

const typeList = ['text/html', 'text/plain', 'application/json', '*/*']
.map((contentType) => ({ contentType, index: accept.indexOf(contentType) }))
.filter(({ index }) => index >= 0)
.sort((a, b) => a.index - b.index)
.map(({ contentType }) => contentType)
Expand All @@ -37,7 +33,7 @@ export function GraphQLHTTP<Req extends GQLRequest = GQLRequest, Ctx extends { r
}

let params: Promise<GraphQLParams>

if (request.method === 'GET') {
const urlQuery = request.url.substring(request.url.indexOf('?'))
const queryParams = new URLSearchParams(urlQuery)
Expand All @@ -52,7 +48,7 @@ export function GraphQLHTTP<Req extends GQLRequest = GQLRequest, Ctx extends { r
...headers
})
})
} else if(typeList.length === 1 && typeList[0] === 'text/html') {
} else if (typeList.length === 1 && typeList[0] === 'text/html') {
return new Response('Not Acceptable', { status: 406, headers: new Headers(headers) })
} else if (queryParams.has('query')) {
params = Promise.resolve({ query: queryParams.get('query') } as GraphQLParams)
Expand All @@ -69,8 +65,8 @@ export function GraphQLHTTP<Req extends GQLRequest = GQLRequest, Ctx extends { r
const result = await runHttpQuery<Req, Ctx>(await params, options, { request })

let contentType = 'text/plain'
if(!typeList.length || typeList.includes('application/json') || typeList.includes('*/*'))

if (!typeList.length || typeList.includes('application/json') || typeList.includes('*/*'))
contentType = 'application/json'

return new Response(JSON.stringify(result, null, 2), {
Expand Down
43 changes: 42 additions & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
import type { GraphQLArgs } from 'https://deno.land/x/[email protected]/lib/graphql.d.ts'
import type { GraphQLSchema } from 'https://deno.land/x/[email protected]/lib/type/schema.d.ts'
import type { RenderPageOptions } from './graphiql/render.ts'

/**
* gql options
*/
export interface GQLOptions<Context = any, Req extends GQLRequest = GQLRequest> extends Omit<GraphQLArgs, 'source'> {
schema: GraphQLSchema
context?: (val: Req) => Context | Promise<Context>
/**
* GraphQL playground
*/
graphiql?: boolean
/**
* Custom headers for responses
*/
headers?: HeadersInit
/**
* Custom options for GraphQL Playground
*/
playgroundOptions?: Omit<RenderPageOptions, 'endpoint'>
}

interface Params {
variables?: Record<string, unknown>
operationName?: string
}

interface QueryParams extends Params {
query: string
mutation?: never
}

interface MutationParams extends Params {
mutation: string
query?: never
}

export type GraphQLParams = QueryParams | MutationParams

export type GQLRequest = {
url: string
method: string
headers: Headers
json: () => Promise<any>
json: () => Promise<GraphQLParams>
}

0 comments on commit 65d1a2f

Please sign in to comment.