@@ -376,9 +376,29 @@ code-example(language="json").
376
376
:marked
377
377
Now for the fun part. Inside of the `add()` function, add an `addHero` mutation
378
378
using the `apollo.mutate` function as follows:
379
- + makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'add-mutation' , 'heroes.component.ts' )
379
+ + makeExample('heroes-graphql/ts/src/app/heroes.component.2. ts' , 'add-mutation' , 'heroes.component.ts' )
380
380
:marked
381
381
The mutation requires and variable and we pass it to the `mutate` function through the `variables` param.
382
+
383
+ As mentioned above, with GraphQL mutations, you specify the result you want to get back from the server.
384
+
385
+ Apollo's `mutate` function returns the result as an Observable.
386
+ The Observable returns a `mutationResult` variable that is structured
387
+ like the `ApolloQueryResult` Typescript Type, where the generic `T` type is a `Hero` type:
388
+ code-example( language ="json" ) .
389
+ type ApolloQueryResult< T> = {
390
+ data: T;
391
+ loading: boolean;
392
+ networkStatus: NetworkStatus;
393
+ };
394
+ :marked
395
+ and that's also how you reference that result variable in Typescript.
396
+ So in order the get the actual hero data being returned, you access those values like so:
397
+ + makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'access-mutation-result' , 'heroes.component.ts' )
398
+ :marked
399
+ and then push them into the `heroes` array.
400
+
401
+
382
402
.alert.is-important
383
403
:marked
384
404
Uri: Could you explain this above segment section by section?
@@ -393,11 +413,17 @@ code-example(language="json").
393
413
of what an observable is...I see we are pushing the `id` and `name` but
394
414
don't quite understand how/when. The subscribe syntax loses me. Lastly,
395
415
I don't know why we are setting the `selectedHero` to `null`.
416
+ :Kapunahele - changed above
396
417
397
418
:marked
398
419
Just like a query, the mutate function returns an Observable you can subscribe to
399
420
that handles the data you request.
400
421
422
+ At this point, your heroes existing heroes app can also add a hero:
423
+ figure.image-display
424
+ img( src ='/resources/images/cookbooks/heroes-graphql/heroes- graphql-mutation.gif' alt ="Heroes GraphQL Mutation" )
425
+
426
+
401
427
.alert.is-important
402
428
:marked
403
429
Uri: Let's say something here about how we can now add a hero. Can the
@@ -407,6 +433,7 @@ code-example(language="json").
407
433
Imagine the reader coming to this page and giving it a cursory scroll
408
434
to see how long it is in deciding whether or not to work through it.
409
435
A gif (especially) or screenshot could help them decide to do it.
436
+ ::Kapunahele - great point. added.
410
437
411
438
.l-main-section
412
439
<a id =" resources" ></a >
@@ -436,13 +463,16 @@ code-example(language="json").
436
463
Running the server in the browser helps you start using
437
464
GraphQL on your frontend without having a GraphQL backend.
438
465
If you don't have the option of running GraphQL on the server,
439
- this method makes it easier to handle multiple REST
440
- requests and join logic on the client.
466
+ this method makes it possible to still use GraphQL in your app with the
467
+ benefits for not needing to sync multiple REST requests and join logic
468
+ on the client.
441
469
442
470
.alert.is-important
443
471
:marked
444
472
Uri: Can you tell me why it makes it easier? We should say
445
473
"...logic on the client because..."
474
+ :Kapunahele - changed above
475
+
446
476
447
477
.l-sub-section
448
478
:marked
@@ -451,14 +481,15 @@ code-example(language="json").
451
481
it is almost identical to the local server in this appendix.
452
482
Everything you learn here applies to writing an actual GraphQL backend server.
453
483
454
- Additionally, there are a few GraphQL based backend-as-a-service platforms available,
455
- such as Firebase, but they are based on the GraphQL API. For help on getting
456
- up and running, see [Scaphold](https://www.scaphold.io/) and [Graphcool](https://www.graph.cool/).
484
+ Additionally, there are a few GraphQL backend-as-a-service platforms available,
485
+ similar to Firebase, but based on the GraphQL API spec.
486
+ For help on getting up and running, see [Scaphold](https://www.scaphold.io/) and [Graphcool](https://www.graph.cool/).
457
487
458
488
.alert.is-important
459
489
:marked
460
490
Uri: Can you explain why the GraphQL API makes using things like Firebase different?
461
491
There's a "but" in that sentence, but I don't know what it implies.
492
+ :Kapunahele - the word `but` was wrong. fixed and edited a bit
462
493
463
494
:marked
464
495
In order to create a GraphQL schema, you need the `graphql-tools` library.
@@ -477,7 +508,7 @@ code-example(language="sh" class="code-shell").
477
508
.alert.is-important
478
509
:marked
479
510
Uri: Could you change the use of "we" in the above comments to "you" or the imperative (command without
480
- "you")?
511
+ "you")? :Kapunahele - done.
481
512
.l-sub-section
482
513
:marked
483
514
While the schema includes the major points covered in this cookbook,
@@ -502,13 +533,16 @@ code-example(language="sh" class="code-shell").
502
533
:marked
503
534
You may have noticed some functions from `lodash` such as `lodashFind()` so don't
504
535
forget to install them from npm and import them:
536
+ code-example( language ="sh" class ="code-shell" ) .
537
+ npm install lodash --save
505
538
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-lodash' , 'in-memory-graphql.ts (imports)' )
506
539
507
540
.alert.is-important
508
541
:marked
509
542
Uri: What is the npm install command for the lodash functions?
510
543
Do we install the whole lodash library or just the functions?
511
544
(I've never used lodash before).
545
+ :Kapunahele - fixed. for ease of use I've installed all of lodash
512
546
513
547
514
548
:marked
@@ -517,7 +551,8 @@ code-example(language="sh" class="code-shell").
517
551
518
552
This mechanism makes writing simple GraphQL servers straightforward—you simply
519
553
resolve a specific type of data.
520
- This removes the coupling between frontend and backend.
554
+ This removes the coupling between frontend and backend because if you don't need to know the specific
555
+ query the client makes to create the server Implementation.
521
556
522
557
.alert.is-important
523
558
:marked
@@ -526,6 +561,7 @@ code-example(language="sh" class="code-shell").
526
561
I'm right about "This removes the coupling between frontend and backend".
527
562
I was trying to make it clearer by breaking it into smaller ideas, but
528
563
I'm not sure what those ideas should be.
564
+ :Kapunahele - expanded on that, let me know if that works
529
565
530
566
:marked
531
567
Now, connect the schema to the resolvers with the `makeExecutableSchema` function from
@@ -537,28 +573,42 @@ code-example(language="sh" class="code-shell").
537
573
In the constant `schema`, `makeExecutableSchema` has two properties,
538
574
`typeDefs` and `resolvers`. Here, you define them with the `typeDefinitions`
539
575
and `resolveFunctions` that you created earlier in `in-memory-graphql.ts`.
540
- This way, your app/GraphQL? (Uri - can you correct me here?) knows where to look
541
- for definitions and resolvers.
576
+ This way, your GraphQL server knows where to look for definitions and resolvers.
542
577
543
578
:marked
544
579
Now that you have an executable schema, execute it using the `graphql`
545
580
library and export it so you can use it with the Apollo Client.
546
581
First, `npm install`:
582
+ code-example( language ="sh" class ="code-shell" ) .
583
+ npm install graphql --save
547
584
548
585
.alert.is-important
549
586
:marked
550
587
Uri: can you provide the npm install command?
588
+ :Kapunahele - done
551
589
552
590
:marked
553
591
Next, add an import statement for `execute`.
554
592
555
593
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-graphql' , 'in-memory-graphql.ts (excerpt)' )
594
+ :marked
595
+ Now create a new `networkInterface` class and call it `InBrowserNetworkInterface`.
596
+
597
+ That class will have a `schema` property which it will initialize in the constructor.
556
598
599
+ Then create a `query` function that will recieve a query as a variable and execute that query using
600
+ `graphql` execute function against the schema property.
601
+
602
+ You send empty objects to the `rootValue` and `contextValue` arguments of the function and send the
603
+ `variables` and `operationName` arguments that are related to the query request.
604
+
605
+ Now export the new `InBrowserNetworkInterface` class in order to import it to Apollo Client.
557
606
.alert.is-important
558
607
:marked
559
608
Uri: Could you explain what's happening in the below?
560
609
This will probably take 2-3 short paragraphs. Just a
561
610
general tour from top to bottom.
611
+ :Kapunahele - let me know if that works
562
612
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'execute-and-export' , 'in-memory-graphql.ts (excerpt)' )
563
613
:marked
564
614
Now all that's left is to connect the new in-memory server to the Apollo Client configuration
@@ -578,18 +628,21 @@ code-example(language="sh" class="code-shell").
578
628
:marked
579
629
Uri: Let's add a conclusion showing what was accomplished in the doc.
580
630
Maybe something like (please add anything I've missed):
631
+ :Kapunahele - done, but not sure if its enough?
581
632
582
633
.l-main-section
583
634
:marked
584
635
## Conclusion
585
636
586
637
This cookbook covered:
587
638
639
+ - What is GraphQL and why it can benefit Angular developers.
588
640
- How to create a basic GraphQL query.
589
641
- How to create a basic GraphQL mutation.
590
642
- How to build a GraphQL server.
643
+ - Resources to dive deeper.
591
644
592
-
645
+
593
646
594
647
.l-main-section
595
648
<a id =" example" ></a >
0 commit comments