简体   繁体   English

使用Prisma GraphQL-Yoga作为Express / Node应用程序:如何在服务器端调用GraphQL突变?

[英]Using Prisma GraphQL-Yoga as an Express/Node application: how can I call GraphQL mutations server-side?

I'm trying to do image processing on Node and am using a Prisma GraphQL-Yoga server to manage the queue of jobs. 我正在尝试在Node上进行图像处理,并且正在使用Prisma GraphQL-Yoga服务器来管理作业队列。 A React/Apollo front end that queues the jobs, that is, it calls a Mutation on the server which stores the jobs in the DB via Prisma. React / Apollo前端将作业排队,也就是说,它在服务器上调用Mutation,该服务器通过Prisma将作业存储在DB中。 The images are uploaded using multer , taking advantage of the fact that the GraphQL-Yoga server exposes the Express server underneath with server.express -- I'm assuming that it is OK to use the GraphQL-Yoga server as a regular Express/Node server when I need to. 图像是使用multer上传的,这是因为GraphQL-Yoga服务器使用server.express暴露了Express服务器。我假设可以将GraphQL-Yoga服务器用作常规Express / Node。服务器何时需要。

My question is: how do I change the status of the jobs to completed in my database when each job is done? 我的问题是: completed每个作业completed ,如何更改作业状态以在数据库中完成? I can access prisma directly, of course, as I do in my resolvers, but I was thinking it might be more elegant to use a Mutation, that is, keep all accesses to the DB using GraphQL. 当然,我可以像在解析器中一样直接访问prisma ,但是我认为使用Mutation可能更优雅,也就是说,使用GraphQL保留对DB的所有访问。 As said, I'm using Apollo from the React front end. 如前所述,我正在从React前端使用Apollo。 Can I 'call' a Mutation from the backend? 我可以从后端“称呼”变异吗? How might I do that? 我该怎么办?

Thanks for any insights! 感谢您的任何见解! I'm new to GraphQL and sometimes missing the forest for the trees... 我是GraphQL的新手,有时错过了树林。

I do something like that on my server, I can show you how I do it in case it helps you. 我在服务器上做了类似的事情,我可以告诉您如何做,以防它对您有所帮助。

So I export the Prisma db instance like this: 所以我像这样导出Prisma数据库实例:

import { Prisma } from 'prisma-binding';
export const db = new Prisma({
  typeDefs: 'src/generated/prisma.graphql',
  endpoint: process.env.PRISMA_ENDPOINT,
  secret: process.env.PRISMA_SECRET,
  debug: true,
});

Then, anywhere I want to use that, I just import it import { db } from '../config/config'; 然后,在任何我想使用它的地方,我只需import { db } from '../config/config'; and then I have access to the resolvers etc: 然后我可以访问解析器等:

db.query.user({}, ` { 
    id
    name
  } `
)

Or similar for mutations. 或类似的突变。

This is the only way I've been able to access data from other places in my server. 这是我能够从服务器中其他位置访问数据的唯一方法。 Not sure if that is what you are already doing, but just in case it can help you. 不知道这是否是您已经在做的,但以防万一它可以帮助您。

Cheers :) 干杯:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM