简体   繁体   中英

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. 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. 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.

My question is: how do I change the status of the jobs to completed in my database when each job is done? 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. As said, I'm using Apollo from the React front end. 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...

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:

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'; 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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