Do I need a mongoose with a schedule?

If I want to connect the mongo database to the graphql schema, do I need Mongoose ORM or can I just make calls with raw drivers?

+7
mongodb graphql
source share
1 answer

You can do both.

If you already have mongoose models installed, you can use them when writing resolve functions. See the following example .

 var QueryType = new GraphQLObjectType({ name: 'Query', fields: () => ({ todos: { type: new GraphQLList(TodoType), resolve: () => { return new Promise((resolve, reject) => { TODO.find((err, todos) => { if (err) reject(err) else resolve(todos) }) }) } } }) }) 

If you don’t have mongoose models or want to use your own mongodb driver, you can do this too. The following is a simple example using MongoDB Node.JS Driver .

 resolve: () => { return new Promise((resolve, reject) => { db.collection('todos').find({}).toArray((err, todos) => { if (err) reject(err) else resolve(todos) }) }) } 

If you have mongoose models and want to generate a GraphQL schema, you might be interested in graffiti-mongoose , which generates GraphQL types and schemas for existing mongoose models.

+6
source share

All Articles