Stack Overflow Asked by Kelly Milligan on November 24, 2021
I am working on a project that currently uses protobuf to communicate from the client to the api. I would like to do a proof of concept showing communication via graphql, and am wondering if its possible to send protobuf to apollo server instead of json.
also asked in spectrum for apollo server:
https://spectrum.chat/apollo/apollo-server/is-there-support-in-the-road-map-at-all-for-sending-protobuf-from-the-client~dbdbc639-d7c7-4d0f-9caa-b58bb3744a90
I see a few protobuf related packages,
https://www.npmjs.com/package/apollo-engine-reporting-protobuf
https://www.npmjs.com/package/@apollo/protobufjs
and am wondering if there are any plans for this or if it’s possible. asking here because i’ve received no response on the community site.
If you can use apollo-server-express
, you can achieve that as long as you can convert the protobuf to json again on the server, which I guess it is possible with protobuf.js.
apollo-server
or apollo-server-express
expects three key-pair values in the request body
operationName
variables
query
This is how request body looks like (logged using morgan-body)
If you are using apollo-server-express
, you can use your custom express middleware
in which you can deserialize the protobuf to json and add the json to the req.body
for Apollo Server.
Changing from apollo-server
to apollo-server-express
just takes a few steps and no change in your apollo server config
// import it from apollo-server-express instead of apollo-server
const { ApolloServer } = require('apollo-server-express');
const express = require('express');
const app = express();
app.use((req, res, next) => {
// Intercept the request before it reaches the Apollo Server
// Use protobuf.js to deserialize the request body into json
// Add the json to req.body
// Call the next() to go to next middleware
next();
});
const server = new ApolloServer({/* Your apollo server config */})
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () => console.log(`? Server ready at http://localhost:4000${server.graphqlPath}`));
Answered by SkrewEverything on November 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP