Comments (5)
We are planning to move away from using response.raw for helix, by exposing a Node.js compatible stream API that can be consumed by fastify. https://github.com/fastify/fastify/blob/main/lib/reply.js#L142-L147
Then you can use any fastify plugin with helix!
from graphql-helix.
import fastify from "fastify";
import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL } from "graphql-helix";
import { toResponsePayload } from "graphql-helix/to-response-payload";
import { toReadable } from "graphql-helix/node/to-readable";
import { schema } from "./schema";
const app = fastify();
app.route({
method: ["GET", "POST"],
url: "/graphql",
async handler(req, reply) {
const request = {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
};
if (shouldRenderGraphiQL(request)) {
reply.type("text/html");
reply.send(renderGraphiQL({}));
} else {
const request = {
body: req.body,
headers: req.headers,
method: req.method,
query: req.query,
};
const { operationName, query, variables } = getGraphQLParameters(request);
const result = await processRequest({
operationName,
query,
variables,
request,
schema,
});
const responsePayload = toResponsePayload(result);
reply.status(responsePayload.status);
reply.headers(responsePayload.headers);
reply.send(toReadable(responsePayload.source));
}
},
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`GraphQL server is running on port ${port}.`);
});
It is working fine for me 😇
from graphql-helix.
Apparently the fastify session plugin is not supported in this case as it bypasses fastify by using res.raw
, as being said in this issue.
from graphql-helix.
@n1ru4l Oh that's great, would love to stick with Helix!
from graphql-helix.
@n1ru4l Thank you for the example! I have just tried it and according to this comment, I changed the example a little bit to make it use Readable.from()
. It looked like a cookie was getting set. However, I immediately received an error in my console:
Cannot set headers after they are sent to the client
, which makes my Fastify server unreachable.
I tried to chain the reply function, which didn't help with that error unfortunately:
res
.headers(responsePayload.headers)
.status(responsePayload.status)
.send(Readable.from(responsePayload.source));
It looked like it worked until I refreshed the page again, which gave me the error again. Not quite sure why.
Edit: I figured out that the headers error has something to do with my custom session store which I used in combination with the fastify-session plugin. I just tried your example again without my session store and everything works fine!
from graphql-helix.
Related Issues (20)
- Avoid bundling GraphiQL HOT 3
- processRequest does not support multiple accept types HOT 2
- FastifyError: Promise may not be fulfilled with 'undefined' when statusCode is not 204 HOT 1
- Prohibiting SSE via POST breaks `graphql-sse` usage (since 1.11.0) HOT 6
- missing exports `send ... ResponseResult` from `node-http` in the barrel file
- Error: Cannot find module `<path>\node_module\graphql-helix\dist\dist.js` HOT 3
- Helix in Gateway to pass headers down to subschema? HOT 1
- graphql-ws example: Invalid query returns "Subscriptions should be sent over WebSocket" HOT 2
- Slow processRequest when error thrown HOT 1
- graphql-koa-example: subscriptions not working
- [Question] Have you thought about graphql-jit? HOT 2
- Adding `extensions` to `GraphQLParams`
- Is development dead? Zero commits since The Guild announced Yoga 2. HOT 1
- Infinite loop when using defer and async iterators (`JavaScript heap out of memory`)
- Can we detect/support cancelation of @stream operations?
- typescript compilation errors
- Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client HOT 1
- Before and after crud operation listeners
- Enabling CSP protection with koa + @koa/helmet causes 404 error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-helix.