Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect to gridFS? #51

Closed
jaqua opened this issue Mar 1, 2018 · 1 comment
Closed

How to connect to gridFS? #51

jaqua opened this issue Mar 1, 2018 · 1 comment
Labels

Comments

@jaqua
Copy link

jaqua commented Mar 1, 2018

I'm trying to implement apollo-upload-server (which is exactly what I'm needing) using gridFS. So I came up to #8 (comment)

But I've some problems how to get connected to the db at all in my express server (do I have to use middleware?), which looks like this:

import express from 'express'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import { apolloUploadExpress } from 'apollo-upload-server'
import crossOriginRequests from './middlewares/cors'
import schema from './graphql/schema'

const app = express()

app.use('/graphql',
  bodyParser.text({ type: 'application/graphql' }),
  crossOriginRequests,
  apolloUploadExpress(),
  graphqlExpress(req => ({
    schema: schema,
    context: { token: req.headers.authorization }
  }))
)

app.listen(port, () => {
  if (process.env.NODE_ENV !== 'production') {
    console.log('Listening on port ' + port)
  }
})

export default app

In my resolver I would like to write the stream:

import { GraphQLUpload } from 'apollo-upload-server'

const processUpload = async upload => {
  const { stream, filename, mimetype, encoding } = await upload

  // use gfs here to write stream...

  return { id, filename, mimetype, encoding }
}

export default {
  Upload: GraphQLUpload,
  Mutation: {
    singleUpload: (obj, { file }) => processUpload(file)
  }
}
@jaydenseric
Copy link
Owner

jaydenseric commented Mar 2, 2018

Not directly related to apollo-upload-server, but I will take a stab at answering anyway 😉

Keep in mind the MongoDB Node.js drivers have updated recently, so this is a bit different to how I used to do it…

In db.mjs:

import { MongoClient } from 'mongodb'

export let db

export async function connect() {
  if (db) return
  const client = await MongoClient.connect(process.env.DB_URI)
  db = client.db('yourdbname')
}

In server.mjs (using Koa):

import { connect } from './db'

// Blah blab…

connect()
  .then(() =>
    app.listen(process.env.PORT, error => {
      if (error) throw error
      console.info(
        `Serving http://localhost:${process.env.PORT} for ${
          process.env.NODE_ENV
        }.`
      )
    })
  )
  .catch(({ stack }) => console.error(stack))

To use the DB in resolvers:

import { db } from './db'

It is assumed that the db exists for resolvers to import because we waited for it to be created before starting the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants