mirror of
https://github.com/sergeyyarkov/graphql-server-todo.git
synced 2026-10-11 10:19:27 +03:00
added mongoose
This commit is contained in:
parent
809e8b1b7c
commit
8d4528057a
@ -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 }
|
||||
8
src/graphql/models/category.js
Normal file
8
src/graphql/models/category.js
Normal 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)
|
||||
10
src/graphql/models/note.js
Normal file
10
src/graphql/models/note.js
Normal 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)
|
||||
@ -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({})
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@ -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 })
|
||||
},
|
||||
},
|
||||
}),
|
||||
|
||||
@ -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}`))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user