diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/package.json b/package.json index c35f022..161a092 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server/package.json b/server/package.json new file mode 100644 index 0000000..5091460 --- /dev/null +++ b/server/package.json @@ -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" + } +} diff --git a/server/schema/schema.js b/server/schema/schema.js new file mode 100644 index 0000000..b95406e --- /dev/null +++ b/server/schema/schema.js @@ -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 +}); \ No newline at end of file diff --git a/server/server/app.js b/server/server/app.js new file mode 100644 index 0000000..f9ac906 --- /dev/null +++ b/server/server/app.js @@ -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}...`)) +