-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.js
165 lines (151 loc) · 5.28 KB
/
index2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This is the practice file for the project.
// I want to be able to type every single line withoyt any external help
// Require ApolloServer
const { ApolloServer, gql } = require('apollo-server');
const employees = [
{
name: "Mike",
id: 1,
employerId: 1,
},
{
name: "Mike",
id: 2,
employerId: 1,
},
{
name: "Mike",
id: 3,
employerId: 2,
},
{
name: "Mike",
id: 4,
employerId: 3,
},
]
const employers = [
{
id: 1,
name: "Google",
},
{
id: 2,
name: "Facebook",
},
{
id: 3,
name: "Twitter",
},
]
// TYPEDEFS TYPEDEFS TYPEDEFS
const typeDefs = gql`
type Query {
employers: [Employer]
employees: [Employee]
employer(id: Int): Employer
employee(id: Int): Employee
}
type Employer {
id: Int
name: String
employees: [Employee]
numEmployees: Int
}
type Employee {
id: Int
name: String
employer: Employer
employerId: Int
}
type Mutation {
addEmployee(employerId: Int!, name: String!): Employee
removeEmployee(id: Int!): [Employee]
changeEmployeeName(id: Int!, name: String!): Employee
changeEmployer(id: Int!, employerId: Int!): Employee
}
`
// RESOLVERS RESOLVERS RESOLVERS
const resolvers = {
Query: {
employer: (parentValue, args, context, info) => employers.filter(e => e.id === args.id)[0],
employee: (parentValue, args, context, info) => employees.filter(e => e.id === args.id)[0],
employers: () => employers,
employees: () => employees
},
Employer: {
// Custom resolver functions under this employer data type
numEmployees: (parentValue) => {
console.log(`The parentValue in Employer: `, parentValue);
return employees.filter(e => e.employerId === parentValue.id).length
},
// employees returns an array of employees
employees: () =>{
return employees.filter(e => e.employerId === parentValue.id)
},
},
Employee: {
employer: (parentValue) => {
// to return one employer
return employers.filter(e => e.id === parentValue.employerId)[0]
}
},
Mutation: {
addEmployee: (_, args) => {
const newEmployee = {
id: employees.length + 1,
name: args.name,
employerId: args.employerId
}
employees.push(newEmployee)
return newEmployee
},
removeEmployee: (_, args) => {
return employees.filter(e => e.id !== args.id)
},
changeEmployeeName: (_,args) => {
let newEmployee;
// Change employees array
employees = employees.map(e => {
if(e.id === args.id){
newEmployee ={
...e,
name: args.name,
};
return newEmployee
}
//if condition is not matched return e
return e;
})
// return changed employee
return newEmployee
},
changeEmployer: (_, args) => {
let newEmployee;
employees = employees.map(e => {
if(e.id === args.id){
newEmployee = {
...e,
employerId: args.employerId
}
// adds the newEmployee to the employees Arrat
return newEmployee
}
return e
// If none of these conditions are met, then just return e, aka keep traversing through the list of employees
})
// Finally return an employee, like as we specified the return type would be in the typedef
return newEmployee
},
},
}
const server = new ApolloServer({typeDefs, resolvers})
server.listen().then(({ url }) => {
console.log(`🚀 Server Listening on port ${url}🚀 `);
})
// fieldName(obj, args, context, info) { result }
// These arguments have the following meanings and conventional names:
// obj: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-level Query field, the rootValue passed from the server configuration. This argument enables the nested nature of GraphQL queries.
// args: An object with the arguments passed into the field in the query. For example, if the field was called with author(name: "Ada"), the args object would be: { "name": "Ada" }.
// context: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. If you’re using Apollo Server, read about how to set the context in the setup documentation.
// info: This argument should only be used in advanced cases, but it contains information about the execution state of the query, including the field name, path to the field from the root, and more. It’s only documented in the GraphQL.js source code.