Skip to content

Commit 4ed421c

Browse files
committed
everything is fine until mutations
1 parent 144a992 commit 4ed421c

File tree

4 files changed

+96
-45
lines changed

4 files changed

+96
-45
lines changed

Diff for: services/graphql-service/data-providers/address-provider.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
const fetch = require('node-fetch');
22
const fetchJson = require('node-fetch-json');
3+
// const HOST_NAME = 'address-service';
4+
const HOST_NAME = 'localhost';
35

46
class AddressProvider {
57
getAddress(userid) {
6-
return fetch(`http://address-service:8090/address/${userid}`)
8+
return fetch(`http://${HOST_NAME}:8090/address/${userid}`)
79
.then(response => response.json())
810
.then((data) => {
911
return data;
1012
});
1113
}
1214

1315
createAddress(userid, detail, city) {
14-
return fetchJson('http://address-service:8090/address', {
16+
return fetchJson(`http://${HOST_NAME}:8090/address`, {
1517
method: 'POST',
1618
body: {
1719
userid,

Diff for: services/graphql-service/data-providers/user-provider.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
const fetch = require('node-fetch');
22
const fetchJson = require('node-fetch-json');
3+
// const HOST_NAME = 'user-service';
4+
const HOST_NAME = 'localhost';
35

46
class UserProvider {
57
getAllUsers() {
6-
return fetch('http://localhost:8092/users').then(body => body);
8+
return fetch(`http://${HOST_NAME}:8092/users`).then(body => body.json());
79
}
810

9-
getUsers(id) {
11+
getUser(id) {
1012
if (!id) {
1113
return this.getAllUsers();
1214
}
13-
return fetch(`http://localhost:8092/users/${id}`)
14-
.then(response => response.json())
15-
.then((data) => {
16-
return data;
17-
});
15+
return fetch(`http://${HOST_NAME}:8092/users/${id}`)
16+
.then(response => response.json());
1817
}
1918

2019
createUser(name, email, password) {
21-
return fetchJson('http://localhost:8092/users', {
20+
return fetchJson(`http://${HOST_NAME}:8092/users`, {
2221
method: 'POST',
2322
body: {
2423
name,
2524
email,
2625
password
2726
}
28-
});
27+
}).then((data) => data.json());
2928
}
3029
}
3130

Diff for: services/graphql-service/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
const express = require('express');
22
const graphqlHTTP = require('express-graphql');
33
const { GraphQLSchema } = require('graphql');
4-
const { schema, QueryObjectType } = require('./schema/application.schema');
5-
const userProvider = require('./data-providers/user-provider.js');
6-
const addressProvider = require('./data-providers/address-provider.js');
4+
const { schema, QueryObjectType, MutationsType } = require('./schema/application.schema');
75
const app = express();
86

97
const rootValue = {
108
};
119

1210
let Schema = new GraphQLSchema({
13-
query: QueryObjectType
11+
query: QueryObjectType,
12+
mutation: MutationsType
1413
});
1514

1615

Diff for: services/graphql-service/schema/application.schema.js

+81-30
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,46 @@ const {
88
GraphQLInt,
99
GraphQLList,
1010
GraphQLScalarType,
11-
GraphQLEnumType
11+
GraphQLEnumType,
12+
GraphQLInputObjectType
1213
} = require('graphql');
1314

1415
const userProvider = require('../data-providers/user-provider.js');
16+
const addressProvider = require('../data-providers/address-provider.js');
1517

18+
const AddressType = new GraphQLObjectType({
19+
name: 'Address',
20+
description: 'Address of the users.',
21+
type: GraphQLObjectType,
22+
fields: () => {
23+
return {
24+
id: { type: GraphQLID },
25+
details: { type: GraphQLString },
26+
city: { type: GraphQLString },
27+
user: {
28+
type: UserType,
29+
description: 'Owner of the Address',
30+
resolve: (args) => userProvider.getUser(args.userid)
31+
}
32+
}
33+
}
34+
})
1635

17-
// const schema = buildSchema(`
18-
// type User {
19-
// id: ID
20-
// name: String!
21-
// email: String!
22-
// password: String!
23-
// address: [Address]
24-
// }
25-
26-
// type Address {
27-
// id: ID
28-
// detail: String!
29-
// city: String!
30-
// user: User!
31-
// }
32-
33-
// type Query {
34-
// getUsers(id: String): [User]
35-
// getAddressByUser(userid: String!): [Address]
36-
// }
37-
// `);
38-
39-
let UserType = new GraphQLObjectType({
36+
const UserType = new GraphQLObjectType({
4037
name: 'User',
41-
description: 'user api',
38+
description: 'Users of the application',
4239
type: GraphQLObjectType,
4340
fields: () => {
4441
return {
45-
id: { type: GraphQLString },
42+
id: { type: GraphQLID },
4643
name: { type: GraphQLString },
4744
email: { type: GraphQLString },
48-
password: { type: GraphQLString }
45+
password: { type: GraphQLString },
46+
addresses: {
47+
type: new GraphQLList(AddressType),
48+
description: 'List of addresses of the user.',
49+
resolve: (args) => addressProvider.getAddress(args.id)
50+
}
4951
};
5052
}
5153
});
@@ -64,10 +66,24 @@ const FIELDS = {
6466
args: {
6567
id: {
6668
description: 'User id',
67-
type: new GraphQLNonNull(GraphQLString)
69+
type: GraphQLString
6870
},
6971
},
70-
resolve: (_, { id, identity }) => userProvider.getUsers(id)
72+
resolve: (_, { id }) => userProvider.getUser(id)
73+
},
74+
users: {
75+
type: new GraphQLList(UserType),
76+
resolve: (_) => userProvider.getAllUsers()
77+
},
78+
addresses: {
79+
type: new GraphQLList(AddressType),
80+
args: {
81+
userid: {
82+
description: 'User id.',
83+
type: new GraphQLNonNull(GraphQLString)
84+
}
85+
},
86+
resolve: (_, { userid }) => addressProvider.getAddress(userid)
7187
}
7288
};
7389

@@ -77,4 +93,39 @@ const queryObjectType = new GraphQLObjectType({
7793
fields: () => FIELDS
7894
});
7995

80-
module.exports = { QueryObjectType: queryObjectType };
96+
const MUTATION_FIELDS = {
97+
createUser: {
98+
type: UserType,
99+
args: {
100+
userInput: {
101+
type: UserType
102+
},
103+
addressInput: {
104+
type: AddressType)
105+
}
106+
},
107+
resolver: (source, args) => {
108+
return userProvider.createUser(args.name, args.email, args.password)
109+
.then((user) => {
110+
if (args.address) {
111+
addressProvider.createAddress(user.id, args.address.detail, args.address.city);
112+
}
113+
return user;
114+
});
115+
}
116+
}
117+
// , createAddress: {
118+
119+
// }
120+
};
121+
122+
const mutationType = new GraphQLObjectType({
123+
name: 'ApplicationMutationAPI',
124+
description: 'APIs for users to mutation',
125+
fields: () => MUTATION_FIELDS
126+
});
127+
128+
module.exports = {
129+
QueryObjectType: queryObjectType,
130+
MutationsType: mutationType
131+
};

0 commit comments

Comments
 (0)