Table of Contents
Preface vii
1 Welcome to GraphQL 1
What Is GraphQL? 2
The GraphQL Specification 4
Design Principles of GraphQL 5
Origins of GraphQL 5
History of Data Transport 6
Remote Procedure Call 6
Simple Object Access Protocol 6
REST 7
REST Drawbacks 7
Overfetching 8
Underfetching 9
Managing REST Endpoints 11
GraphQL in the Real World 12
GraphQL Clients 12
2 Graph Theory 15
Graph Theory Vocabulary 18
History of Graph Theory 20
Trees are Graphs 24
Graphs in the Real World 27
3 The GraphQL Query Language 31
GraphQL API Tools 33
GraphiQL 33
GraphQL Playground 36
Public GraphQL APIs 38
The GraphQL Query 38
Edges and Connections 41
Fragments 43
Mutations 48
Using Query Variables 50
Subscriptions 51
Introspection 52
Abstract Syntax Trees 53
4 Designing a Schema 57
Defining Types 57
Types 58
Scalar Types 59
Enums 59
Connections and Lists 60
One-to-One Connections 61
One-to-Many Connections 62
Many-to-Many Connections 64
Lists of Different Types 66
Arguments 68
Filtering Data 69
Mutations 72
Input Types 74
Return Types 77
Subscriptions 78
Schema Documentation 79
5 Creating a GraphQL API 83
Project Setup 83
Resolvers 84
Root Resolvers 86
Type Resolvers 88
Using Inputs and Enums 91
Edges and Connections 93
Custom Scalars 97
Apollo-server-express 102
Context 104
Installing Mongo 104
Adding Database to Context 105
GitHub Authorization 107
Setting Up GitHub OAuth 108
The Authorization Process 111
GithubAuth Mutation 112
Authenticating Users 115
Conclusion 121
6 GraphQL Clients 123
Using a GraphQL API 123
Fetch Requests 123
Graphql-request 125
Apollo Client 128
Apollo Client with React 128
Project Setup 129
Configure Apollo Client 129
The Query Component 131
The Mutation Component 135
Authorization 137
Authorizing the User 137
Identifying the User 141
Working with the Cache 143
Fetch Policies 143
Persisting The Cache 144
Updating the Cache 145
7 GraphQL in the Real World 149
Subscriptions 150
Working with Subscriptions 150
Consuming Subscriptions 156
Uploading Files 160
Handling Uploads on the Server 160
Posting a New Photo with Apollo Client 162
Security 168
Request Timeouts 169
Data Limitations 169
Limiting Query Depth 170
Limiting Query Complexity 172
Apollo Engine 174
Taking the Next Steps 175
Incremental Migration 175
Schema-First Development 176
GraphQL Events 178
Community 179
Community Slack Channels 180
Index 181