File size: 6,724 Bytes
08c8a6d |
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
graphql
#graphql
Table of Contents
About
Chapter 1: Getting started with graphql
Remarks
When should I use GraphQL?
Implementations
Client Libraries
Dev Tools
Versions
Examples
Server Installation & Implementation
GraphQL.js
Example Server
Server Middleware Alternatives
GraphQL Query Language (GQL)
Query for All Film Titles from Star Wars Schema
Query for Planet Residents to Star Wars film "A New Hope"
Schema Definition
Schema Object Type
Schema Query
Query Nick's Friend's Friend's Friends
Credits
1
2
2
2
2
2
2
3
3
3
3
3
4
4
4
6
8
8
8
8
10
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: graphql
It is an unofficial and free graphql ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official graphql.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to info@zzzprojects.com
https://riptutorial.com/
1
Chapter 1: Getting started with graphql
Remarks
GraphQL is a query language for APIs and a runtime for fulfilling those queries with
your existing data. GraphQL provides a complete and understandable description of
the data in your API, gives clients the power to ask for exactly what they need and
nothing more, makes it easier to evolve APIs over time, and enables powerful
developer tools.
When should I use GraphQL?
GraphQL is intended to be the outward facing HTTP API for any kind of application but it is most
powerful when the data is normalized with highly interconnected Collections|Tables|Nodes. The
GraphQL Query Language (GQL) is designed to project interconnected data in a very intuitive and
flexible way.
Implementations
GraphQL itself is a Spec and is implemented by a number of different programming languages.
These are the most popular supported languages
•
•
•
•
•
•
•
•
•
•
GraphQL.js
graphql-ruby
Graphene (Python)
Sangria (Scala)
graphql-java
graphql-clj (Clojure)
graphql-go
graphql-php
graphql-dotnet (C# / .Net)
absinthe (Elixir)
Client Libraries
Running queries from the client side can be done with any HTTP client but client side libraries can
be very beneficial.
•
•
•
Relay
Apollo
Lokka
https://riptutorial.com/
2
Dev Tools
•
•
•
graphiql /ˈɡrafək(ə)l/ - An interactive in-browser GraphQL IDE.
libgraphqlparser - A GraphQL query language parser in C++ with C and C++ APIs.
GraphQL Language Service - An interface for building GraphQL language services for IDEs
(diagnostics, autocomplete etc).
Versions
Version Release Date
v0.5.0
2016-04-08
v0.6.0
2016-05-10
v0.6.1
2016-07-07
v0.6.2
2016-07-21
v0.7.0
2016-08-26
v0.7.1
2016-09-29
v0.7.2
2016-10-10
v0.8.0
2016-11-10
v0.8.1
2016-11-11
v0.8.2
2016-11-16
Examples
Server Installation & Implementation
GraphQL.js
GraphQL.js is a JavaScript reference implementation for GraphQL. You can install it via npm:
•
•
Initialize npm in your project if you have not done so already: npm init
Install GraphQL.js from npm: npm install --save graphql
Example Server
var { graphql, buildSchema } = require('graphql');
https://riptutorial.com/
3
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});
Server Middleware Alternatives
•
•
•
Express GraphQL Middleware
Hapi GraphQL Middleware
Koa GraphQL Middleware
GraphQL Query Language (GQL)
Instead of defining URL endpoints for each data resource, in GraphQL you define a single
endpoint that accepts GraphQL queries. Unlike traditional database query languages, GraphQL
Query Language (GQL) is a projection of data returned by a root level query. The GraphQL
schema will define the data model and root level queries and mutations. GQL executes those
queries and defines what data to return.
Query for All Film Titles from Star Wars Schema
https://riptutorial.com/
4
GraphiQL Query
https://riptutorial.com/
5
Query for Planet Residents to Star Wars film "A New Hope"
https://riptutorial.com/
6
https://riptutorial.com/
7
GraphiQL Query
Notice how the root query film() takes in the id parameter of the film "A New Hope" and the
projection from GQL returns the title, planets connected to the film, and then resident names of the
planets. Check out the Star Wars GraphiQL site to experiment with querying in GraphQL.
Schema Definition
In GraphQL the Schema defines the root execution queries and mutations as well as the types for
your data.
Schema Object Type
The Person type has two fields, one is a standard Scalar type and the other represents a
relationship to a list of other Person types that are 'friends'. Linking other types is what makes
GraphQL so powerful. Now in GraphQL Query Language (GQL), the client can traverse the friends
graph without any additional code or advanced querying.
type Person {
id: ID
name: String
friends: [Person]
}
Schema Query
The person query looks up a single person by it's id. This is the entry point to your data for clients
using GQL.
type Query {
person(id: ID!): Person
}
Query Nick's Friend's Friend's Friends
Now that we have a Person type and a person root query we can look up a person and as many
degrees of separation we want of the person's friends network.
query {
person(id: 'nick'){
id
name
friends{
id
name
friends{
id
name
friends{
https://riptutorial.com/
8
id
name
}
}
}
}
}
Read Getting started with graphql online: https://riptutorial.com/graphql/topic/7649/getting-started-
with-graphql
https://riptutorial.com/
9
Credits
S.
No
1
Chapters
Contributors
Getting started with
graphql
Community, jengeb, Nikordaris
https://riptutorial.com/
10
|