Created
March 8, 2023 16:16
-
-
Save xorus/1e63571cfb6040c876c303f8ccdd0b66 to your computer and use it in GitHub Desktop.
graphql yoga in Hono server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Env, Hono, HonoRequest} from "hono"; | |
import {createSchema, createYoga} from "graphql-yoga"; | |
const schema = createSchema({ | |
typeDefs: `...graphql schema...`, | |
resolvers: { | |
Query: { | |
hello: () => 'Hello wolrd :)', | |
}, | |
Subscription: { | |
countdown: { | |
subscribe: async function* (_, {from}) { | |
for (let i = from; i >= 0; i--) { | |
await new Promise((resolve) => setTimeout(resolve, 1000)) | |
yield {countdown: i} | |
} | |
} | |
} | |
} | |
} | |
}) | |
const yoga = createYoga({ | |
schema, | |
graphqlEndpoint: '/api/graphql', | |
}); | |
// adapted from https://github.com/hagishi/hono-graphql-yoga that was not updated | |
app.use('/api/graphql', async (c) => { | |
const ctx = Object.prototype.hasOwnProperty.call(c, 'executionCtx') ? c.executionCtx : {} | |
return yoga.handleRequest(c.req, { | |
env: c.env, | |
ctx, | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should work if you're deploying to Cloudflare Workers: