-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphql-api.html
More file actions
711 lines (701 loc) · 36.9 KB
/
graphql-api.html
File metadata and controls
711 lines (701 loc) · 36.9 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>APIs — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="APIs — Ruby on Rails Guides" />
<meta name="description" content="APIsAfter working through this guide you will: know about the thinking behind GraphQL APIs be able to configure your existing Rails App to offer a GraphQL API" />
<meta property="og:description" content="APIsAfter working through this guide you will: know about the thinking behind GraphQL APIs be able to configure your existing Rails App to offer a GraphQL API" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<h1>APIs</h1><p>After working through this guide you will:</p>
<ul>
<li>know about the thinking behind GraphQL APIs</li>
<li>be able to configure your existing Rails App to offer a GraphQL API</li>
</ul>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#api-styles">API Styles</a>
<ul>
<li><a href="#what-is-an-api-questionmark">What is an API?</a></li>
<li><a href="#soap-rest-and-graphql">SOAP, REST and GraphQL</a></li>
<li><a href="#api-layer-is-a-separate-layer">API layer is a separate layer</a></li>
</ul></li>
<li><a href="#graphql">GraphQL</a>
<ul>
<li><a href="#query-language">Query Language</a></li>
<li><a href="#introspection-and-playground">Introspection and Playground</a></li>
<li><a href="#types">Types</a></li>
<li><a href="#how-the-server-handles-the-request">How the server handles the request</a></li>
</ul></li>
<li><a href="#using-rails-to-build-a-graphql-api">Using Rails to build a GraphQL API</a>
<ul>
<li><a href="#setup">Setup</a></li>
<li><a href="#generating-a-type-from-a-model">Generating a Type from a Model</a></li>
<li><a href="#defining-a-query">Defining a Query</a></li>
<li><a href="#relationships-between-models">Relationships between models</a></li>
<li><a href="#more-types">More Types</a></li>
</ul></li>
<li><a href="#a-lot-more-to-learn">A lot more to learn</a>
<ul>
<li><a href="#see-also">See Also</a></li>
</ul></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_graphql-api.html#/0'>◻</a></p>
<h2 id="api-styles"><a class="anchorlink" href="#api-styles"><span>1</span> API Styles</a></h2></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_graphql-api.html#/1'>◻</a></p>
<h3 id="what-is-an-api-questionmark"><a class="anchorlink" href="#what-is-an-api-questionmark"><span>1.1</span> What is an API?</a></h3><p>API stands for "Application Programming Interface". It is a set of clearly defined methods
of communication with a software component. So the objects and methods exposed by a library
form an API.</p><p>In Web development the acronym API is most
commonly used when the software component in question runs on a different server on the
Internet and is accessed via HTTP.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_graphql-api.html#/2'>◻</a></p>
<h3 id="soap-rest-and-graphql"><a class="anchorlink" href="#soap-rest-and-graphql"><span>1.2</span> SOAP, REST and GraphQL</a></h3><p>Currently three main API styles are used on the Web:</p>
<ul>
<li>SOAP, designed 1998 at Microsoft, uses XML and POST requests to make "remote procedure calls"</li>
<li>REST, described in 2000, uses different HTTP Methods and Status Messages to access "resources"</li>
<li>GraphQL, released 2015 by Facebook, uses POST requests, it's own query language and JSON</li>
</ul>
<p>This Guide is concerned with GraphQL, there is a second guide for <a href="/rest-api.html">REST</a>. SOAP
is rearely offered with Rails, but there is a <a href="https://github.com/savonrb/savon">soap client</a> in
ruby.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_graphql-api.html#/3'>◻</a></p>
<h3 id="api-layer-is-a-separate-layer"><a class="anchorlink" href="#api-layer-is-a-separate-layer"><span>1.3</span> API layer is a separate layer</a></h3><p>Please note that any of the API styles can be used
with any backend, frontend, persistance layers:</p>
<ul>
<li>You can build a REST in front of a PHP backend using MongoDB as the database and use it from a frontend written with jQuery.</li>
<li>You can build a GraphQL API for a Rails backend using MySQL as the database and build the frontend with React.</li>
</ul>
<p>That's kind of the point of an API: to allow different technologies
on both sides of the API.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-4' href='slides_graphql-api.html#/4'>◻</a></p>
<h2 id="graphql"><a class="anchorlink" href="#graphql"><span>2</span> GraphQL</a></h2><p>REST and SOAP APIs are fixed interfaces: each api call has a fixed set of
arguments and returns a fixed data structure. All decisions are made when
specifying the API.</p><p>GraphQL shifts some decisions to the client: through a Query Language the client
can request data in the specific shape it needs, within the limits that the
API sets.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-5' href='slides_graphql-api.html#/5'>◻</a></p>
<h3 id="query-language"><a class="anchorlink" href="#query-language"><span>2.1</span> Query Language</a></h3><p>A simple example with two models: a project has many URLs. when querying
through GraphQL you can request different attributes of both models:</p><p><img src="images/two-queries.gif" alt="1:n relationship in the database"></p><p>Notice how the resulting JSON data mirrors the structure of the query.
The query language is used for both Queries (getting data) and Mutations (changing
data).</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-6' href='slides_graphql-api.html#/6'>◻</a></p>
<h3 id="introspection-and-playground"><a class="anchorlink" href="#introspection-and-playground"><span>2.2</span> Introspection and Playground</a></h3><p>A GraphQL API can always be queried to give information
about the possible queries, mutations and types. This is called
introspection.</p><p>The GraphQL Playground is a Web App that enables you to
make Queries and Mutations. It uses introspection to display
documentation and to offer autocompletion:</p>
<video controls="" autoplay="">
<source src="images/autocomplete.mov" type="video/quicktime">
<source src="images/autocomplete.mp4" type="video/mp4">

