Graphql-yoga: How to create one route for receiving non graphql post requests?

Created on 1 Nov 2018  Â·  10Comments  Â·  Source: dotansimha/graphql-yoga

And configure body-parser for it, to access req.body params?
So i have my graphql server, and want to receive some non graphql data on it, and get req.body.

server.express.post('/rout',bodyParser.json(), bodyParser.urlencoded({ extended: true }), function(err, req, res, next){
  const params = req.body;
})

I can accesss req.body like in the code sample. But what if i want to enable the bodyParser to several routes?

How can i access the ctx.db.query or ctx.db.mutation from this route?

statustale

Most helpful comment

@triesa @mubaidr
For me registering custom express router(s) after the graphql server has started works.

await graphQLServer.start(serverOptions);

const customRouter = Express.Router();
customRouter.use(BodyParser());
customRouter.post('/some/other/router', handler);

graphQLServer.express.use(customRouter);

All 10 comments

I have similar question also registering body-parser on root route / emits errors (probably because body parser is registered more than one times)

I need to add custom rest-api endpoints to the same app for which I need body-parser.

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

Hey :wave:, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. :slightly_smiling_face:

@triesa @mubaidr
For me registering custom express router(s) after the graphql server has started works.

await graphQLServer.start(serverOptions);

const customRouter = Express.Router();
customRouter.use(BodyParser());
customRouter.post('/some/other/router', handler);

graphQLServer.express.use(customRouter);

hi @yss14 ! Thanks for your answer,
I still dont get how you manage to have acces to ctx, in order to hit the database?
Thanks
related: https://www.prisma.io/forum/t/how-to-create-one-route-for-receiving-non-graphql-post-requests/7239

Perhaps this is going to help someone

const prismaClient = new Prisma({
      typeDefs: path.join(__dirname, './generated-schema.graphql'),
      endpoint: 'http://localhost:4466/',
});

const server = new GraphQLServer({
  typeDefs,
  resolvers,
  directiveResolvers,
  context: req => ({
    ...req,
    db: prismaClient,
  }),
})

server.express.get('/route', async (req, res, done) => {
    const params = req.body;
    // Invoke prisma from here
    let smth = await prismaClient.query.users(...);
});

Hae guys this is not working for me. Am running next js on the frontend and graphql yoga. I have tried doing this for an endpoint '/payment' but nothing is working
not-working

If it is from your browser, it should be a ‘GET‘ not. ‘POST‘.

If you want to user ‘POST‘ use postman software to simulate a ‘POST‘

Perhaps this is going to help someone

const prismaClient = new Prisma({
      typeDefs: path.join(__dirname, './generated-schema.graphql'),
      endpoint: 'http://localhost:4466/',
});

const server = new GraphQLServer({
  typeDefs,
  resolvers,
  directiveResolvers,
  context: req => ({
    ...req,
    db: prismaClient,
  }),
})

server.express.get('/route', async (req, res, done) => {
    const params = req.body;
    // Invoke prisma from here
    let smth = await prismaClient.query.users(...);
});

@peterrogov This was exactly what I was looking for! Thank you.

If it is from your browser, it should be a ‘GET‘ not. ‘POST‘.

Why not though? @alan345

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmedosama5200 picture ahmedosama5200  Â·  4Comments

woss picture woss  Â·  5Comments

playerx picture playerx  Â·  5Comments

kv-pawar picture kv-pawar  Â·  3Comments

joshhopkins picture joshhopkins  Â·  3Comments