mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 10:51:01 +00:00
48 lines
575 B
Markdown
48 lines
575 B
Markdown
|
# GraphQL Server Middleware
|
||
|
|
||
|
## Requirements
|
||
|
|
||
|
This middleware depends on [GraphQL.js](https://www.npmjs.com/package/graphql).
|
||
|
|
||
|
```plain
|
||
|
npm i graphql
|
||
|
```
|
||
|
|
||
|
or
|
||
|
|
||
|
```plain
|
||
|
yarn add graphql
|
||
|
```
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
index.js:
|
||
|
|
||
|
```js
|
||
|
import { Hono } from 'hono'
|
||
|
import { graphqlServer } from 'hono/graphql-server'
|
||
|
import { buildSchema } from 'graphql'
|
||
|
|
||
|
export const app = new Hono()
|
||
|
|
||
|
const schema = buildSchema(`
|
||
|
type Query {
|
||
|
hello: String
|
||
|
}
|
||
|
`)
|
||
|
|
||
|
const rootValue = {
|
||
|
hello: () => 'Hello Hono!',
|
||
|
}
|
||
|
|
||
|
app.use(
|
||
|
'/graphql',
|
||
|
graphqlServer({
|
||
|
schema,
|
||
|
rootValue,
|
||
|
})
|
||
|
)
|
||
|
|
||
|
app.fire()
|
||
|
```
|