</video>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-7' href='slides_graphql-api.html#/7'>◻</a></p>
<h3 id="types"><a class="anchorlink" href="#types"><span>2.3</span> Types</a></h3><p>A GraphQL delivers JSON, which only has objects, arrays, strings, numbers and null
as types. But GraphQL itself can build a more detailed type system and check
these types in queries and mutations.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-8' href='slides_graphql-api.html#/8'>◻</a></p>
<h3 id="how-the-server-handles-the-request"><a class="anchorlink" href="#how-the-server-handles-the-request"><span>2.4</span> How the server handles the request</a></h3>
<ul>
<li>parse the query</li>
<li>validate with schema</li>
<li>resolve data</li>
<li>convert response to JSON</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-9' href='slides_graphql-api.html#/9'>◻</a></p>
<h2 id="using-rails-to-build-a-graphql-api"><a class="anchorlink" href="#using-rails-to-build-a-graphql-api"><span>3</span> Using Rails to build a GraphQL API</a></h2></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-10' href='slides_graphql-api.html#/10'>◻</a></p>
<h3 id="setup"><a class="anchorlink" href="#setup"><span>3.1</span> Setup</a></h3><p>To add a GraphQL API to an existing Rails app you need just two gems:</p><div class="interstitial code">
<pre><code class="highlight plaintext">gem 'graphql'
group :development do
gem 'graphql-rails-generators'
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="gem 'graphql'
group :development do
gem 'graphql-rails-generators'
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-11' href='slides_graphql-api.html#/11'>◻</a></p>
<h4 id="generator"><a class="anchorlink" href="#generator"><span>3.1.1</span> generator</a></h4><p>The rest of the setup is handled by a generator added by the <code>graphql</code> gem:</p><div class="interstitial code">
<pre><code class="highlight plaintext">rails generate graphql:install
exist app/graphql/types
create app/graphql/types/.keep
create app/graphql/portfolio_relaunch_schema.rb
create app/graphql/types/base_object.rb
create app/graphql/types/base_argument.rb
create app/graphql/types/base_field.rb
create app/graphql/types/base_enum.rb
create app/graphql/types/base_input_object.rb
create app/graphql/types/base_interface.rb
create app/graphql/types/base_scalar.rb
create app/graphql/types/base_union.rb
create app/graphql/types/query_type.rb
add_root_type query
create app/graphql/mutations
create app/graphql/mutations/.keep
create app/graphql/mutations/base_mutation.rb
create app/graphql/types/mutation_type.rb
add_root_type mutation
create app/controllers/graphql_controller.rb
route post "/graphql", to: "graphql#execute"
gemfile graphiql-rails
route graphiql-rails
Gemfile has been modified, make sure you `bundle install`
</code></pre>
<button class="clipboard-button" data-clipboard-text="rails generate graphql:install
exist app/graphql/types
create app/graphql/types/.keep
create app/graphql/portfolio_relaunch_schema.rb
create app/graphql/types/base_object.rb
create app/graphql/types/base_argument.rb
create app/graphql/types/base_field.rb
create app/graphql/types/base_enum.rb
create app/graphql/types/base_input_object.rb
create app/graphql/types/base_interface.rb
create app/graphql/types/base_scalar.rb
create app/graphql/types/base_union.rb
create app/graphql/types/query_type.rb
add_root_type query
create app/graphql/mutations
create app/graphql/mutations/.keep
create app/graphql/mutations/base_mutation.rb
create app/graphql/types/mutation_type.rb
add_root_type mutation
create app/controllers/graphql_controller.rb
route post "/graphql", to: "graphql#execute"
gemfile graphiql-rails
route graphiql-rails
Gemfile has been modified, make sure you `bundle install`
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-12' href='slides_graphql-api.html#/12'>◻</a></p>
<h4 id="playground-is-available"><a class="anchorlink" href="#playground-is-available"><span>3.1.2</span> playground is available</a></h4><p>After running bundle and restarting the server you can
access the graphql playground at <code>http://localhost:3000/graphiql</code>:
in the left pane you can enter a query, run it, and see the
result in the middle pane. The right pane contains documentation
that was created automatically.</p><p><img src="images/graphql-playground.png" alt="GraphQL Playground"></p><p>We have no Queries to run yet.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-13' href='slides_graphql-api.html#/13'>◻</a></p>
<h3 id="generating-a-type-from-a-model"><a class="anchorlink" href="#generating-a-type-from-a-model"><span>3.2</span> Generating a Type from a Model</a></h3><p>As a first step we can
use a generator to create a type from an existing model:</p><div class="interstitial code">
<pre><code class="highlight plaintext">rails generate gql:model_type Project
create app/graphql/types/project_type.rb
</code></pre>
<button class="clipboard-button" data-clipboard-text="rails generate gql:model_type Project
create app/graphql/types/project_type.rb
">Copy</button>
</div>
<p>Now we can edit the type to fit our needs, for example
we can remove attributes that should never be public:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class ProjectType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: false
field :publicationdate, GraphQL::Types::ISO8601Date, null: false
field :membership, Int, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :slug, String, null: false
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class ProjectType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: false
field :publicationdate, GraphQL::Types::ISO8601Date, null: false
field :membership, Int, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :slug, String, null: false
end
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-14' href='slides_graphql-api.html#/14'>◻</a></p>
<h3 id="defining-a-query"><a class="anchorlink" href="#defining-a-query"><span>3.3</span> Defining a Query</a></h3><p>The next level up we come to the query. There is already
a dummy Query in <code>app/graphql/types/query_type.rb</code></p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class QueryType < Types::BaseObject
# Add root-level fields here.
# They will be entry points for queries on your schema.
# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World!"
end
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class QueryType < Types::BaseObject
# Add root-level fields here.
# They will be entry points for queries on your schema.
# TODO: remove me
field :test_field, String, null: false,
description: "An example field added by the generator"
def test_field
"Hello World!"
end
end
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-15' href='slides_graphql-api.html#/15'>◻</a></p>
<h4 id="declare-the-query-and-implement-the-resolver"><a class="anchorlink" href="#declare-the-query-and-implement-the-resolver"><span>3.3.1</span> Declare the Query and implement the resolver</a></h4><p>We can replace this with a query that returns a list of all projects:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class QueryType < Types::BaseObject
field :all_projects, [ProjectType],
null: false,
description: "a list of all publicly visible projects"
def all_projects
Project.public.all
end
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class QueryType < Types::BaseObject
field :all_projects, [ProjectType],
null: false,
description: "a list of all publicly visible projects"
def all_projects
Project.public.all
end
end
end
">Copy</button>
</div>
<p>We can now run the query in the playground. Your query will be autocompleted.</p>
<video controls="" autoplay="">
<source src="images/autocomplete.mov" type="video/quicktime">
<source src="images/autocomplete.mp4" type="video/mp4">

