Replies: 6 comments 22 replies
-
Hi @lane711 !
EDIT: GraphQL Yoga works on Hono! See below. |
Beta Was this translation helpful? Give feedback.
-
@lane711 GraphQL Yoga works just fine even with bindings. Just create a route and feed the import { Hono } from "hono";
import { createYoga } from "graphql-yoga";
type Bindings = {
NODE_ENV: string;
LOGGING: string;
// ENVs here
};
const app = new Hono<{ Bindings: Bindings }>();
app.on(["POST", "GET"], "/whatever_path_you_want/", async (c) =>
createYoga<Bindings & ExecutionContext>({
logging: c.env.LOGGING,
// `NODE_ENV` is under `c.env`
maskedErrors: c.env.NODE_ENV == "production",
// Keep as / so you're using just the hono route
graphqlEndpoint: "/",
schema: // feed in your schema here normally
}).fetch(c.req.raw, c.env, c.executionCtx),
// Make sure it's `c.req.raw` not `c.req` since yoga expects regular `Request` object. Then proceed to pass in env and ctx
);
export default app; FYI my answer is slightly tweaked for cloudflare workers, but the gist is the same. |
Beta Was this translation helpful? Give feedback.
-
@demosjarco Hi, do you make yogo sse works with hono ? curl will block and return once
|
Beta Was this translation helpful? Give feedback.
-
Hi, i'm just trying the yoga with hono and based on this topic and the documentation i came up a basic solution (no sse). I'm here to request some advice. This is a good way to integrate yoga or i'm in a very wrong direction? import { createSchema, createYoga } from "graphql-yoga";
import { createMiddleware } from "hono/factory";
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
greetings: String
}
`,
resolvers: {
Query: {
greetings: () => "Hello from Yoga in a Bun app!",
},
},
}),
});
export const withYoga = createMiddleware(async (c, next) => {
const response = await yoga.fetch(
c.req.url,
{
method: c.req.method,
headers: c.req.header(),
body: c.req.raw.body,
},
c
);
const headersObj = Object.fromEntries(response.headers.entries());
return c.body(response.body, {
status: response.status,
headers: headersObj,
});
});
// app.js
app.use("/graphql", withYoga); The code is working. I can send requests with graphiql and looks fine, but i'm courious of your opinion about it. Thanks! |
Beta Was this translation helpful? Give feedback.
-
This is mine, add support for SSE subscription app.on(
['POST', 'GET', 'OPTIONS'],
'/graphql/*',
cors({
origin: '*',
allowMethods: ['POST', 'GET', 'OPTIONS'],
allowHeaders: ['content-type', 'authorization'],
credentials: true,
}),
requestContext,
withAuth,
async (c) => {
let req = c.req.raw;
let accept = c.req.header('accept');
// use /graphql/stream for subscription
if (accept && !accept.includes('application/json') && accept.includes('text/event-stream')) {
if (c.req.path === '/graphql' || c.req.path === '/graphql/') {
req = new Request(req.url.replace('/graphql', '/graphql/stream'), req);
}
}
// for non-node
// return yoga.fetch(c.req.raw, c.env, c.executionCtx);
return yoga.fetch(req, c.env);
},
); |
Beta Was this translation helpful? Give feedback.
-
@devmetal can u share ur better last version of integrate yoga with hono and enable ws for subscription ?? |
Beta Was this translation helpful? Give feedback.
-
Hello, I am trying to setup yoga graphql with hono, but its throwing an error when I go to /graphql:
Trace: TypeError: Cannot read properties of undefined (reading 'endsWith')
my index.ts
full code:
https://github.com/lane711/yoga/blob/feature/hono2/src/cms/graphql/graphql.ts
Beta Was this translation helpful? Give feedback.
All reactions