From d26dae32a1e44d79c6df2f29e05776182b19bd1c Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 4 Jun 2020 20:05:01 +0300 Subject: [PATCH] first mutation --- src/graphql/schema.js | 2 ++ src/graphql/typeDefs/mutation.js | 43 ++++++++++++++++++++++++++++++++ src/server.js | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/graphql/typeDefs/mutation.js diff --git a/src/graphql/schema.js b/src/graphql/schema.js index 38acf86..d10ea88 100644 --- a/src/graphql/schema.js +++ b/src/graphql/schema.js @@ -1,6 +1,8 @@ const { GraphQLSchema } = require('graphql') +const { Mutation } = require('./typeDefs/mutation') const { Query } = require('./typeDefs/query') module.exports = new GraphQLSchema({ query: Query, + mutation: Mutation, }) diff --git a/src/graphql/typeDefs/mutation.js b/src/graphql/typeDefs/mutation.js new file mode 100644 index 0000000..9bf944f --- /dev/null +++ b/src/graphql/typeDefs/mutation.js @@ -0,0 +1,43 @@ +const { + GraphQLObjectType, + GraphQLString, + GraphQLID, + GraphQLNonNull, +} = require('graphql') +const { NoteType, CategoryType } = require('./types/index') + +const Notes = require('../models/note') +const Categories = require('../models/category') + +const Mutation = new GraphQLObjectType({ + name: 'Mutation', + fields: { + addCategory: { + type: CategoryType, + args: { title: { type: new GraphQLNonNull(GraphQLString) } }, + resolve(parent, args) { + return new Categories({ + title: args.title, + }).save() + }, + }, + addNote: { + type: NoteType, + args: { + title: { type: new GraphQLNonNull(GraphQLString) }, + categoryId: { type: new GraphQLNonNull(GraphQLID) }, + }, + resolve(parent, args) { + return new Notes({ + title: args.title, + createdAt: new Date().toISOString(), + categoryId: args.categoryId, + }).save() + }, + }, + }, +}) + +module.exports = { + Mutation, +} diff --git a/src/server.js b/src/server.js index d888148..71919bc 100644 --- a/src/server.js +++ b/src/server.js @@ -9,7 +9,7 @@ const PORT = 3001 server.use('/graphql', graphqlHTTP({ schema, graphiql: true })) mongoose.connect( - 'mongodb+srv://user:password@cluster0-1dsll.mongodb.net/todo-db?retryWrites=true&w=majority', + 'mongodb+srv://sergeyyarkov:1234@cluster0-1dsll.mongodb.net/todo-db?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true } )