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.
Ahmad ferdous
source share