Skip to content

Commit 67bbf00

Browse files
committed
implemented mutations (add, del, edit)
1 parent 79039c3 commit 67bbf00

2 files changed

Lines changed: 88 additions & 16 deletions

File tree

server/db.json

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
{
22
"users": [
3-
{ "id": "23", "firstName": "Bob", "age": 21, "companyId": "1" },
4-
{ "id": "34", "firstName": "Alice", "age": 25, "companyId": "2" },
5-
{ "id": "34", "firstName": "Max", "age": 27, "companyId": "1" }
3+
{
4+
"id": "23",
5+
"firstName": "Bob",
6+
"age": 21,
7+
"companyId": "1"
8+
},
9+
{
10+
"id": "34",
11+
"firstName": "Alice",
12+
"age": 25,
13+
"companyId": "2"
14+
},
15+
{
16+
"id": "34",
17+
"firstName": "Max",
18+
"age": 27,
19+
"companyId": "1"
20+
},
21+
{
22+
"firstName": "Morty",
23+
"age": 17,
24+
"companyId": "2",
25+
"id": "Pil8msS"
26+
}
627
],
728
"companies": [
8-
{ "id": "1", "name": "Apple", "description": "iphone" },
9-
{ "id": "2", "name": "Tesla", "description": "car" }
29+
{
30+
"id": "1",
31+
"name": "Apple",
32+
"description": "iphone"
33+
},
34+
{
35+
"id": "2",
36+
"name": "Tesla",
37+
"description": "car"
38+
}
1039
]
1140
}

server/schema.js

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ const {
77
GraphQLString,
88
GraphQLInt,
99
GraphQLSchema,
10-
GraphQLList
10+
GraphQLList,
11+
GraphQLNonNull
1112
} = graphql;
1213

13-
const BASE_URL = 'http://localhost:3000';
14+
const BASE_URL = "http://localhost:3000";
1415

15-
const fetchFromUrl = url =>
16-
axios
17-
.get(url)
18-
.then(_.property("data"))
19-
.catch(console.error);
16+
const getDataPromise = axiosPromise =>
17+
axiosPromise.then(_.property("data")).catch(console.error);
18+
19+
const fetchFromUrl = url => getDataPromise(axios.get(url));
20+
21+
const addOnUrl = (url, body) => getDataPromise(axios.post(url, body));
22+
23+
const deleteOnUrl = url => getDataPromise(axios.delete(url));
24+
25+
const editOnUrl = (url, body) => getDataPromise(axios.patch(url, body));
2026

2127
const CompanyType = new GraphQLObjectType({
2228
name: "Company",
@@ -42,9 +48,7 @@ const UserType = new GraphQLObjectType({
4248
company: {
4349
type: CompanyType,
4450
resolve(parentValue, args) {
45-
return fetchFromUrl(
46-
`${BASE_URL}/companies/${parentValue.companyId}`
47-
);
51+
return fetchFromUrl(`${BASE_URL}/companies/${parentValue.companyId}`);
4852
}
4953
}
5054
})
@@ -70,6 +74,45 @@ const RootQuery = new GraphQLObjectType({
7074
}
7175
});
7276

77+
const mutation = new GraphQLObjectType({
78+
name: "mutation",
79+
fields: {
80+
addUser: {
81+
type: UserType,
82+
args: {
83+
firstName: { type: new GraphQLNonNull(GraphQLString) },
84+
age: { type: new GraphQLNonNull(GraphQLInt) },
85+
companyId: { type: GraphQLString }
86+
},
87+
resolve(parentValue, args) {
88+
return addOnUrl(`${BASE_URL}/users`, args);
89+
}
90+
},
91+
deleteUser: {
92+
type: UserType,
93+
args: {
94+
id: { type: new GraphQLNonNull(GraphQLString) }
95+
},
96+
resolve(parentValue, args) {
97+
return deleteOnUrl(`${BASE_URL}/users/${args.id}`);
98+
}
99+
},
100+
editUser: {
101+
type: UserType,
102+
args: {
103+
id: { type: new GraphQLNonNull(GraphQLString) },
104+
firstName: { type: GraphQLString },
105+
age: { type: GraphQLInt },
106+
companyId: { type: GraphQLString }
107+
},
108+
resolve(parentValue, args) {
109+
return editOnUrl(`${BASE_URL}/users/${args.id}`, args);
110+
}
111+
}
112+
}
113+
});
114+
73115
module.exports = new GraphQLSchema({
74-
query: RootQuery
116+
query: RootQuery,
117+
mutation
75118
});

0 commit comments

Comments
 (0)