PRISMA:如何接收REST API发布请求(非GraphQL)?

时间:2019-06-13 16:31:09

标签: rest graphql prisma prisma-graphql

如何创建一条接收非graphql发布请求的路由?

我有我的graphql服务器,并希望在其上接收一些非graphql数据。

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

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

我们如何从这条路线访问ctx.db.queryctx.db.mutation? 谢谢!

相关问题: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

1 个答案:

答案 0 :(得分:1)

您可以使用在上下文中传递的相同变量:

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)
})