Prisma 1 Forum

Should I migrate from GraphQL-Yoga to Apollo-Server?

Topic.

There has already been some discussion here: graphql-yoga #449

Is there an official stance on on this? Will yoga still be the recomended way to go?

Update: I migrated to apollo-server. Here is my index.js before

const { GraphQLServer } = require('graphql-yoga')
const prisma = require('./prisma')
const { resolvers } = require('./resolvers')
const middlewares = require('./middlewares')

const server = new GraphQLServer({
  typeDefs: './src/schema/index.graphql',
  resolvers,
  middlewares, // disabled auth for development
  context: req => ({
    ...req,
    prisma
  })
})

const options = {
  // only enable playground during development
  playground: process.env.NODE_ENV === 'development' ? '/' : false
}

server.start(options, ({ port, endpoint }) => console.log(`Server is running on http://localhost:${port}${endpoint === '/' ? '' : endpoint }`))

And here it is after

const { ApolloServer, makeExecutableSchema } = require('apollo-server')
const { applyMiddleware } = require('graphql-middleware')
const { importSchema } = require('graphql-import')

const prisma = require('./prisma')

const typeDefs = importSchema('./src/schema/index.graphql')
const { resolvers } = require('./resolvers')
const middlewares = require('./middlewares')

const schema = makeExecutableSchema({ typeDefs, resolvers })
const schemaWithMiddleware = applyMiddleware(schema, ...middlewares)

const server = new ApolloServer({
  schema: schemaWithMiddleware,
  context: ({ req }) => ({
    ...req,
    prisma
  }),
  playground: process.env.NODE_ENV === 'development',
  debug: process.env.NODE_ENV === 'development'
})

server.listen()
  .then(({ url, server }) => {
    console.log(`Server is running on ${url}`)
  })

I have not done any testing yet but at least it serves the schema and grapqhl playground.

2 Likes

Update 2: split that file for clarity. Here are my main files if anyone wants to use it as an example:

// src/index.js
const { ApolloServer } = require('apollo-server')

const prisma = require('./prisma')
const schema = require('./schema')

const server = new ApolloServer({
  schema,
  context: ({ req }) => ({
    ...req,
    prisma
  }),
  playground: process.env.NODE_ENV === 'development',
  debug: process.env.NODE_ENV === 'development'
})

server.listen()
  .then(({ url, server }) => {
    console.log(`Server is running on ${url}`)
  })
// src/schema.js
const { makeExecutableSchema } = require('apollo-server')
const { applyMiddleware } = require('graphql-middleware')
const { importSchema } = require('graphql-import')

const typeDefs = importSchema('./src/schema/index.graphql')
const { resolvers } = require('./resolvers')
const middlewares = require('./middlewares')

const schema = makeExecutableSchema({ typeDefs, resolvers })
const schemaWithMiddleware = applyMiddleware(schema, ...middlewares)

module.exports = schemaWithMiddleware
// src/resolvers.js
const { extractFragmentReplacements } = require('prisma-binding')

const resolvers = {
  Query: require('./resolvers/Query'),
  Mutation: require('./resolvers/Mutation'),
  AuthPayload: require('./resolvers/AuthPayload')
}

module.exports = {
  resolvers,
  fragmentReplacements: extractFragmentReplacements(resolvers)
}
// src/prisma.js
const Bindings = require('prisma-binding')
const Client = require('./schema/generated/prisma-client')
const { fragmentReplacements } = require('./resolvers')

module.exports = {
  client: new Client.Prisma({
    fragmentReplacements,
    endpoint: process.env.PRISMA_ENDPOINT,
    secret: process.env.PRISMA_SECRET,
    debug: false
  }),
  bindings: new Bindings.Prisma({
    typeDefs: 'src/schema/generated/prisma.graphql',
    fragmentReplacements,
    endpoint: process.env.PRISMA_ENDPOINT,
    secret: process.env.PRISMA_SECRET,
    debug: false
  })
}
8 Likes

Intresting to see this. Most resources and tutorials are currently using graphql-yoga, so been wondering how to do this with pure prismaapollo-server only. If you have a boilerplate repo it would be nice to see the full setup. :+1:

1 Like

You may find this small project interesting:

2 Likes

Hey, BenoitRanque, thank you for posting your solution. I followed your update and succesfuly managed to get things working!

I have a minor suggestion that may help. If you spread ...prisma into centext. Then you may use forwardTo like this in your queries/mutations… const { forwardTo } = require('prisma-binding'); and users: forwardTo('bindings'), :slight_smile:

I migrate today to apollo server.
Hope the Prisma team will give us a clear guideline regarding this matter: should we migrate GraphQL Yoga to apollo server?