How to pass a graphql query and other config to a node hosted altair client via http post #2446
-
I am using the altair-express-middleware package in my node server and trying to pass a graphql query to the initialQuery parameter of the altairExpress function. The idea is that my app will spawn a new browser tab with the altair client initialised with the given query. How can I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you're using the altair-express-middleware then you are using express, which allows you to use chain additional middlewares. So you can have a custom middleware in front of the altair middleware that extracts the information you need from the request and passes them to the altair middleware using res.locals (more details). Something like this (note I haven't tested this): app.use((req, res, next) => {
res.locals.query = req.data.query;
next();
}, (req, res, next) => {
return altairExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:4000/subscriptions`,
initialQuery: res.locals.query,
})(req, res, next);
}); |
Beta Was this translation helpful? Give feedback.
Actually I have realized that the correct way to do this is using altar-static instead, as below:
import { getDistDirectory, renderAltair, RenderOptions } from "altair-static";
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.static(getDistDirectory()));
app.get("/altair", (request, response) => {
let opts: RenderOptions = {
endpointURL:
${process.env.HOST}/graphql/request
,subscriptionsProtocol: "graphql-sse",
subscriptionsEndpoint:
${process.env.HOST}/graphql/stream
,initialQuery: request.query.param as string,
initialHeaders: {
"Content-Type": "application/json",
},
};
return response.send(renderAltair(opts));
});
However I've noticed that the subscripti…