-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphqlsetup.html
More file actions
594 lines (558 loc) · 39.6 KB
/
graphqlsetup.html
File metadata and controls
594 lines (558 loc) · 39.6 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
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<title>Marstech Buzz</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="description" content="Marstech Buzz is a technology blog with a focus on the latest technology updates and how it can be used to make the world a better place." />
<meta name="keywords" content="Marstech, HTML, CSS, JavaScript, PHP,Blog, eco-friendly, Blockchain, Technology, Coding, web-development, website, app, android, ios" />
<meta name="author" content="Marstech" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="images/favicon.svg" />
<link href='https://fonts.googleapis.com/css2?family=Nunito&display=swap' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href='css/clear.css' />
<link rel="stylesheet" type="text/css" href='css/common.css' />
<link rel="stylesheet" type="text/css" href='css/font-awesome.min.css' />
<link rel="stylesheet" type="text/css" href='css/carouFredSel.css' />
<link rel="stylesheet" type="text/css" href='css/sm-clean.css' />
<link rel="stylesheet" type="text/css" href='style.css' />
<!--[if lt IE 9]>
<script src="js/html5.js"></script>
<![endif]-->
</head>
<body class="single-post">
<!-- Preloader Gif -->
<table class="doc-loader">
<tbody>
<tr>
<td>
<img src="images/logo.gif" alt="Loading...">
</td>
</tr>
</tbody>
</table>
<!-- Left Sidebar -->
<div id="sidebar" class="sidebar">
<div class="menu-left-part">
<div class="search-holder">
<label>
<input type="search" class="search-field" placeholder="Type here to search..." value="" name="s"
title="Search for:">
</label>
</div>
<div class="site-info-holder">
<h1 class="site-title">Marstech Buzz</h1>
<p class="site-description">
Marstech Buzz is a technology blog with a focus on the latest technology updates and how it can be
used to make the world a better place.
</p>
</div>
<nav id="header-main-menu">
<ul class="main-menu sm sm-clean">
<li><a href="index.html">Home</a></li>
<li><a href="innovationhub.html">Innovation Hub</a></li>
<li><a href="about.html">About</a></li>
<li><a href="allcategories.html">All Categories</a></li>
<li><a href="allposts.html">All Posts</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
<footer>
<div class="footer-info">
More information about Marstech click <a href="http://www.marstech.org.in/">here</a>.
</div>
</footer>
</div>
<div class="menu-right-part">
<div class="logo-holder">
<a href="index.html">
<img src="images/logo.svg" alt="Marstech Buzz">
</a>
</div>
<div class="toggle-holder">
<div id="toggle">
<div class="menu-line"></div>
</div>
</div>
<div class="social-holder">
<div class="social-list">
<a href="https://instagram.com/_marstech_?igshid=17j16ujd711ki"><i class="fa fa-instagram"></i></a>
<a href="https://www.linkedin.com/in/marstech-3-8936b6188"><i class="fa fa-linkedin"></i></a>
<a href="https://www.facebook.com/marstech.co.in"><i class="fa fa-facebook"></i></a>
<a href="error404.html"><i class="fa fa-twitter"></i></a>
<a href="error404.html"><i class="fa fa-youtube-play"></i></a>
</div>
</div>
<div class="fixed scroll-top"><i class="fa fa-caret-square-o-up" aria-hidden="true"></i></div>
</div>
<div class="clear"></div>
</div>
<!-- Single Content -->
<div id="content" class="site-content center-relative">
<div class="single-post-wrapper content-1070 center-relative">
<article class="center-relative">
<h1 class="entry-title">
Build a GraphQL Server with Spring Boot in 3 minutes
</h1>
<div class="post-info content-660 center-relative">
<div class="cat-links">
<ul>
<li><a href="techblog.html">Technology</a></li>
</ul>
</div>
<div class="entry-date published">August 14, 2020</div>
<br>
<div class="clear"></div>
<div>
<br>
<br>
<img src="images/gqlmeme.jpeg" alt="">
</div>
</div>
<div class="entry-content">
<div class="content-wrap content-660 center-relative">
<p>This article focuses on how to set up a GraphQL server from scratch, utilizing Spring Boot.
</p>
<br>
<p>There are many different libraries with different approaches for the same. However, the
library I used here has a mature Spring Boot support and is very easy to use. So this may be
one of your fastest options.
</p>
<br>
<p>Also, This article focuses only on the setup and configuration part and does not cover up
basics about GraphQL. I suggest you should read <a style="text-decoration:underline;"
href="https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/">official
documentation</a> to read basics and
divulge more into GraphQL.
</p>
<br>
<p>In this tutorial, we are going to use a project called <code
class="code_highlight">graphql-tools</code> to integrate GraphQL
with Spring Boot. According to the <a style="text-decoration:underline;"
href="https://github.com/graphql-java-kickstart/graphql-java-tools">project GitHub page
readme</a> , <code class="code_highlight">graphql-tools</code> “allows you to
use the GraphQL schema language to build your <a style="text-decoration:underline;"
href="https://github.com/graphql-java/graphql-java">graphql-java</a> schema and allows
you to BYOO
(bring your own object) to fill in the implementations.”
</p>
<br>
<p>It works great if you have, or want to have, plain old java objects (POJO) that define your
data model. Which I do.
</p>
<br>
<p> <a style="font-weight: 600;">Note:</a> <i>This tutorial assumes that you are familiar with
the Java programming language, the
Spring Boot framework. No prior GraphQL experience is required.</i>
</p>
<br>
<div class="clear"></div>
</div>
<div class="content-wrap content-660 center-relative ">
<h3 class="entry-title">
Let’s get to it
</h3>
<p>Assuming you already have a spring boot project setup and ready to use.
<ol style="font-size: 95%; margin-left: 5%;">
<li>Add the dependency in your build.gradle file or pom.xml</li>
<li>Create a GraphQL Schema file</li>
<li>Define Resolver for fields in the schema</li>
<li>Visualize and run through GraphIQL</li>
</ol>
</p>
<br>
<h3 class="entry-title">
1. Add the dependency in your build.gradle file or pom.xml
</h3>
<p>This Example uses Gradle so add the below dependencies to your <code
class="code_highlight">build.gradle</code> file.
</p>
<br>
<p>Keep in mind that we are using provider <code
class="code_highlight">com.graphql-java-kickstart</code> instead of <code
class="code_highlight">com.graphql-java</code> since the <a
style="text-decoration:underline;"
href="https://www.graphql-java.com/blog/moving-projects/"> repository is moved </a> to
avoid confusion.
</p>
<br>
<figure>
<div class="gist-file" wfd-id="2">
<div class="gist-data" wfd-id="4">
<div class="js-gist-file-update-container js-task-list-container file-box"
wfd-id="5">
<div id="file-build-gradle" class="file " wfd-id="6">
<div itemprop="text" class="Box-body p-0 blob-wrapper data type-gradle "
wfd-id="7" style="overflow-x:auto;">
<table class="highlight tab-size js-file-line-container"
data-tab-size="8" data-paste-markdown-skip="">
<tbody>
<tr>
<td id="file-build-gradle-L1"
class="blob-num js-line-number" data-line-number="1">
</td>
<td id="file-build-gradle-LC1"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="19">..</span>.</td>
</tr>
<tr>
<td id="file-build-gradle-L2"
class="blob-num js-line-number" data-line-number="2">
</td>
<td id="file-build-gradle-LC2"
class="blob-code blob-code-inner js-file-line"><span
class="pl-en" wfd-id="18">dependencies</span> {</td>
</tr>
<tr>
<td id="file-build-gradle-L3"
class="blob-num js-line-number" data-line-number="3">
</td>
<td id="file-build-gradle-LC3"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="17">..</span>.</td>
</tr>
<tr>
<td id="file-build-gradle-L4"
class="blob-num js-line-number" data-line-number="4">
</td>
<td id="file-build-gradle-LC4"
class="blob-code blob-code-inner js-file-line">compile <span class="pl-s" wfd-id="14"><span class="pl-pds" wfd-id="16">'</span>com.graphql-java-kickstart:graphql-spring-boot-starter:5.0.5<span class="pl-pds" wfd-id="15">'</span></span></td>
</tr>
<tr>
<td id="file-build-gradle-L5"
class="blob-num js-line-number" data-line-number="5">
</td>
<td id="file-build-gradle-LC5"
class="blob-code blob-code-inner js-file-line">compile <span class="pl-s" wfd-id="11"><span class="pl-pds" wfd-id="13">'</span>com.graphql-java-kickstart:graphiql-spring-boot-starter:5.0.5<span class="pl-pds" wfd-id="12">'</span></span></td>
</tr>
<tr>
<td id="file-build-gradle-L6"
class="blob-num js-line-number" data-line-number="6">
</td>
<td id="file-build-gradle-LC6"
class="blob-code blob-code-inner js-file-line">compile <span class="pl-s" wfd-id="8"><span class="pl-pds" wfd-id="10">'</span>com.graphql-java-kickstart:graphql-java-tools:5.3.4<span class="pl-pds" wfd-id="9">'</span></span></td>
</tr>
<tr>
<td id="file-build-gradle-L7"
class="blob-num js-line-number" data-line-number="7">
</td>
<td id="file-build-gradle-LC7"
class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<figcaption style="color: rgba(117, 117, 117, 1); text-align: center; ">Dependency file
</figcaption>
</figure>
<br>
<h3 class="entry-title">
2. Create a Schema file
</h3>
<p>Create a file called <code class="code_highlight">schema.graphqls</code> in the resources
folder, with the following content:
</p>
<br>
<figure>
<div class="gist-file" wfd-id="4">
<div class="js-gist-file-update-container js-task-list-container file-box" wfd-id="5">
<div id="file-schema-graphqls" class="file " wfd-id="6">
<div itemprop="text" class="Box-body p-0 blob-wrapper data type-graphql "
wfd-id="7" style="overflow-x:auto;">
<table class="highlight tab-size js-file-line-container" data-tab-size="8"
data-paste-markdown-skip="">
<tbody>
<tr>
<td id="file-schema-graphqls-L1" class="blob-num js-line-number"
data-line-number="1"></td>
<td id="file-schema-graphqls-LC1"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="16">schema</span> {</td>
</tr>
<tr>
<td id="file-schema-graphqls-L2" class="blob-num js-line-number"
data-line-number="2"></td>
<td id="file-schema-graphqls-LC2"
class="blob-code blob-code-inner js-file-line"> <span
class="pl-v" wfd-id="15">query</span> : <span
class="pl-c1" wfd-id="14">Query</span></td>
</tr>
<tr>
<td id="file-schema-graphqls-L3" class="blob-num js-line-number"
data-line-number="3"></td>
<td id="file-schema-graphqls-LC3"
class="blob-code blob-code-inner js-file-line">}</td>
</tr>
<tr>
<td id="file-schema-graphqls-L4" class="blob-num js-line-number"
data-line-number="4"></td>
<td id="file-schema-graphqls-LC4"
class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-schema-graphqls-L5" class="blob-num js-line-number"
data-line-number="5"></td>
<td id="file-schema-graphqls-LC5"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="13">type</span> <span class="pl-c1"
wfd-id="12">Query</span> {</td>
</tr>
<tr>
<td id="file-schema-graphqls-L6" class="blob-num js-line-number"
data-line-number="6"></td>
<td id="file-schema-graphqls-LC6"
class="blob-code blob-code-inner js-file-line"> <span
class="pl-v" wfd-id="11">user</span>(id : ID) : <span
class="pl-c1" wfd-id="10">User</span></td>
</tr>
<tr>
<td id="file-schema-graphqls-L7" class="blob-num js-line-number"
data-line-number="7"></td>
<td id="file-schema-graphqls-LC7"
class="blob-code blob-code-inner js-file-line">}</td>
</tr>
<tr>
<td id="file-schema-graphqls-L8" class="blob-num js-line-number"
data-line-number="8"></td>
<td id="file-schema-graphqls-LC8"
class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-schema-graphqls-L9" class="blob-num js-line-number"
data-line-number="9"></td>
<td id="file-schema-graphqls-LC9"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="9">type</span> <span class="pl-c1"
wfd-id="8">User</span> {</td>
</tr>
<tr>
<td id="file-schema-graphqls-L10"
class="blob-num js-line-number" data-line-number="10"></td>
<td id="file-schema-graphqls-LC10"
class="blob-code blob-code-inner js-file-line"> id : ID !
</td>
</tr>
<tr>
<td id="file-schema-graphqls-L11"
class="blob-num js-line-number" data-line-number="11"></td>
<td id="file-schema-graphqls-LC11"
class="blob-code blob-code-inner js-file-line"> firstName : String
</td>
</tr>
<tr>
<td id="file-schema-graphqls-L12"
class="blob-num js-line-number" data-line-number="12"></td>
<td id="file-schema-graphqls-LC12"
class="blob-code blob-code-inner js-file-line"> email : String
</td>
</tr>
<tr>
<td id="file-schema-graphqls-L13"
class="blob-num js-line-number" data-line-number="13"></td>
<td id="file-schema-graphqls-LC13"
class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<figcaption style="color: rgba(117, 117, 117, 1); text-align: center; ">Schema file
</figcaption>
</figure>
<p>
Any files ending with the extension .graphqls within your classpath are automatically
scanned and parsed.
</p>
<br>
<p> <a style="font-weight: 600;">Note:</a> <i>The file name is not important. However, the
extension must be <code class="code_highlight">.graphqls</code>, otherwise, the library
will not find it. The file should
also be present in the classpath of your project to be visible.</i>
</p>
<br>
<p>
GraphQL Java Tools automatically maps fields in your GraphQL objects to those on your java
objects. For most scalar fields, a POJO with fields and/or getter methods is enough to
describe the data to GraphQL.
</p>
<br>
<h3 class="entry-title">
3. Define your Resolver
</h3>
<p>GraphQL Java Tools lets you register Resolvers for any type. Each field on each type is
backed by the resolver.
</p>
<br>
<p>The library will first attempt to map fields to methods on the resolver before mapping them
to fields or methods on the data class.
</p>
<br>
<p>In our case, we have declared a UserResolver that implements the GraphQLQueryResolver
interface. It should also have @component or @Service Annotation.
</p>
<br>
<p>Create a UserResolver Java class with contents :
</p>
<br>
<figure>
<div class="gist-file" wfd-id="4">
<div class="js-gist-file-update-container js-task-list-container file-box" wfd-id="5">
<div id="file-userresolver-java" class="file " wfd-id="6">
<div itemprop="text" class="Box-body p-0 blob-wrapper data type-java "
wfd-id="7" style="overflow-x:auto;">
<table class="highlight tab-size js-file-line-container" data-tab-size="8"
data-paste-markdown-skip="">
<tbody>
<tr>
<td id="file-userresolver-java-L1"
class="blob-num js-line-number" data-line-number="1"></td>
<td id="file-userresolver-java-LC1"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="27">import</span> <span
class="pl-smi"
wfd-id="26">com.coxautodev.graphql.tools.GraphQLQueryResolver</span>;
</td>
</tr>
<tr>
<td id="file-userresolver-java-L2"
class="blob-num js-line-number" data-line-number="2"></td>
<td id="file-userresolver-java-LC2"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="25">import</span> <span
class="pl-smi"
wfd-id="24">org.springframework.stereotype.Component</span>;
</td>
</tr>
<tr>
<td id="file-userresolver-java-L3"
class="blob-num js-line-number" data-line-number="3"></td>
<td id="file-userresolver-java-LC3"
class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-userresolver-java-L4"
class="blob-num js-line-number" data-line-number="4"></td>
<td id="file-userresolver-java-LC4"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="23">@Autowired</span></td>
</tr>
<tr>
<td id="file-userresolver-java-L5"
class="blob-num js-line-number" data-line-number="5"></td>
<td id="file-userresolver-java-LC5"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="22">private</span> <span
class="pl-smi" wfd-id="21">UserService</span> userService;</td>
</tr>
<tr>
<td id="file-userresolver-java-L6"
class="blob-num js-line-number" data-line-number="6"></td>
<td id="file-userresolver-java-LC6"
class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-userresolver-java-L7"
class="blob-num js-line-number" data-line-number="7"></td>
<td id="file-userresolver-java-LC7"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="20">@Component</span></td>
</tr>
<tr>
<td id="file-userresolver-java-L8"
class="blob-num js-line-number" data-line-number="8"></td>
<td id="file-userresolver-java-LC8"
class="blob-code blob-code-inner js-file-line"><span
class="pl-k" wfd-id="19">public</span> <span
class="pl-k" wfd-id="18">class</span> <span
class="pl-en" wfd-id="17">UserResolver</span> <span
class="pl-k" wfd-id="16">implements</span> <span
class="pl-e" wfd-id="15">GraphQLQueryResolver</span> {
</td>
</tr>
<tr>
<td id="file-userresolver-java-L9"
class="blob-num js-line-number" data-line-number="9"></td>
<td id="file-userresolver-java-LC9"
class="blob-code blob-code-inner js-file-line"> <span
class="pl-k" wfd-id="14">public</span> <span
class="pl-smi" wfd-id="13">User</span> <span
class="pl-en" wfd-id="12">user</span>(<span
class="pl-smi" wfd-id="11">Long</span> <span
class="pl-v" wfd-id="10">id</span>) {</td>
</tr>
<tr>
<td id="file-userresolver-java-L10"
class="blob-num js-line-number" data-line-number="10"></td>
<td id="file-userresolver-java-LC10"
class="blob-code blob-code-inner js-file-line"> <span
class="pl-k" wfd-id="9">return</span> userService<span
class="pl-k" wfd-id="8">.</span>getUserById(id);</td>
</tr>
<tr>
<td id="file-userresolver-java-L11"
class="blob-num js-line-number" data-line-number="11"></td>
<td id="file-userresolver-java-LC11"
class="blob-code blob-code-inner js-file-line"> }</td>
</tr>
<tr>
<td id="file-userresolver-java-L12"
class="blob-num js-line-number" data-line-number="12"></td>
<td id="file-userresolver-java-LC12"
class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<figcaption></figcaption>
</figure>
<br>
<p>
Keep in mind that: <i style="font-weight: 600;">a resolver method will only be called if we
actually query that particular field.</i> Otherwise, it will never be called. <i
style="font-weight: 600;">This is one of the main benefits of GraphQL over REST.</i>
</p>
<br>
<h3 class="entry-title">
4. Visualize and run through GraphIql
</h3>
<p style="font-weight: 600;">Run your Spring boot Application! Voila!
</p>
<br>
<p>Access the interactive graphql playground at <br>
<a style="text-decoration:underline;"
href="http://localhost:8080/graphiql">http://localhost:8080/graphiql</a>
</p>
<br>
<img alt="Image for post" class="dr ve s t u dc ai c" width="930" height="269"
src="images/gqlgif.gif">
<br>
</div>
</div>
<div class="clear"></div>
</article>
</div>
</div>
<!--Load JavaScript-->
<script type="text/javascript" src="js/jquery.js"></script>
<script type='text/javascript' src='js/imagesloaded.pkgd.js'></script>
<script type='text/javascript' src='js/jquery.nicescroll.min.js'></script>
<script type='text/javascript' src='js/jquery.smartmenus.min.js'></script>
<script type='text/javascript' src='js/jquery.carouFredSel-6.0.0-packed.js'></script>
<script type='text/javascript' src='js/jquery.mousewheel.min.js'></script>
<script type='text/javascript' src='js/jquery.touchSwipe.min.js'></script>
<script type='text/javascript' src='js/jquery.easing.1.3.js'></script>
<script type='text/javascript' src='js/main.js'></script>
</body>
</html>