Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(graphql-server): enhance type safety of RootResolvers and Options #201

Merged

Conversation

koralle
Copy link
Contributor

@koralle koralle commented Oct 15, 2023

related #198 (I'm not familiar with Pothos, so I'm not sure...)

Summary

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:

<E extends Env = any, P extends string = any, I extends Input = {}>

See: https://github.com/honojs/hono/blob/main/src/context.ts

Diff

To confirm, change bun_test/index.test.ts as follows (not included in the commit)

describe('graphql-server', () => {
  // Construct a schema, using GraphQL schema language
  const schema = buildSchema(`
  type Query {
    hello: String
  }
`)

  // My motivation is to development a GraphQL server on Cloudflare Workers with D1.
  type Bindings = {
    DB: string
  }
  
  type GraphQLContext = Context<{ Bindings: Bindings }, '/graphql'>

  // The root provides a resolver function for each API endpoint
  const rootValue = (ctx?: GraphQLContext) => {
    return {
      hello: () => {
        return 'Hello world!'
      },
    }
  }

  const app = new Hono()

  app.use(
    '/graphql',
    graphqlServer({
      schema,
      rootResolver: rootValue,
    })
  )

  // ...


})

Before

スクリーンショット 2023-10-15 13 18 19

スクリーンショット 2023-10-15 13 18 46

スクリーンショット 2023-10-15 13 19 22

After

スクリーンショット 2023-10-15 13 15 35

スクリーンショット 2023-10-15 13 17 00

スクリーンショット 2023-10-15 16 52 08

@changeset-bot
Copy link

changeset-bot bot commented Oct 15, 2023

🦋 Changeset detected

Latest commit: 490ad75

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/graphql-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@koralle koralle force-pushed the fix/type-parameters-to-rootresolvers-and-options branch from d244127 to 32023c1 Compare October 15, 2023 07:54
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 = {}>
```
@koralle koralle force-pushed the fix/type-parameters-to-rootresolvers-and-options branch from 32023c1 to 490ad75 Compare October 15, 2023 08:07
@yusukebe
Copy link
Member

@koralle

Thank you for the PR. Looks good! Ship it now.

@yusukebe yusukebe merged commit 989d968 into honojs:main Oct 18, 2023
1 check passed
@github-actions github-actions bot mentioned this pull request Oct 18, 2023
@koralle koralle deleted the fix/type-parameters-to-rootresolvers-and-options branch October 19, 2023 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants