I mean, how can I access graphql mutation within g...
# prisma-whats-new
s
I mean, how can I access graphql mutation within graphql-yoga middleware?
l
Instantiate the
db
separately and then pass it. E.g.
Copy code
const db = new Prisma({
  ...
})
...
module.exports = { db }
s
Oh... nice!
l
Just make sure you only export it once per project or you'll have multiple instances running around which will be a bad time 🙂
s
what about server file, index.ts?
Copy code
const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
      secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
      debug: true, // log all GraphQL queries & mutations
    }),
  }),
})
it already has Prisma instantiation
l
So just pull it out as
const db = ...
Copy code
const db = new Prisma({
endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
debug: true, // log all GraphQL queries & mutations
})

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db
  }),
})
Now you can pass db around or export it
s
here is my full code for server/index.ts file
Copy code
import { GraphQLServer } from 'graphql-yoga'
import * as express from "express";
import jwt from 'jsonwebtoken';
import { Prisma } from './generated/prisma'
import resolvers from './resolvers'
import confirmMiddleware from './confirm';

import {user as settings} from '../config';

const SECRET = settings.secret;

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
      secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
      debug: true, // log all GraphQL queries & mutations
    }),
  }),
})

server.express.get('/confirmation/:token', confirmMiddleware(SECRET, {}, jwt));


server.start(() => console.log(`Server is running on <http://localhost:4000`>))
l
Ok
s
confirmMiddleware
this guy needs access to db
how can pass db into it?
sorry, I am just confused
l
confirmMiddleware(SECRET, {}, jwt, db)
s
OK 🙂
l
Does that make sense?
s
vs code is saying that 'cannot find name'
if I hover
db
l
Copy code
import { GraphQLServer } from 'graphql-yoga'
import * as express from "express";
import jwt from 'jsonwebtoken';
import { Prisma } from './generated/prisma'
import resolvers from './resolvers'
import confirmMiddleware from './confirm';

import {user as settings} from '../config';

const SECRET = settings.secret;

const db = new Prisma({
endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
debug: true, // log all GraphQL queries & mutations
})

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db
  }),
})

server.express.get('/confirmation/:token', confirmMiddleware(SECRET, {}, jwt, db));


server.start(() => console.log(`Server is running on <http://localhost:4000`>))
s
thanks
let me try it
@lawjolla thank you so much for taking your time and helped me. I really appreciate. It all worked
parrotwave6 1