diff --git a/README.md b/README.md index d18af94..70fedfc 100644 --- a/README.md +++ b/README.md @@ -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/graphql_tools@0.0.2/mod.ts' import { gql } from 'https://deno.land/x/graphql_tag@0.0.1/mod.ts' diff --git a/common.ts b/common.ts index cf3e508..6a31733 100644 --- a/common.ts +++ b/common.ts @@ -1,44 +1,5 @@ -import { graphql, GraphQLSchema, ExecutionResult } from 'https://deno.land/x/graphql_deno@v15.0.0/mod.ts' -import type { GraphQLArgs } from 'https://deno.land/x/graphql_deno@v15.0.0/lib/graphql.d.ts' -import type { GQLRequest } from './types.ts' -import type { RenderPageOptions } from './graphiql/render.ts' - -/** - * gql options - */ -export interface GQLOptions extends Omit { - schema: GraphQLSchema - context?: (val: Req) => Context | Promise - /** - * GraphQL playground - */ - graphiql?: boolean - /** - * Custom headers for responses - */ - headers?: HeadersInit - /** - * Custom options for GraphQL Playground - */ - playgroundOptions?: Omit -} - -interface Params { - variables?: Record - 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/graphql_deno@v15.0.0/mod.ts' +import type { GQLOptions, GQLRequest, GraphQLParams } from './types.ts' /** * Execute a GraphQL query diff --git a/examples/opine.ts b/examples/opine.ts index b9ad045..75de5b0 100644 --- a/examples/opine.ts +++ b/examples/opine.ts @@ -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/graphql_tools@0.0.2/mod.ts' import { gql } from 'https://deno.land/x/graphql_tag@0.0.1/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 } diff --git a/examples/tinyhttp.ts b/examples/tinyhttp.ts deleted file mode 100644 index 85382c8..0000000 --- a/examples/tinyhttp.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { App, Request } from 'https://deno.land/x/tinyhttp@0.1.24/mod.ts' -import { GraphQLHTTP } from '../mod.ts' -import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools@0.0.2/mod.ts' -import { gql } from 'https://deno.land/x/graphql_tag@0.0.1/mod.ts' - -const typeDefs = gql` - type Query { - hello: String - } -` - -const resolvers = { - Query: { - hello: () => `Hello World!` - } -} - -const schema = makeExecutableSchema({ resolvers, typeDefs }) - -const app = new App() - -app - .use( - '/graphql', - GraphQLHTTP({ - schema, - graphiql: true - }) - ) - .listen(3000, () => console.log(`☁ Started on http://localhost:3000`)) diff --git a/examples/vanilla.ts b/examples/vanilla.ts index ea146b1..344fbdb 100644 --- a/examples/vanilla.ts +++ b/examples/vanilla.ts @@ -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/graphql_tools@0.0.2/mod.ts' import { gql } from 'https://deno.land/x/graphql_tag@0.0.1/mod.ts' diff --git a/http.ts b/http.ts index 81ebfce..d33b4e5 100644 --- a/http.ts +++ b/http.ts @@ -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 @@ -19,13 +19,9 @@ export function GraphQLHTTP) { 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) @@ -37,7 +33,7 @@ export function GraphQLHTTP - + if (request.method === 'GET') { const urlQuery = request.url.substring(request.url.indexOf('?')) const queryParams = new URLSearchParams(urlQuery) @@ -52,7 +48,7 @@ export function GraphQLHTTP(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), { diff --git a/types.ts b/types.ts index f2af9ed..306db7e 100644 --- a/types.ts +++ b/types.ts @@ -1,6 +1,47 @@ +import type { GraphQLArgs } from 'https://deno.land/x/graphql_deno@v15.0.0/lib/graphql.d.ts' +import type { GraphQLSchema } from 'https://deno.land/x/graphql_deno@v15.0.0/lib/type/schema.d.ts' +import type { RenderPageOptions } from './graphiql/render.ts' + +/** + * gql options + */ +export interface GQLOptions extends Omit { + schema: GraphQLSchema + context?: (val: Req) => Context | Promise + /** + * GraphQL playground + */ + graphiql?: boolean + /** + * Custom headers for responses + */ + headers?: HeadersInit + /** + * Custom options for GraphQL Playground + */ + playgroundOptions?: Omit +} + +interface Params { + variables?: Record + 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 + json: () => Promise }