Skip to content

Commit

Permalink
fix(graphql-server): enhance type safety of RootResolvers and Options
Browse files Browse the repository at this point in the history
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 = {}>
```
  • Loading branch information
koralle committed Oct 15, 2023
1 parent d2398a4 commit cf9dbd0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/graphql-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit cf9dbd0

Please sign in to comment.