From 989d96879566a190c58804d01888e8356dbc762c Mon Sep 17 00:00:00 2001 From: koralle <33865215+koralle@users.noreply.github.com> Date: Thu, 19 Oct 2023 07:16:11 +0900 Subject: [PATCH] fix(graphql-server): enhance type safety of RootResolvers and Options (#201) * fix(graphql-server): enhance type safety of RootResolvers and Options This commit introduces more specific type annotations to the following modules: * RootResolvers * Options Specifically, the same type parameters used in the definition of `Context` class were given to them: ```ts ``` * chore: run `changeset` --- .changeset/old-readers-complain.md | 5 +++++ packages/graphql-server/src/index.ts | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 .changeset/old-readers-complain.md diff --git a/.changeset/old-readers-complain.md b/.changeset/old-readers-complain.md new file mode 100644 index 00000000..e82b7773 --- /dev/null +++ b/.changeset/old-readers-complain.md @@ -0,0 +1,5 @@ +--- +'@hono/graphql-server': patch +--- + +add type safety to RootResolvers and Options diff --git a/packages/graphql-server/src/index.ts b/packages/graphql-server/src/index.ts index 3e3961c9..a30ea3cd 100644 --- a/packages/graphql-server/src/index.ts +++ b/packages/graphql-server/src/index.ts @@ -17,26 +17,36 @@ import type { GraphQLFormattedError, } from 'graphql' -import type { Context } from 'hono' +import type { Context, Env, Input } from 'hono' import { parseBody } from './parse-body' -export type RootResolver = (ctx?: Context) => Promise | unknown +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type RootResolver = ( + // eslint-enable-next-line @typescript-eslint/no-explicit-any + ctx?: Context +) => Promise | unknown -type Options = { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type Options = { + // eslint-enable-next-line @typescript-eslint/no-explicit-any schema: GraphQLSchema - rootResolver?: RootResolver + rootResolver?: RootResolver pretty?: boolean validationRules?: ReadonlyArray // graphiql?: boolean } -export const graphqlServer = (options: Options) => { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export const graphqlServer = ( + // eslint-enable-next-line @typescript-eslint/no-explicit-any + options: Options +) => { const schema = options.schema const pretty = options.pretty ?? false const validationRules = options.validationRules ?? [] // const showGraphiQL = options.graphiql ?? false - return async (c: Context) => { + return async (c: Context) => { // GraphQL HTTP only supports GET and POST methods. if (c.req.method !== 'GET' && c.req.method !== 'POST') { return c.json(errorMessages(['GraphQL only supports GET and POST requests.']), 405, {