-
Notifications
You must be signed in to change notification settings - Fork 0
/
01 - Intro.js
137 lines (128 loc) · 3.21 KB
/
01 - Intro.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
/**
* Lesson 01 - Individual requests for each item
*
* This is an Apollo GraphQL application with a PostGRES database
* that contains a table of posts and a table of users
*
* We have a posts query that returns an array of Posts
* And a users query that returns an array of Users
*
* Lets query the posts, and we can see every item
* in the database is returned
*
query {
posts {
id
title
}
}
*
* And the users query does the same for the users
*
query {
users {
id
name
}
}
*
* So what we're looking to accomplish here is to
* extend the Post query to also return the user
* who wrote the post
*
* We can start by adding a new field to the schema
* named author with a return type of User
*
* Then if we scroll down to the resolvers section
* for the "Post" type and add an "author" field
*
* We'll return a list of "users" where the id matches the post's "author_id"
* and since we only want one, grab the first one from the array
*
* Lets try that request out, with the author, ID, and name
*
query {
posts {
id
title
author {
id
name
}
}
}
*
* And we get all the author information for each post
*
* It's a little hard to tell what the database is actually doing here
* So we'll add some console logs to track how many times
* we're interacting with the database
*
* Under "posts", we'll do console log, select all from posts
*
* And under "author" we'll do console log, select all from users
* where id = post.author_id
*
* If we run our request again, we can see that we're only making
* one database request to fetch the list of posts
*
* But we're also making individual requests for the author on each post
* Including several requests for the same author
*
* At the end of this series, we'll show how to use Data Loaders
* for caching and batching database operations so that we can
* serve all of these results with only one database request
*/
const { ApolloServer, gql } = require('apollo-server')
const sql = require('knex')({
client: 'pg',
connection: {
host : '127.0.0.1',
port : 5432,
user : 'postgres',
password : 'password',
database : 'postgres'
}
});
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
type Query {
posts: [Post]
}
type Post {
id: String
title: String
author: User
}
type User {
id: String
name: String
}
`
const resolvers = {
Query: {
posts() {
// Executes once per query
console.log('SELECT * from posts')
return sql('posts').select('*')
},
},
Post: {
async author(post) {
// Executes once per post per query
console.log('SELECT * from users WHERE id =', post.author_id)
return sql
.select('*')
.from('users')
.where('id', post.author_id)
.first()
},
},
}
const server = new ApolloServer({ typeDefs, resolvers })
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
})