From 8d4528057a268247d0916228ac8b494b3fb87915 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Mon, 1 Jun 2020 23:37:39 +0300 Subject: [PATCH] added mongoose --- src/graphql/data/index.js | 58 ----------------------------- src/graphql/models/category.js | 8 ++++ src/graphql/models/note.js | 10 +++++ src/graphql/typeDefs/query.js | 12 +++--- src/graphql/typeDefs/types/index.js | 8 ++-- src/server.js | 11 ++++++ 6 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 src/graphql/data/index.js create mode 100644 src/graphql/models/category.js create mode 100644 src/graphql/models/note.js diff --git a/src/graphql/data/index.js b/src/graphql/data/index.js deleted file mode 100644 index 5a6cb98..0000000 --- a/src/graphql/data/index.js +++ /dev/null @@ -1,58 +0,0 @@ -const notes = [ - { - id: '1', - title: 'Todo 1', - createdAt: '2020-09-28T13:44:55', - categoryId: '1', - }, - { - id: '2', - title: 'Todo 2', - createdAt: '2020-02-14T17:23:11', - categoryId: '3', - }, - { - id: '3', - title: 'Todo 3', - createdAt: '2020-02-14T17:23:11', - categoryId: '2', - }, - { - id: '4', - title: 'Todo 4', - createdAt: '2020-02-14T17:23:11', - categoryId: '3', - }, - { - id: '5', - title: 'Todo 5', - createdAt: '2020-02-14T17:23:11', - categoryId: '3', - }, - { - id: '6', - title: 'Todo 6', - createdAt: '2020-02-14T17:23:11', - categoryId: '2', - }, - { - id: '7', - title: 'Todo 7', - createdAt: '2020-02-14T17:23:11', - categoryId: '1', - }, - { - id: '8', - title: 'Todo 8', - createdAt: '2020-02-14T17:23:11', - categoryId: '3', - }, -] - -const categories = [ - { id: '1', title: 'News' }, - { id: '2', title: 'Work' }, - { id: '3', title: 'Health' }, -] - -module.exports = { notes, categories } diff --git a/src/graphql/models/category.js b/src/graphql/models/category.js new file mode 100644 index 0000000..54be811 --- /dev/null +++ b/src/graphql/models/category.js @@ -0,0 +1,8 @@ +const mongoose = require('mongoose') +const Schema = mongoose.Schema + +const categorySchema = new Schema({ + title: String, +}) + +module.exports = mongoose.model('Category', categorySchema) diff --git a/src/graphql/models/note.js b/src/graphql/models/note.js new file mode 100644 index 0000000..af89ab3 --- /dev/null +++ b/src/graphql/models/note.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose') +const Schema = mongoose.Schema + +const noteSchema = new Schema({ + title: String, + createdAt: String, + categoryId: String, +}) + +module.exports = mongoose.model('Note', noteSchema) diff --git a/src/graphql/typeDefs/query.js b/src/graphql/typeDefs/query.js index 8040edd..ac5735f 100644 --- a/src/graphql/typeDefs/query.js +++ b/src/graphql/typeDefs/query.js @@ -1,7 +1,9 @@ const { GraphQLObjectType, GraphQLID, GraphQLList } = require('graphql') -const { notes, categories } = require('../data/index') const { NoteType, CategoryType } = require('./types/index') +const Notes = require('../models/note') +const Categories = require('../models/category') + const Query = new GraphQLObjectType({ name: 'Query', fields: { @@ -9,28 +11,28 @@ const Query = new GraphQLObjectType({ type: NoteType, args: { id: { type: GraphQLID } }, resolve(parent, args) { - return notes.find((note) => note.id === args.id) + return Notes.findById(args.id) }, }, category: { type: CategoryType, args: { id: { type: GraphQLID } }, resolve(parent, args) { - return categories.find((category) => category.id === args.id) + return Categories.findById(args.id) }, }, notes: { type: new GraphQLList(NoteType), args: { id: { type: GraphQLID } }, resolve(parent, args) { - return notes + return Notes.find({}) }, }, categories: { type: new GraphQLList(CategoryType), args: { id: { type: GraphQLID } }, resolve(parent, args) { - return categories + return Categories.find({}) }, }, }, diff --git a/src/graphql/typeDefs/types/index.js b/src/graphql/typeDefs/types/index.js index d12df63..6114f6b 100644 --- a/src/graphql/typeDefs/types/index.js +++ b/src/graphql/typeDefs/types/index.js @@ -4,7 +4,9 @@ const { GraphQLString, GraphQLList, } = require('graphql') -const { categories, notes } = require('../../data/index') + +const Notes = require('../../models/note') +const Categories = require('../../models/category') const NoteType = new GraphQLObjectType({ name: 'Note', @@ -16,7 +18,7 @@ const NoteType = new GraphQLObjectType({ category: { type: CategoryType, resolve(parent, args) { - return categories.find((category) => category.id === parent.categoryId) + return Categories.findById(parent.categoryId) }, }, }), @@ -30,7 +32,7 @@ const CategoryType = new GraphQLObjectType({ notes: { type: new GraphQLList(NoteType), resolve(parent, args) { - return notes.filter((note) => note.categoryId === parent.id) + return Notes.find({ categoryId: parent.id }) }, }, }), diff --git a/src/server.js b/src/server.js index f411ec1..d888148 100644 --- a/src/server.js +++ b/src/server.js @@ -1,10 +1,21 @@ const express = require('express') const graphqlHTTP = require('express-graphql') const schema = require('./graphql/schema') +const mongoose = require('mongoose') const server = express() 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', + { useNewUrlParser: true, useUnifiedTopology: true } +) + +mongoose.connection.on('error', (err) => + console.log(`❌ Database error: ${err}`) +) +mongoose.connection.once('open', () => console.log('✔️ Database connected!')) + server.listen(PORT, () => console.log(`🚀 App is running on port ${PORT}`))