added mongoose

This commit is contained in:
Sergey Yarkov 2020-06-01 23:37:39 +03:00
parent 809e8b1b7c
commit 8d4528057a
6 changed files with 41 additions and 66 deletions

View File

@ -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 }

View File

@ -0,0 +1,8 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const categorySchema = new Schema({
title: String,
})
module.exports = mongoose.model('Category', categorySchema)

View File

@ -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)

View File

@ -1,7 +1,9 @@
const { GraphQLObjectType, GraphQLID, GraphQLList } = require('graphql') const { GraphQLObjectType, GraphQLID, GraphQLList } = require('graphql')
const { notes, categories } = require('../data/index')
const { NoteType, CategoryType } = require('./types/index') const { NoteType, CategoryType } = require('./types/index')
const Notes = require('../models/note')
const Categories = require('../models/category')
const Query = new GraphQLObjectType({ const Query = new GraphQLObjectType({
name: 'Query', name: 'Query',
fields: { fields: {
@ -9,28 +11,28 @@ const Query = new GraphQLObjectType({
type: NoteType, type: NoteType,
args: { id: { type: GraphQLID } }, args: { id: { type: GraphQLID } },
resolve(parent, args) { resolve(parent, args) {
return notes.find((note) => note.id === args.id) return Notes.findById(args.id)
}, },
}, },
category: { category: {
type: CategoryType, type: CategoryType,
args: { id: { type: GraphQLID } }, args: { id: { type: GraphQLID } },
resolve(parent, args) { resolve(parent, args) {
return categories.find((category) => category.id === args.id) return Categories.findById(args.id)
}, },
}, },
notes: { notes: {
type: new GraphQLList(NoteType), type: new GraphQLList(NoteType),
args: { id: { type: GraphQLID } }, args: { id: { type: GraphQLID } },
resolve(parent, args) { resolve(parent, args) {
return notes return Notes.find({})
}, },
}, },
categories: { categories: {
type: new GraphQLList(CategoryType), type: new GraphQLList(CategoryType),
args: { id: { type: GraphQLID } }, args: { id: { type: GraphQLID } },
resolve(parent, args) { resolve(parent, args) {
return categories return Categories.find({})
}, },
}, },
}, },

View File

@ -4,7 +4,9 @@ const {
GraphQLString, GraphQLString,
GraphQLList, GraphQLList,
} = require('graphql') } = require('graphql')
const { categories, notes } = require('../../data/index')
const Notes = require('../../models/note')
const Categories = require('../../models/category')
const NoteType = new GraphQLObjectType({ const NoteType = new GraphQLObjectType({
name: 'Note', name: 'Note',
@ -16,7 +18,7 @@ const NoteType = new GraphQLObjectType({
category: { category: {
type: CategoryType, type: CategoryType,
resolve(parent, args) { 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: { notes: {
type: new GraphQLList(NoteType), type: new GraphQLList(NoteType),
resolve(parent, args) { resolve(parent, args) {
return notes.filter((note) => note.categoryId === parent.id) return Notes.find({ categoryId: parent.id })
}, },
}, },
}), }),

View File

@ -1,10 +1,21 @@
const express = require('express') const express = require('express')
const graphqlHTTP = require('express-graphql') const graphqlHTTP = require('express-graphql')
const schema = require('./graphql/schema') const schema = require('./graphql/schema')
const mongoose = require('mongoose')
const server = express() const server = express()
const PORT = 3001 const PORT = 3001
server.use('/graphql', graphqlHTTP({ schema, graphiql: true })) 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}`)) server.listen(PORT, () => console.log(`🚀 App is running on port ${PORT}`))