</video>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-16' href='slides_graphql-api.html#/16'>◻</a></p>
<h4 id="queries-with-arguments"><a class="anchorlink" href="#queries-with-arguments"><span>3.3.2</span> Queries with arguments</a></h4><p>If a query needs arguments you have do declare
them in the <code>fields</code> declaration and then handle
them in the method:</p><div class="interstitial code">
<pre><code class="highlight plaintext"> field :project, ProjectType, null: true do
argument :id, ID, required: true
end
def project(id:)
Project.visible.find(id)
end
</code></pre>
<button class="clipboard-button" data-clipboard-text=" field :project, ProjectType, null: true do
argument :id, ID, required: true
end
def project(id:)
Project.visible.find(id)
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-17' href='slides_graphql-api.html#/17'>◻</a></p>
<h3 id="relationships-between-models"><a class="anchorlink" href="#relationships-between-models"><span>3.4</span> Relationships between models</a></h3></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-18' href='slides_graphql-api.html#/18'>◻</a></p>
<h4 id="create-type-for-a-second-model"><a class="anchorlink" href="#create-type-for-a-second-model"><span>3.4.1</span> Create type for a second model</a></h4><p><img src="images/graphql-project-has-many-urls.png" alt="1:n relationship in the database"></p><p>generate the UrlType automatically from the model:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
end
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-19' href='slides_graphql-api.html#/19'>◻</a></p>
<h4 id="create-queries-for-the-second-model"><a class="anchorlink" href="#create-queries-for-the-second-model"><span>3.4.2</span> create queries for the second model</a></h4><p>add the queries <code>all_urls</code> and <code>url</code> to <code>app/graphql/types/query_type.rb</code></p><p>now you can use those queries in the playground</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-20' href='slides_graphql-api.html#/20'>◻</a></p>
<h4 id="add-field-for-relationship-to-type"><a class="anchorlink" href="#add-field-for-relationship-to-type"><span>3.4.3</span> add field for relationship to type</a></h4><p>add the field <code>urls</code> to <code>app/graphql/types/project_type.rb</code>:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class ProjectType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: false
field :publicationdate, GraphQL::Types::ISO8601Date, null: false
field :membership, Int, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :slug, String, null: false
field :urls, [ UrlType ], null: false
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class ProjectType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: false
field :publicationdate, GraphQL::Types::ISO8601Date, null: false
field :membership, Int, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :slug, String, null: false
field :urls, [ UrlType ], null: false
end
end
">Copy</button>
</div>
<p>now you can query for Urls through their project:</p><p><img src="images/graphql-query-relationship.png" alt="query a 1:n relationship with graphql"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-21' href='slides_graphql-api.html#/21'>◻</a></p>
<h4 id="inverse-relationship"><a class="anchorlink" href="#inverse-relationship"><span>3.4.4</span> inverse relationship</a></h4><p>what would you need to do, to make a query
from Url to Project possible?</p><div class="interstitial code">
<pre><code class="highlight plaintext">{
url(id:2077){
id
url
urlType
title
project {
title
}
}
}
</code></pre>
<button class="clipboard-button" data-clipboard-text="{
url(id:2077){
id
url
urlType
title
project {
title
}
}
}
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-22' href='slides_graphql-api.html#/22'>◻</a></p>
<h4 id="implementation-of-the-inverse-relationship"><a class="anchorlink" href="#implementation-of-the-inverse-relationship"><span>3.4.5</span> implementation of the inverse relationship</a></h4><p>Answer: add a field project to UrlType:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
field :project, ProjectType, null: false
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, String, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
field :project, ProjectType, null: false
end
end
">Copy</button>
</div>
<p><img src="images/graphql-query-belongs-to.png" alt="query a 1:n relationship with graphql (reverse)"></p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-23' href='slides_graphql-api.html#/23'>◻</a></p>
<h3 id="more-types"><a class="anchorlink" href="#more-types"><span>3.5</span> More Types</a></h3><p>In Ruby and JavaScript we often use Strings to store all
kinds of values. In the example
above, <code>Url.url_type</code> only has three possible values: 'Link','Repository','Award'.</p><p>GraphQL comes with an Enum Type that we can use for that</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-24' href='slides_graphql-api.html#/24'>◻</a></p>
<h4 id="define-the-enum-type"><a class="anchorlink" href="#define-the-enum-type"><span>3.5.1</span> define the Enum Type:</a></h4><div class="interstitial code">
<pre><code class="highlight plaintext">rails g graphql:enum UrlTypeEnum Link Repository Award
create app/graphql/types/url_type_enum_type.rb
</code></pre>
<button class="clipboard-button" data-clipboard-text="rails g graphql:enum UrlTypeEnum Link Repository Award
create app/graphql/types/url_type_enum_type.rb
">Copy</button>
</div>
<div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class UrlTypeEnumType < Types::BaseEnum
value "Link"
value "Repository"
value "Award"
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class UrlTypeEnumType < Types::BaseEnum
value "Link"
value "Repository"
value "Award"
end
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-25' href='slides_graphql-api.html#/25'>◻</a></p>
<h4 id="using-the-enum-type"><a class="anchorlink" href="#using-the-enum-type"><span>3.5.2</span> using the Enum Type</a></h4><p>This can now be used in <code>UrlType</code>:</p><div class="interstitial code">
<pre><code class="highlight plaintext">module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, UrlTypeEnumType, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
field :project, ProjectType, null: false
end
end
</code></pre>
<button class="clipboard-button" data-clipboard-text="module Types
class UrlType < Types::BaseObject
field :id, GraphQL::Types::ID, null: false
field :title, String, null: true
field :url, String, null: true
field :url_type, UrlTypeEnumType, null: true
field :created_at, GraphQL::Types::ISO8601DateTime, null: true
field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
field :project, ProjectType, null: false
end
end
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-26' href='slides_graphql-api.html#/26'>◻</a></p>
<h4 id="types-uses-and-limitations"><a class="anchorlink" href="#types-uses-and-limitations"><span>3.5.3</span> Types: uses and limitations</a></h4><p>The data returned by a query is still JSON, and cannot contain
enums, only Strings.</p><p><img src="images/graphql-enum.png" alt="querying an enum gives a string"></p><p>In the documentation you can see that
only three Strings are valid. The GraphQL API will
validate this both in Queries and in Mutations.</p></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-27' href='slides_graphql-api.html#/27'>◻</a></p>
<h2 id="a-lot-more-to-learn"><a class="anchorlink" href="#a-lot-more-to-learn"><span>4</span> A lot more to learn</a></h2><p>This guide has not touched on:</p>
<ul>
<li>mutations</li>
<li>filtering, searching</li>
<li>pagination</li>
<li>resolving graphql queries that are not directly mapped to models</li>
<li>...</li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-28' href='slides_graphql-api.html#/28'>◻</a></p>
<h3 id="see-also"><a class="anchorlink" href="#see-also"><span>4.1</span> See Also</a></h3>
<ul>
<li><a href="https://www.howtographql.com/graphql-ruby/0-introduction/">GraphQL Guides: Ruby </a></li>
<li><a href="https://pragmaticstudio.com/tutorials/what-is-graphql">Pragmatic Tutorial: GraphQL?</a> (see also part 2 + 3)</li>
<li><a href="https://www.apollographql.com/">Apollo Client + Server</a> in JavaScript</li>
<li><a href="https://api-platform.com/">API Platform</a> to build REST + GraphQL APIs in PHP</li>
<li><a href="https://wundergraph.com/blog/graphql_in_production_analyzing_public_graphql_apis_1_twitch_tv">Analyzing the Twitch.tv GraphQL API</a></li>
</ul>
</div>
</div>
</div>
</main>
<hr class="hide" />
<footer id="page_footer">
<div class="wrapper">
<p class="copyright">published under <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/at/deed.de">creative commons by-nc-sa</a> in 2012-2025 by <a href="https://brigitte-jellinek.at">Brigitte Jellinek</a>.
</p>
<p>If you want to contribute: <a href="https://github.com/backend-development/backend-development-textbook/fork">fork the source on github</a>
</p>
<p>Favicon "backend development" by Arkinasi from <a href="https://thenounproject.com/browse/icons/term/backend-development/" target="_blank" title="backend development Icons">Noun Project</a> (CC BY 3.0)</p>
</div>
</footer>
</body>
</html>