Skip to content

Instantly share code, notes, and snippets.

@AdityaRanjanSingh
Created September 30, 2019 22:34
Show Gist options
  • Save AdityaRanjanSingh/495f435914f728fcdfad55db2fe9bc6d to your computer and use it in GitHub Desktop.
Save AdityaRanjanSingh/495f435914f728fcdfad55db2fe9bc6d to your computer and use it in GitHub Desktop.
Graphql sample code to query and add items to mongodb
const express = require('express');
const graphqlHTTP = require('express-graphql');
const schema = require('./schema/schema');
const mongoose = require('mongoose');
const app = express();
const MongoClient = require('mongodb').MongoClient;
const uri = "";
mongoose.connection.once('open', () => {
console.log('connected to database')
})
mongoose.connection.on('error', (err) => {
console.log(`Connection error: ${err.message}`)
});
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
app.listen(4000, () => {
console.log('inside now listening')
});
const graphql = require('graphql');
const _ = require('lodash');
const Book = require('../models/book');
const Author = require('../models/author');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull
} = graphql;
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
author: {
type: AuthorType,
resolve(parent, args) {
return Author.findById(parent.authorId);
}
}
})
});
const AuthorType = new GraphQLObjectType({
name: 'Author',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age: { type: GraphQLInt },
books: {
type: new GraphQLList(BookType),
resolve(parent, args) {
return Book.find({ authorId: parent.id })
}
}
})
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args) {
//code to get the data from db
return Book.findById(args.id)
}
},
author: {
type: AuthorType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args) {
return Author.findById(args.id)
}
},
books: {
type: new GraphQLList(BookType),
resolve(parent, args) {
return Book.find({})
}
},
authors: {
type: new GraphQLList(AuthorType),
resolve(parent, args) {
return Author.find({})
}
}
}
});
const Mutations = new GraphQLObjectType({
name: 'Mutations',
fields: {
addAuthor: {
type: AuthorType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
age: { type: new GraphQLNonNull(GraphQLInt) }
},
resolve(parent, args) {
let author = new Author({
name: args.name,
age: args.age
});
return author.save();
}
},
addBook: {
type: BookType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
genre: { type: new GraphQLNonNull(GraphQLString) },
authorId: { type: new GraphQLNonNull(GraphQLID) }
},
resolve(parent, args) {
let book = new Book({
name: args.name,
genre: args.genre,
authorId: args.authorId
});
return book.save();
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutations
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment