@@ -218,6 +218,13 @@ a#http-heroes
218
218
This cookbook touches on the main points of using GraphQL with Angular.
219
219
The full documentation can be found on the [Apollo Client docs website](http://dev.apollodata.com/).
220
220
221
+ :marked
222
+ The starting point for the app would be the Tour Of Heroes tutorial app at it's end state.
223
+ You can download that app [here](https://github.com/johnpapa/angular2-tour-of-heroes/archive/master.zip).
224
+
225
+ You will gradually migrate that app from using REST to using GraphQL.
226
+
227
+
221
228
.l-main-section
222
229
<a id =" installation" ></a >
223
230
:marked
@@ -384,57 +391,28 @@ code-example(language="json").
384
391
385
392
Apollo's `mutate` function returns the result as an Observable.
386
393
The Observable returns a `mutationResult` variable that is structured
387
- like the `ApolloQueryResult` Typescript Type, where the generic `T` type is a `Hero` type:
394
+ like the `ApolloQueryResult` TypeScript Type, where the generic `T` type is a `Hero` type:
388
395
code-example( language ="json" ) .
389
396
type ApolloQueryResult< T> = {
390
397
data: T;
391
398
loading: boolean;
392
399
networkStatus: NetworkStatus;
393
400
};
394
401
: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:
402
+ If this looks familiar, it's because that's also how you reference the `mutationResult` variable in TypeScript .
403
+ To access the hero data `mutationResult` returns, use dot notation to traverse `mutationResult` and assign it to a new hero object.
397
404
+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'access-mutation-result' , 'heroes.component.ts' )
398
405
:marked
399
- and then push them into the `heroes` array.
400
-
401
-
402
- .alert.is-important
403
- :marked
404
- Uri: Could you explain this above segment section by section?
405
- You've already explained the part in green, so you shouldn't
406
- have to say much about it. The rest, though, could benefit from
407
- a little guiding prose. Two or
408
- three short paragraphs should be fine. We just want the reader to
409
- understand at least the gist of what each part of the code we give
410
- them is doing. I'd explain these but have only the vaguest notion
411
- of what's going on. Pretend you are explaining it to me. I don't know what
412
- the tick marks are doing (are they like a template string?). I have a cursory understanding
413
- of what an observable is...I see we are pushing the `id` and `name` but
414
- don't quite understand how/when. The subscribe syntax loses me. Lastly,
415
- I don't know why we are setting the `selectedHero` to `null`.
416
- :Kapunahele - changed above
406
+ Once you have created the new object with the data, push it into the `heroes` array.
417
407
418
408
:marked
419
409
Just like a query, the mutate function returns an Observable you can subscribe to
420
410
that handles the data you request.
421
411
422
- At this point, your heroes existing heroes app can also add a hero:
412
+ At this point, your existing heroes app can also add a hero:
423
413
figure.image-display
424
414
img( src ='/resources/images/cookbooks/heroes-graphql/heroes- graphql-mutation.gif' alt ="Heroes GraphQL Mutation" )
425
415
426
-
427
- .alert.is-important
428
- :marked
429
- Uri: Let's say something here about how we can now add a hero. Can the
430
- reader now click their add button and their new hero be added? Can we
431
- show this in a gif or a screenshot? Showing the achievement could
432
- make the point about how beneficial GraphQL is very quickly.
433
- Imagine the reader coming to this page and giving it a cursory scroll
434
- to see how long it is in deciding whether or not to work through it.
435
- A gif (especially) or screenshot could help them decide to do it.
436
- ::Kapunahele - great point. added.
437
-
438
416
.l-main-section
439
417
<a id =" resources" ></a >
440
418
:marked
@@ -467,13 +445,6 @@ figure.image-display
467
445
benefits for not needing to sync multiple REST requests and join logic
468
446
on the client.
469
447
470
- .alert.is-important
471
- :marked
472
- Uri: Can you tell me why it makes it easier? We should say
473
- "...logic on the client because..."
474
- :Kapunahele - changed above
475
-
476
-
477
448
.l-sub-section
478
449
:marked
479
450
To read more about how to run a full GraphQL backend, see the [Apollo Server documentation](http://dev.apollodata.com/tools/).
@@ -485,12 +456,6 @@ figure.image-display
485
456
similar to Firebase, but based on the GraphQL API spec.
486
457
For help on getting up and running, see [Scaphold](https://www.scaphold.io/) and [Graphcool](https://www.graph.cool/).
487
458
488
- .alert.is-important
489
- :marked
490
- Uri: Can you explain why the GraphQL API makes using things like Firebase different?
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
493
-
494
459
:marked
495
460
In order to create a GraphQL schema, you need the `graphql-tools` library.
496
461
It allows you to write a GraphQL schema as a string and make it executable.
@@ -504,11 +469,12 @@ code-example(language="sh" class="code-shell").
504
469
and paste in the following schema:
505
470
506
471
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'graphql-schema' , 'in-memory-graphql.ts' )
472
+ :marked
473
+ The schema starts with a representions of the model of data the server exposes.
474
+ Then the schema specifies what queries are allowed on that data, followed by specifing
475
+ what mutations (actions) clients are allowed to do on the server.
476
+ The end of the schema provides the above definitions as the root types the GrpahQL server will expose.
507
477
508
- .alert.is-important
509
- :marked
510
- Uri: Could you change the use of "we" in the above comments to "you" or the imperative (command without
511
- "you")? :Kapunahele - done.
512
478
.l-sub-section
513
479
:marked
514
480
While the schema includes the major points covered in this cookbook,
@@ -537,31 +503,14 @@ code-example(language="sh" class="code-shell").
537
503
npm install lodash --save
538
504
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-lodash' , 'in-memory-graphql.ts (imports)' )
539
505
540
- .alert.is-important
541
- :marked
542
- Uri: What is the npm install command for the lodash functions?
543
- Do we install the whole lodash library or just the functions?
544
- (I've never used lodash before).
545
- :Kapunahele - fixed. for ease of use I've installed all of lodash
546
-
547
-
548
506
:marked
549
507
Notice that the server includes functions that correspond to each
550
508
type in the schema _and_ the mutations.
551
509
552
510
This mechanism makes writing simple GraphQL servers straightforward—you simply
553
511
resolve a specific type of data.
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.
556
-
557
- .alert.is-important
558
- :marked
559
- Uri: I don't know how to incorporate this: "separate the
560
- frontend logic from the backend". And I don't know if
561
- I'm right about "This removes the coupling between frontend and backend".
562
- I was trying to make it clearer by breaking it into smaller ideas, but
563
- I'm not sure what those ideas should be.
564
- :Kapunahele - expanded on that, let me know if that works
512
+ This removes the coupling between the frontend and backend because you don't need to know the specific
513
+ query the client makes to create the server implementation.
565
514
566
515
:marked
567
516
Now, connect the schema to the resolvers with the `makeExecutableSchema` function from
@@ -580,12 +529,7 @@ code-example(language="sh" class="code-shell").
580
529
library and export it so you can use it with the Apollo Client.
581
530
First, `npm install`:
582
531
code-example( language ="sh" class ="code-shell" ) .
583
- npm install graphql --save
584
-
585
- .alert.is-important
586
- :marked
587
- Uri: can you provide the npm install command?
588
- :Kapunahele - done
532
+ npm install graphql --save
589
533
590
534
:marked
591
535
Next, add an import statement for `execute`.
@@ -594,21 +538,15 @@ code-example(language="sh" class="code-shell").
594
538
:marked
595
539
Now create a new `networkInterface` class and call it `InBrowserNetworkInterface`.
596
540
597
- That class will have a `schema` property which it will initialize in the constructor.
541
+ This class will have a `schema` property which it will initialize in the constructor.
598
542
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.
543
+ Next, the `query` function takes as an argument the query request and executes
544
+ that query using the GraphQL ` execute` function against the schema property.
601
545
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.
546
+ You send empty objects to the `rootValue` and `contextValue` arguments of the function with `{}` and `{}` respectively
547
+ and send the `variables` and `operationName` arguments that are related to the query request.
604
548
605
- Now export the new `InBrowserNetworkInterface` class in order to import it to Apollo Client.
606
- .alert.is-important
607
- :marked
608
- Uri: Could you explain what's happening in the below?
609
- This will probably take 2-3 short paragraphs. Just a
610
- general tour from top to bottom.
611
- :Kapunahele - let me know if that works
549
+ Lastly export the new `InBrowserNetworkInterface` class in order to import it to Apollo Client.
612
550
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'execute-and-export' , 'in-memory-graphql.ts (excerpt)' )
613
551
:marked
614
552
Now all that's left is to connect the new in-memory server to the Apollo Client configuration
@@ -624,12 +562,6 @@ code-example(language="sh" class="code-shell").
624
562
* You can make the resolver functions call your server's existing REST endpoint.
625
563
* You can start a separate Node GraphQL server and simply move the code into it for persistance.
626
564
627
- .alert.is-important
628
- :marked
629
- Uri: Let's add a conclusion showing what was accomplished in the doc.
630
- Maybe something like (please add anything I've missed):
631
- :Kapunahele - done, but not sure if its enough?
632
-
633
565
.l-main-section
634
566
:marked
635
567
## Conclusion
0 commit comments