Created
February 10, 2020 09:38
-
-
Save dimuthu/cb27e3aa57ebf1b09ec875087aa42cd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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