Skip to content

Commit 6f0df86

Browse files
committed
more example and understanding graphql
1 parent 67bbf00 commit 6f0df86

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

server/schema.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const deleteOnUrl = url => getDataPromise(axios.delete(url));
2424

2525
const editOnUrl = (url, body) => getDataPromise(axios.patch(url, body));
2626

27+
const fetchUsersByFirstName = args => {
28+
const fetchUsersPromise = fetchFromUrl(`${BASE_URL}/users`);
29+
return fetchUsersPromise.then(allUsers =>
30+
_.find(allUsers, { firstName: args.firstName })
31+
);
32+
};
33+
2734
const CompanyType = new GraphQLObjectType({
2835
name: "Company",
2936
fields: () => ({
@@ -55,12 +62,23 @@ const UserType = new GraphQLObjectType({
5562
});
5663

5764
const RootQuery = new GraphQLObjectType({
58-
name: "RootQueryType",
65+
name: "RootQueryHere",
5966
fields: {
67+
hello: {
68+
// for more understanding basic example
69+
type: GraphQLString,
70+
resolve() {
71+
return "world";
72+
}
73+
},
6074
user: {
6175
type: UserType,
62-
args: { id: { type: GraphQLString } },
76+
args: { id: { type: GraphQLString }, firstName: { type: GraphQLString } },
6377
resolve(parentValue, args) {
78+
if (!args.id) {
79+
// improvising query
80+
return fetchUsersByFirstName(args);
81+
}
6482
return fetchFromUrl(`${BASE_URL}/users/${args.id}`);
6583
}
6684
},
@@ -75,7 +93,7 @@ const RootQuery = new GraphQLObjectType({
7593
});
7694

7795
const mutation = new GraphQLObjectType({
78-
name: "mutation",
96+
name: "MutationHere",
7997
fields: {
8098
addUser: {
8199
type: UserType,

0 commit comments

Comments
 (0)