Search code examples
restgraphqlprismaprisma-graphql

PRISMA: How to receive REST API post requests (non GraphQL)?


How to create one route for receiving non graphql post requests?

I have my graphql server, and want to receive some non graphql data on it.

const server = new GraphQLServer({ ... })

server.express.get('/route', async (req, res, done) => {
  const params = req.body;
  // do some actions with ctx..
})

How can we access to ctx.db.query or ctx.db.mutation from this route? Thanks!

Related question: https://github.com/prisma/graphql-yoga/issues/482 https://www.prisma.io/forum/t/how-to-create-one-route-for-receiving-rest-api-post-requests/7239


Solution

  • You can use the same variable you passed in the context:

    const { prisma } = require('./generated/prisma-client')
    const { GraphQLServer } = require('graphql-yoga')
    
    const server = new GraphQLServer({
      typeDefs: './schema.graphql',
      resolvers,
      context: {
        prisma,
      },
    })
    
    server.express.get('/route', async (req, res, done) => {
      const params = req.body;
      const user = prisma.user({where: {id: params.id} })
    
      res.send(user)
    })