@@ -218,6 +218,13 @@ a#http-heroes
218218 This cookbook touches on the main points of using GraphQL with Angular.
219219 The full documentation can be found on the [Apollo Client docs website](http://dev.apollodata.com/).
220220
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+
221228.l-main-section
222229<a id =" installation" ></a >
223230:marked
@@ -384,57 +391,28 @@ code-example(language="json").
384391
385392 Apollo's `mutate` function returns the result as an Observable.
386393 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:
388395code-example( language ="json" ) .
389396 type ApolloQueryResult< T> = {
390397 data: T;
391398 loading: boolean;
392399 networkStatus: NetworkStatus;
393400 };
394401: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.
397404+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'access-mutation-result' , 'heroes.component.ts' )
398405: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.
417407
418408:marked
419409 Just like a query, the mutate function returns an Observable you can subscribe to
420410 that handles the data you request.
421411
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:
423413figure.image-display
424414 img( src ='/resources/images/cookbooks/heroes-graphql/heroes- graphql-mutation.gif' alt ="Heroes GraphQL Mutation" )
425415
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-
438416.l-main-section
439417<a id =" resources" ></a >
440418:marked
@@ -467,13 +445,6 @@ figure.image-display
467445 benefits for not needing to sync multiple REST requests and join logic
468446 on the client.
469447
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-
477448.l-sub-section
478449 :marked
479450 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
485456 similar to Firebase, but based on the GraphQL API spec.
486457 For help on getting up and running, see [Scaphold](https://www.scaphold.io/) and [Graphcool](https://www.graph.cool/).
487458
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-
494459:marked
495460 In order to create a GraphQL schema, you need the `graphql-tools` library.
496461 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").
504469 and paste in the following schema:
505470
506471+ 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.
507477
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.
512478.l-sub-section
513479 :marked
514480 While the schema includes the major points covered in this cookbook,
@@ -537,31 +503,14 @@ code-example(language="sh" class="code-shell").
537503 npm install lodash --save
538504+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-lodash' , 'in-memory-graphql.ts (imports)' )
539505
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-
548506:marked
549507 Notice that the server includes functions that correspond to each
550508 type in the schema _and_ the mutations.
551509
552510 This mechanism makes writing simple GraphQL servers straightforward—you simply
553511 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.
565514
566515:marked
567516 Now, connect the schema to the resolvers with the `makeExecutableSchema` function from
@@ -580,12 +529,7 @@ code-example(language="sh" class="code-shell").
580529 library and export it so you can use it with the Apollo Client.
581530 First, `npm install`:
582531code-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
589533
590534:marked
591535 Next, add an import statement for `execute`.
@@ -594,21 +538,15 @@ code-example(language="sh" class="code-shell").
594538:marked
595539 Now create a new `networkInterface` class and call it `InBrowserNetworkInterface`.
596540
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.
598542
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.
601545
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.
604548
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.
612550+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'execute-and-export' , 'in-memory-graphql.ts (excerpt)' )
613551:marked
614552 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").
624562 * You can make the resolver functions call your server's existing REST endpoint.
625563 * You can start a separate Node GraphQL server and simply move the code into it for persistance.
626564
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-
633565.l-main-section
634566:marked
635567 ## Conclusion
0 commit comments