mirror of
https://github.com/sergeyyarkov/graphql-server-todo.git
synced 2026-10-11 10:19:27 +03:00
server
This commit is contained in:
commit
809e8b1b7c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
node_modules
|
||||||
8
README.md
Normal file
8
README.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
## GraphQL Server Todo
|
||||||
|
|
||||||
|
### 🚀 Start Server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
npm start
|
||||||
|
```
|
||||||
1489
package-lock.json
generated
Normal file
1489
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
package.json
Normal file
20
package.json
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "graphql-server-test",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/server.js",
|
||||||
|
"dev": "nodemon src/server.js"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"express-graphql": "^0.9.0",
|
||||||
|
"graphql": "^15.0.0",
|
||||||
|
"mongoose": "^5.9.14",
|
||||||
|
"nodemon": "^2.0.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
58
src/graphql/data/index.js
Normal file
58
src/graphql/data/index.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
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 }
|
||||||
6
src/graphql/schema.js
Normal file
6
src/graphql/schema.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
const { GraphQLSchema } = require('graphql')
|
||||||
|
const { Query } = require('./typeDefs/query')
|
||||||
|
|
||||||
|
module.exports = new GraphQLSchema({
|
||||||
|
query: Query,
|
||||||
|
})
|
||||||
41
src/graphql/typeDefs/query.js
Normal file
41
src/graphql/typeDefs/query.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
const { GraphQLObjectType, GraphQLID, GraphQLList } = require('graphql')
|
||||||
|
const { notes, categories } = require('../data/index')
|
||||||
|
const { NoteType, CategoryType } = require('./types/index')
|
||||||
|
|
||||||
|
const Query = new GraphQLObjectType({
|
||||||
|
name: 'Query',
|
||||||
|
fields: {
|
||||||
|
note: {
|
||||||
|
type: NoteType,
|
||||||
|
args: { id: { type: GraphQLID } },
|
||||||
|
resolve(parent, args) {
|
||||||
|
return notes.find((note) => note.id === args.id)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
category: {
|
||||||
|
type: CategoryType,
|
||||||
|
args: { id: { type: GraphQLID } },
|
||||||
|
resolve(parent, args) {
|
||||||
|
return categories.find((category) => category.id === args.id)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
notes: {
|
||||||
|
type: new GraphQLList(NoteType),
|
||||||
|
args: { id: { type: GraphQLID } },
|
||||||
|
resolve(parent, args) {
|
||||||
|
return notes
|
||||||
|
},
|
||||||
|
},
|
||||||
|
categories: {
|
||||||
|
type: new GraphQLList(CategoryType),
|
||||||
|
args: { id: { type: GraphQLID } },
|
||||||
|
resolve(parent, args) {
|
||||||
|
return categories
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Query,
|
||||||
|
}
|
||||||
42
src/graphql/typeDefs/types/index.js
Normal file
42
src/graphql/typeDefs/types/index.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
const {
|
||||||
|
GraphQLObjectType,
|
||||||
|
GraphQLID,
|
||||||
|
GraphQLString,
|
||||||
|
GraphQLList,
|
||||||
|
} = require('graphql')
|
||||||
|
const { categories, notes } = require('../../data/index')
|
||||||
|
|
||||||
|
const NoteType = new GraphQLObjectType({
|
||||||
|
name: 'Note',
|
||||||
|
fields: () => ({
|
||||||
|
id: { type: GraphQLID },
|
||||||
|
title: { type: GraphQLString },
|
||||||
|
createdAt: { type: GraphQLString },
|
||||||
|
categoryId: { type: GraphQLID },
|
||||||
|
category: {
|
||||||
|
type: CategoryType,
|
||||||
|
resolve(parent, args) {
|
||||||
|
return categories.find((category) => category.id === parent.categoryId)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const CategoryType = new GraphQLObjectType({
|
||||||
|
name: 'Category',
|
||||||
|
fields: () => ({
|
||||||
|
id: { type: GraphQLID },
|
||||||
|
title: { type: GraphQLString },
|
||||||
|
notes: {
|
||||||
|
type: new GraphQLList(NoteType),
|
||||||
|
resolve(parent, args) {
|
||||||
|
return notes.filter((note) => note.categoryId === parent.id)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
NoteType,
|
||||||
|
CategoryType,
|
||||||
|
}
|
||||||
10
src/server.js
Normal file
10
src/server.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
const express = require('express')
|
||||||
|
const graphqlHTTP = require('express-graphql')
|
||||||
|
const schema = require('./graphql/schema')
|
||||||
|
|
||||||
|
const server = express()
|
||||||
|
const PORT = 3001
|
||||||
|
|
||||||
|
server.use('/graphql', graphqlHTTP({ schema, graphiql: true }))
|
||||||
|
|
||||||
|
server.listen(PORT, () => console.log(`🚀 App is running on port ${PORT}`))
|
||||||
Loading…
x
Reference in New Issue
Block a user