Skip to content

Commit 18ccfc7

Browse files
Merge pull request #1 from anthonypena97/develop
Feature/1/Created Express Server and GraphQL Schema with UserType, HobbyType, and PostType + dummy data
2 parents 16ad675 + 8f818e2 commit 18ccfc7

File tree

4 files changed

+199
-3
lines changed

4 files changed

+199
-3
lines changed

server/app.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
const express = require('express');
2+
const {graphqlHTTP} = require('express-graphql');
3+
4+
const schema = require('./schema/schema');
25

36
const app = express();
7+
8+
// Middleware
9+
app.use('/graphql', graphqlHTTP({
10+
schema: schema,
11+
graphiql: true,
12+
pretty: true,
13+
}));
14+
15+
const PORT = 4000;
416

5-
app.listen(4000, ()=>{
6-
console.log("Listening for requests on port 4000");
17+
app.listen(PORT, ()=>{
18+
console.log(`Listening for requests on port ${PORT}`);
719
});

server/package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"author": "anthonypena97",
1010
"license": "ISC",
1111
"dependencies": {
12-
"express": "^4.18.1"
12+
"express": "^4.18.1",
13+
"express-graphql": "^0.12.0",
14+
"graphql": "^16.5.0",
15+
"lodash": "^4.17.21"
1316
}
1417
}

server/schema/schema.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
const {GraphQLObjectType, GraphQLID, GraphQLString, GraphQLInt, GraphQLSchema, GraphQLBoolean, GraphQLList} = require('graphql');
2+
var _ = require('lodash');
3+
4+
// dummy data
5+
var userData = [
6+
{id: '1', name: 'Bond', age: 36, profession: 'Programmer',},
7+
{id: '13', name: 'Anna', age: 26, profession: 'Baker',},
8+
{id: '211', name: 'Bella', age: 16, profession: 'Mechanic',},
9+
{id: '19', name: 'Gina', age: 26, profession: 'Painter',},
10+
{id: '150', name: 'Georgina', age: 36, profession: 'Teacher'},
11+
];
12+
13+
var hobbyData = [
14+
{id: '1', title: 'Programming', description: 'Using computers to make the world a better place', userId: '150',},
15+
{id: '2', title: 'Rowing', description: 'Sweat and feel better before eating donoughts', userId: '19',},
16+
{id: '3', title: 'Swimming', description: 'Get in the ware and learn to become the water', userId: '1',},
17+
{id: '4', title: 'Fencing', description: 'A hobby for fency people', userId: '150',},
18+
{id: '5', title: 'Programming', description: 'Wear hiking boots and explore the world', userId: '1',},
19+
];
20+
21+
var postData = [
22+
{id: '1', comment: 'Building a Mind', userId: '1'},
23+
{id: '2', comment: 'GraphQL is Amazing', userId: '1'},
24+
{id: '3', comment: 'How to Change the World', userId: '19'},
25+
{id: '4', comment: 'How to Change the World', userId: '211'},
26+
{id: '5', comment: 'How to Change the World', userId: '1'},
27+
];
28+
29+
// create types
30+
const UserType = new GraphQLObjectType({
31+
name: 'User',
32+
description: 'Documentation for user...',
33+
fields: () => ({
34+
id: {type: GraphQLString},
35+
name: {type: GraphQLString},
36+
age: {type: GraphQLInt},
37+
profession: {type: GraphQLString},
38+
39+
posts:{
40+
type: new GraphQLList(PostType),
41+
resolve(parent, args){
42+
return _.filter(postData, {userId: parent.id});
43+
}
44+
},
45+
46+
hobbies:{
47+
type: new GraphQLList(HobbyType),
48+
resolve(parent, args){
49+
return _.filter(hobbyData, {userId: parent.id});
50+
}
51+
}
52+
})
53+
});
54+
55+
const HobbyType = new GraphQLObjectType({
56+
name: 'Hobby',
57+
description: 'Hobby Description',
58+
fields: () => ({
59+
id: {type: GraphQLID},
60+
title: {type: GraphQLString},
61+
description: {type: GraphQLString},
62+
user: {
63+
type: UserType,
64+
resolve(parent, args){
65+
return _.find(userData, {id: parent.userId});
66+
}
67+
},
68+
})
69+
});
70+
71+
const PostType = new GraphQLObjectType({
72+
name: 'Post',
73+
description: 'Post description',
74+
fields: () => ({
75+
id: {type: GraphQLID},
76+
comment: {type: GraphQLString},
77+
user: {
78+
type: UserType,
79+
resolve(parent, args){
80+
return _.find(userData, {id: parent.userId});
81+
}
82+
},
83+
})
84+
});
85+
86+
// RootQuery
87+
const RootQuery = new GraphQLObjectType({
88+
name: 'RootQueryType',
89+
description: 'Description',
90+
fields: {
91+
user:{
92+
type: UserType,
93+
args: {id: {type: GraphQLString}},
94+
95+
resolve(parent, args){
96+
97+
return _.find(userData, {id: args.id});
98+
99+
}
100+
},
101+
102+
hobby:{
103+
type: HobbyType,
104+
args: {id: {type: GraphQLID}},
105+
106+
resolve(parent, args){
107+
108+
return _.find(hobbyData, {id: args.id});
109+
110+
}
111+
},
112+
113+
post:{
114+
type: PostType,
115+
args: {id: {type: GraphQLID}},
116+
117+
resolve(parent, args){
118+
119+
return _.find(postData, {id: args.id});
120+
121+
}
122+
},
123+
124+
}
125+
});
126+
127+
const schema = new GraphQLSchema({
128+
query: RootQuery
129+
})
130+
131+
module.exports = schema;

0 commit comments

Comments
 (0)