mirror of
https://github.com/sergeyyarkov/graphql-server-todo.git
synced 2026-10-11 10:19:27 +03:00
first mutation
This commit is contained in:
parent
8d4528057a
commit
d26dae32a1
@ -1,6 +1,8 @@
|
|||||||
const { GraphQLSchema } = require('graphql')
|
const { GraphQLSchema } = require('graphql')
|
||||||
|
const { Mutation } = require('./typeDefs/mutation')
|
||||||
const { Query } = require('./typeDefs/query')
|
const { Query } = require('./typeDefs/query')
|
||||||
|
|
||||||
module.exports = new GraphQLSchema({
|
module.exports = new GraphQLSchema({
|
||||||
query: Query,
|
query: Query,
|
||||||
|
mutation: Mutation,
|
||||||
})
|
})
|
||||||
|
|||||||
43
src/graphql/typeDefs/mutation.js
Normal file
43
src/graphql/typeDefs/mutation.js
Normal file
@ -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,
|
||||||
|
}
|
||||||
@ -9,7 +9,7 @@ const PORT = 3001
|
|||||||
server.use('/graphql', graphqlHTTP({ schema, graphiql: true }))
|
server.use('/graphql', graphqlHTTP({ schema, graphiql: true }))
|
||||||
|
|
||||||
mongoose.connect(
|
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 }
|
{ useNewUrlParser: true, useUnifiedTopology: true }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user