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
<E extends Env = any, P extends string = any, I extends Input = {}>
```

* chore: run `changeset`
pull/202/head
koralle 2023-10-19 07:16:11 +09:00 committed by GitHub
parent d2398a4c90
commit 989d968795
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/graphql-server': patch
---
add type safety to RootResolvers and Options

View File

@ -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> | unknown
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RootResolver<E extends Env = any, P extends string = any, I extends Input = {}> = (
// eslint-enable-next-line @typescript-eslint/no-explicit-any
ctx?: Context<E, P, I>
) => Promise<unknown> | unknown
type Options = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Options<E extends Env = any, P extends string = any, I extends Input = {}> = {
// eslint-enable-next-line @typescript-eslint/no-explicit-any
schema: GraphQLSchema
rootResolver?: RootResolver
rootResolver?: RootResolver<E, P, I>
pretty?: boolean
validationRules?: ReadonlyArray<ValidationRule>
// graphiql?: boolean
}
export const graphqlServer = (options: Options) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const graphqlServer = <E extends Env = any, P extends string = any, I extends Input = {}>(
// eslint-enable-next-line @typescript-eslint/no-explicit-any
options: Options<E, P, I>
) => {
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<E, P, I>) => {
// 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, {