Skip to content

Commit e2574a4

Browse files
committed
initial commit: started graphql server
0 parents  commit e2574a4

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/node_modules/**

server/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ab-graphql-1",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"start": "nodemon server.js"
6+
},
7+
"keywords": [
8+
"blogger",
9+
"basic"
10+
],
11+
"author": "devAbnull",
12+
"license": "ISC",
13+
"dependencies": {
14+
"express-graphql": "^0.8.0",
15+
"graphql": "^14.3.1",
16+
"lodash": "^4.17.11"
17+
},
18+
"devDependencies": {
19+
"express": "^4.17.1",
20+
"nodemon": "^1.19.1"
21+
}
22+
}

server/schema.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const graphql = require('graphql');
2+
const _ = require('lodash');
3+
4+
const {
5+
GraphQLObjectType,
6+
GraphQLString,
7+
GraphQLInt,
8+
GraphQLSchema,
9+
} = graphql;
10+
11+
const users = [
12+
{
13+
id: '12',
14+
firstName: 'Alice',
15+
age: 22,
16+
},
17+
{
18+
id: '23',
19+
firstName: 'Bob',
20+
age: 23,
21+
}
22+
]
23+
24+
const UserType = new GraphQLObjectType({
25+
name: 'User',
26+
fields: {
27+
id: {type: GraphQLString},
28+
firstName: {type: GraphQLString},
29+
age: {type: GraphQLInt},
30+
}
31+
});
32+
33+
const RootQuery = new GraphQLObjectType({
34+
name: 'RootQueryType',
35+
fields: {
36+
userKey: {
37+
type: UserType,
38+
args: { id: { type: GraphQLString } },
39+
resolve(parentValue, args) {
40+
return _.find(users, { id: args.id });
41+
}
42+
}
43+
}
44+
})
45+
46+
module.exports = new GraphQLSchema({
47+
query: RootQuery,
48+
})

server/server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const express = require('express');
2+
const app = express();
3+
const expressGraphQL = require('express-graphql');
4+
const schema = require('./schema');
5+
6+
const PORT = 9000;
7+
8+
app.get('/', (req, res) => {
9+
res.send('Hi its working! coool');
10+
});
11+
12+
app.use('/graphql', expressGraphQL({
13+
graphiql: true,
14+
schema
15+
}));
16+
17+
app.listen(PORT, () => {
18+
console.log(`Listening on port http://localhost:${PORT}/`);
19+
})

0 commit comments

Comments
 (0)