Skip to content

Commit 79039c3

Browse files
committed
implemented nested queries
1 parent e2574a4 commit 79039c3

3 files changed

Lines changed: 68 additions & 27 deletions

File tree

server/db.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"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" }
6+
],
7+
"companies": [
8+
{ "id": "1", "name": "Apple", "description": "iphone" },
9+
{ "id": "2", "name": "Tesla", "description": "car" }
10+
]
11+
}

server/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "ab-graphql-1",
33
"version": "1.0.0",
44
"scripts": {
5-
"start": "nodemon server.js"
5+
"start": "nodemon server.js",
6+
"json:server": "json-server --watch db.json"
67
},
78
"keywords": [
89
"blogger",
@@ -11,8 +12,10 @@
1112
"author": "devAbnull",
1213
"license": "ISC",
1314
"dependencies": {
15+
"axios": "^0.19.0",
1416
"express-graphql": "^0.8.0",
1517
"graphql": "^14.3.1",
18+
"json-server": "^0.15.0",
1619
"lodash": "^4.17.11"
1720
},
1821
"devDependencies": {

server/schema.js

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,75 @@
1-
const graphql = require('graphql');
2-
const _ = require('lodash');
1+
const graphql = require("graphql");
2+
const axios = require("axios");
3+
const _ = require("lodash");
34

45
const {
56
GraphQLObjectType,
67
GraphQLString,
78
GraphQLInt,
89
GraphQLSchema,
10+
GraphQLList
911
} = graphql;
1012

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-
]
13+
const BASE_URL = 'http://localhost:3000';
14+
15+
const fetchFromUrl = url =>
16+
axios
17+
.get(url)
18+
.then(_.property("data"))
19+
.catch(console.error);
20+
21+
const CompanyType = new GraphQLObjectType({
22+
name: "Company",
23+
fields: () => ({
24+
id: { type: GraphQLString },
25+
name: { type: GraphQLString },
26+
description: { type: GraphQLString },
27+
users: {
28+
type: new GraphQLList(UserType),
29+
resolve(parentValue, args) {
30+
return fetchFromUrl(`${BASE_URL}/companies/${parentValue.id}/users`);
31+
}
32+
}
33+
})
34+
});
2335

2436
const UserType = new GraphQLObjectType({
25-
name: 'User',
26-
fields: {
27-
id: {type: GraphQLString},
28-
firstName: {type: GraphQLString},
29-
age: {type: GraphQLInt},
30-
}
37+
name: "User",
38+
fields: () => ({
39+
id: { type: GraphQLString },
40+
firstName: { type: GraphQLString },
41+
age: { type: GraphQLInt },
42+
company: {
43+
type: CompanyType,
44+
resolve(parentValue, args) {
45+
return fetchFromUrl(
46+
`${BASE_URL}/companies/${parentValue.companyId}`
47+
);
48+
}
49+
}
50+
})
3151
});
3252

3353
const RootQuery = new GraphQLObjectType({
34-
name: 'RootQueryType',
54+
name: "RootQueryType",
3555
fields: {
36-
userKey: {
56+
user: {
3757
type: UserType,
3858
args: { id: { type: GraphQLString } },
3959
resolve(parentValue, args) {
40-
return _.find(users, { id: args.id });
60+
return fetchFromUrl(`${BASE_URL}/users/${args.id}`);
61+
}
62+
},
63+
company: {
64+
type: CompanyType,
65+
args: { id: { type: GraphQLString } },
66+
resolve(parentValue, args) {
67+
return fetchFromUrl(`${BASE_URL}/companies/${args.id}`);
4168
}
4269
}
4370
}
44-
})
71+
});
4572

4673
module.exports = new GraphQLSchema({
47-
query: RootQuery,
48-
})
74+
query: RootQuery
75+
});

0 commit comments

Comments
 (0)