-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
279 lines (240 loc) Β· 7.21 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
require ('./src/server')
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
// Creating a standalone GraphQL Server Using the Apollo Client ApolloServer
// const {ApolloServer, gql} = require('apollo-server')
// const crypto = require('crypto') // Generate Ids for Users
// const fakeDb = {
// users: [
// {
// id: '1',
// email: 'frankc@gmail.com',
// name: 'Frank',
// role: 'Software Engineer',
// avatarUrl: 'https://gravatar.com/...'
// },
// {
// id: '2',
// email: 'kristenc@gmail.com',
// name: 'Kristen',
// role: 'doTERRA Health Advocate',
// avatarUrl: 'https://gravatar.com/...'
// }
// ],
// messages: [
// {
// id: '1',
// userId: '1',
// body: 'Hi from user 1',
// createdAt: Date.now(),
// },
// {
// id: '2',
// userId: '2',
// body: 'Hey from user 2',
// createdAt: Date.now(),
// },
// {
// id: '3',
// userId: '1',
// body: 'How are you? - User 1',
// createdAt: Date.now(),
// }
// ]
// }
// // Basically our Schema
// const typeDefs = gql`
// type Query {
// users: [User!]!,
// user(id: ID!): User,
// messages: [Message!]!
// }
// type Mutation {
// addUser(email: String!, name: String, role: String!): User
// }
// type User {
// id: ID!
// email: String!
// name: String
// avatarUrl: String
// role: String
// messages: [Message!]!
// }
// type Message {
// id: ID!
// body: String!
// createdAt: String
// }
// `
// // My resolvers
// const resolvers = {
// Query: {
// users: () => fakeDb.users,
// user: args => fakeDb.users.find(user => user.id === args.id),
// messages: () => fakeDb.messages
// },
// Mutation: {
// addUser: ({email, name}) => {
// const user = {
// id: crypto.randomBytes(10).toString('hex'),
// email,
// name
// }
// fakeDb.users.push(user)
// return user
// }
// },
// User: {
// messages: ({id}) => fakeDb.messages.filter(message => message.userId === id)
// }
// }
// const server = new ApolloServer({typeDefs, resolvers})
// server.listen().then((serverInfo) => {
// console.log(`Apollo Server Listening at ${serverInfo.url}`)
// })
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
// Express and GraphQL
// const express = require('express') // Node Express server to run
// const graphqlHttp = require('express-graphql') // Middleware for connecting Express to GraphQL
// const {buildSchema} = require('graphql') // Required for building GraphQL Schema
// const crypto = require('crypto') // Do not need to yarn add because it's built into Node
// /* GraphQL created by Facebook in 2012
// GraphQL is not a storage or database engine, it's an extraction layer on top of the existing db/API
// that comes with a query language that allows me to create queries for my GraphQL service
// When I query my service I ussue queries with the specific data and specific fields I want to get back
// Very useful for REST APIs due to reducing under fetching or over fetching of data
// Useful for multiple clients for ex web client or mobile client
// */
// // I create fake DB since we do not have an existing API to set the layer on now
// // This could be PostgreSQL or the collections could be hosted by a AWS mLab sandbox env vars outside
// const fakeDb = {
// users: [
// {
// id: '1',
// email: 'frankc@gmail.com',
// name: 'Frank',
// role: 'Software Engineer',
// avatarUrl: 'https://gravatar.com/...'
// },
// {
// id: '2',
// email: 'kristenc@gmail.com',
// name: 'Kristen',
// role: 'doTERRA Health Advocate',
// avatarUrl: 'https://gravatar.com/...'
// }
// ],
// messages: [
// {
// id: '1',
// userId: '1',
// body: 'Hi from user 1',
// createdAt: Date.now(),
// },
// {
// id: '2',
// userId: '2',
// body: 'Hey from user 2',
// createdAt: Date.now(),
// },
// {
// id: '3',
// userId: '1',
// body: 'How are you? - User 1',
// createdAt: Date.now(),
// }
// ]
// }
// // We need a User class for correlating messages to specific Users
// class User {
// constructor(user) {
// Object.assign(this, user)
// }
// get messages() {
// return fakeDb.messages.filter(message => message.userId === this.id)
// }
// }
// // Schema types for GraphQL Queries:
// // Mutating(adding a user), fetching an individual User or individual Message
// const schema = buildSchema(`
// type Query {
// users: [User!]!,
// user(id: ID!): User,
// messages: [Message!]!
// }
// type Mutation {
// addUser(email: String!, name: String, role: String!): User
// }
// type User {
// id: ID!
// email: String!
// name: String
// avatarUrl: String
// role: String
// messages: [Message!]!
// }
// type Message {
// id: ID!
// body: String!
// createdAt: String
// }
// `)
// // rootValue for handling response callbacks to handle Http request
// const rootValue = {
// users: () => fakeDb.users.map(user => new User(user)),
// addUser: ({email, name}) => {
// const user = {
// id: crypto.randomBytes(10).toString('hex'),
// email,
// name
// }
// fakeDb.users.push(user)
// return user
// },
// user: args => fakeDb.users.find(user => user.id === args.id),
// messages: () => fakeDb.messages
// }
// // I need a Node Express Server
// const app = express()
// // I set the endpoint which will be localhost:3000/graphql and use graphqlHttp middleware
// // Middleware, our object with config including GUIy available in browser at the port
// app.use('/graphql', graphqlHttp({
// schema,
// rootValue,
// graphiql: true
// }))
// // I listen for the port and console.log that I'm listening
// app.listen(3000, () => console.log('Listening on Port 3000'))
// /* GET requestExample:
// curl -X POST -H "Content-Type: application/json" -d '{"query": "{users {id} }"}' localhost:3000/graphql -w "\n"
// We can make AJAX, ApolloClient, dispatch Redux Action creators, or a MobX fetch equivalent from the Client side to GraphQL to
// query the database specifically to store in the client side assisting with payload performance improvement
// Ex:
// # {
// # users {
// # name
// # role
// # messages {
// # id
// # body
// # }
// # }
// # }
// # query {
// # users {
// # id
// # email
// # name
// # avatarUrl
// # role
// # messages {
// # id
// # body
// # createdAt
// # }
// # }
// # }
// */