➕ added graphql server

This commit is contained in:
Sergey Yarkov 2020-03-01 20:03:15 +03:00
parent a5d2501083
commit 01e49f74b3
5 changed files with 76 additions and 2 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
package-lock.json

View File

@ -5,8 +5,8 @@
"main": "index.js",
"scripts": {
"build": "npm run --prefix ./client build",
"dev": "npm run --prefix ./client dev",
"start": "npm run --prefix ./client start"
"dev": "concurrently \"npm run --prefix ./server/ dev\" \"npm run --prefix ./client/ start\"",
"start": "concurrently \"npm run --prefix ./server/ start\" \"npm run --prefix ./client/ start\""
},
"repository": {
"type": "git",

21
server/package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "graphql-tut",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon server/app.js",
"start": "node server/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^14.6.0",
"mongoose": "^5.9.2",
"nodemon": "^2.0.2"
}
}

35
server/schema/schema.js Normal file
View File

@ -0,0 +1,35 @@
const graphql = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, GraphQLList } = graphql;
const Articles = [
{id: 1, title: 'Article 1'},
{id: 2, title: 'Article 2'},
{id: 3, title: 'Article 3'},
{id: 4, title: 'Article 4'},
{id: 5, title: 'Article 5'}
]
const ArticleType = new GraphQLObjectType({
name: 'Article',
fields: () => ({
id: { type: GraphQLID },
title: { type: GraphQLString }
})
})
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
articles: {
type: new GraphQLList(ArticleType),
resolve() {
return Articles
}
}
}
})
module.exports = new GraphQLSchema({
query: Query
});

16
server/server/app.js Normal file
View File

@ -0,0 +1,16 @@
const express = require('express')
const graphqlHTTP = require('express-graphql')
const schema = require('../schema/schema')
const cors = require('cors')
const app = express()
app.use(cors())
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}))
const PORT = 3005
app.listen(PORT, () => console.log(`Server listen port ${PORT}...`))