Skip to content

Instantly share code, notes, and snippets.

@dimuthu
Created February 10, 2020 09:38
Show Gist options
  • Save dimuthu/cb27e3aa57ebf1b09ec875087aa42cd0 to your computer and use it in GitHub Desktop.
Save dimuthu/cb27e3aa57ebf1b09ec875087aa42cd0 to your computer and use it in GitHub Desktop.
const graphql = require('graphql');
const Todo = require('../models/todo');
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLNonNull
} = graphql;
const TodoType = new GraphQLObjectType({
name: 'todos',
fields: () => ({
id: {
type: GraphQLID
},
title: {
type: GraphQLString
},
completed: {
type: GraphQLBoolean
},
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
todo: {
type: TodoType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args) {
return Todo.findById(args.id);
}
},
Todos: {
type: new GraphQLList(TodoType),
resolve(parent, args) {
return Todo.find({});
}
}
}
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addTodo: {
type: TodoType,
args: {
title: {
type: new GraphQLNonNull(GraphQLString)
},
completed: {
type: new GraphQLNonNull(GraphQLBoolean)
},
},
resolve(parent, args) {
let todo = new Todo({
title: args.title,
completed: args.completed,
});
return todo.save();
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment