@@ -27,7 +27,9 @@ include ../_util-fns
2727
2828 - [Performing a mutation](#mutation)
2929
30- - [Appendix: Setting up a GraphQL server](#server)
30+ - [Appendix 1: Mocking a GraphQL server](#mock-server)
31+
32+ - [Appendix 2: Setting up a GraphQL server](#server)
3133
3234 - [Further resources](#resources)
3335
@@ -224,7 +226,9 @@ a#http-heroes
224226 The full documentation can be found on the [Apollo Client website](http://dev.apollodata.com/).
225227
226228:marked
227- The starting point for the app is the [Tour of Heroes tutorial](https://github.com/Urigo/quickstart/archive/graphql-start.zip) app at its end state.
229+ The starting point for the app is the Tour of Heroes tutorial app at its end state.
230+
231+ **Download the <live-example name="heroes-graphql-starter"></live-example>**.
228232
229233 This guide shows you how to migrate that app from REST to GraphQL.
230234
@@ -253,6 +257,19 @@ code-example(language="sh" class="code-shell").
253257
254258+ makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-client-packages' , 'under packages: { (excerpt)' )
255259
260+ :marked
261+ Because you also have a running server on your app with more dependencies, you will need to add additional configurations to
262+ `systemjs.config.js` as well.
263+
264+ Add the following configuration to your `systemjs.config.js` file under the `map` key:
265+
266+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-graphql-server-map' , 'under map: { (excerpt)' )
267+
268+ :marked
269+ and the following configuration to your `systemjs.config.js` file under the `packages` key:
270+
271+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-graphql-server-packages' , 'under packages: { (excerpt)' )
272+
256273:marked
257274 Next, initialize the client by creating a new file called `client.ts` and
258275 pasting in the following code:
@@ -268,7 +285,7 @@ code-example(language="sh" class="code-shell").
268285.l-sub-section
269286 :marked
270287 ### To use a different URI for the Apollo Client
271- For this cookbook we would use the default `/graphql` endpoint,
288+ For this cookbook you would use the default `/graphql` endpoint,
272289 but it's good to know it is possible to change those settings.
273290 To change the [settings](http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient\.constructor)
274291 of `ApolloClient`, call its constructor with different parameters.
@@ -277,15 +294,18 @@ code-example(language="sh" class="code-shell").
277294:marked
278295 Usually you need to query an existing server.
279296 The server for this guide is based on the [Tour of Heroes](ts/latest/tutorial/) app.
280- The starter app already has an in-memory GraphQL server prepared.
281-
297+ The starter app (<live-example name="heroes-graphql-starter"></live-example>) already has an in-memory GraphQL server prepared.
298+
282299 Now all that's left is to connect the in-memory server to the Apollo Client configuration
283300 by importing `networkInterface` and adding it to the `client` constant in `client.ts`.
284301+ makeExample('heroes-graphql/ts/src/app/client.ts' , '' , 'client.ts' )
285302.l-sub-section
286303 :marked
287304 In order to learn how to create the GraphQL server for this example, follow the instructions on
288- [Appendix: Setting up a GraphQL server](#server).
305+ [Appendix 2: Setting up a GraphQL server](#server).
306+
307+ Another important ability of Apollo is to create a mock server in one line of code based on a GraphQL schema.
308+ Check out the [Appendix 1: Mocking a GraphQL server](#mock-server) to learn about that as well.
289309
290310:marked
291311 After initializing the Apollo Client, import the `ApolloModule` and `getClient`
@@ -297,7 +317,7 @@ code-example(language="sh" class="code-shell").
297317 is an initialization function that accepts the Apollo configuration
298318 you created earlier as an argument and creates a new Apollo instance for the app.
299319
300- + makeExample('heroes-graphql/ts/src/app/app.module.ts' , 'apollo-ngmodule' , 'app.module.ts (excerpt)' )
320+ + makeExample('heroes-graphql/ts/src/app/app.module.1. ts' , 'apollo-ngmodule' , 'app.module.ts (excerpt)' )
301321
302322:marked
303323 Now Apollo is initialized and ready for use in the app.
@@ -317,10 +337,17 @@ code-example(language="sh" class="code-shell").
317337
318338 Here is the schema the Tour of Heroes server in the app use:
319339
320- + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'graphql-schema' , 'Tour of heroes schema' )
340+ + makeExample('heroes-graphql/ts/src/app/schema.ts' , '' , 'schema.ts' )
341+
342+ :marked
343+ Once you have a server, which is prepared already in this app, to start querying data.
344+
345+ You will convert the `heroes.component` from quering the data from the REST endpoint to the GraphQL server.
321346
347+ First remove all references of the exisitng `HeroService`:
348+ + makeExample('heroes-graphql-starter/ts/src/app/heroes.component.1.ts' , '' , 'heroes.component.ts' )
322349:marked
323- Once you have a server, which is prepared already in this app, to start querying data, begin by importing `Apollo` into `heroes.component.ts`,
350+ Now begin adding Apollo importing `Apollo` into `heroes.component.ts`,
324351 and injecting it into the constructor:
325352+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'import-apollo' , 'heroes.component.ts (excerpt)' )
326353+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'inject-apollo' , 'heroes.component.ts (excerpt)' )
@@ -350,12 +377,12 @@ code-example(language="sh" class="code-shell").
350377 For more information on GraphQL queries, see the GraphQL documentation on
351378 [Queries and Mutations](http://graphql.org/learn/queries/).
352379:marked
353- Next, update the template so it displays the results of the query.
380+ Now, the same template that worked before will still displays the results of the new query:
354381+ makeExample('heroes-graphql/ts/src/app/heroes.component.1.html' , 'render-heroes' , 'heroes.component.html' )
355382
356383:marked
357- At this point, if you have a running [GraphQL server](#server), the browser displays the
358- fetched data.
384+ At this point, if you go to the `heroes` tab, and you have a running [GraphQL server](#server),
385+ the browser displays the fetched data.
359386
360387.l-main-section
361388<a id =" mutation" ></a >
@@ -405,10 +432,10 @@ code-example(language="json").
405432 documentation](http://graphql.org/learn/queries/#mutations)
406433 at [GraphQL.org](http://graphql.org).
407434:marked
408- To use a mutation, first update the template with a function to add a hero:
435+ To use a mutation, you can use the same existing template with a function to add a hero:
409436+ makeExample('heroes-graphql/ts/src/app/heroes.component.html' , 'add' , 'heroes.component.html' )
410437:marked
411- In the component class, create the `add()` function. It expects a name argument of type `string`
438+ In the component class, there is already an `add()` function. It expects a name argument of type `string`
412439 and is `void` because it returns nothing.
413440+ makeExample('heroes-graphql/ts/src/app/heroes.component.1.ts' , 'add' , 'heroes.component.ts' )
414441:marked
@@ -444,10 +471,65 @@ code-example(language="json").
444471figure.image-display
445472 img( src ='/resources/images/cookbooks/heroes-graphql/heroes- graphql-mutation.gif' alt ="Heroes GraphQL Mutation" )
446473
474+ .l-main-section
475+ <a id =" mock-server" ></a >
476+ :marked
477+ ## Appendix 1: Mocking a GraphQL server
478+
479+ When writing a GraphQL Angular app, you are quering a shared Schema.
480+ Both the client and the server agree on a single schema that describes the data the client can query and the actions it can perform
481+ on the server.
482+
483+ Once you have that schema, there is no need for an actual server and you can mock your server with one line of code.
484+ That mocking is good for day to day development as well as for automatic tests for your Angular app.
485+
486+ Let's create the schema that is based on the [Tour of Heroes](ts/latest/tutorial/) app.
487+
488+ Create a file called `schema.ts` in the `app` directory and paste in the following schema:
489+ + makeExample('heroes-graphql/ts/src/app/schema.ts' , '' , 'schema.ts' )
490+ :marked
491+ Now that you have the schema, let's mock the server so you would be able to use actual data in your app.
492+ First install the `Apollo Test Utils` library:
493+
494+ code-example( language ="sh" class ="code-shell" ) .
495+ npm install apollo-test-utils --save
496+
497+ .l-sub-section
498+ :marked
499+ This example uses `system.js` so you need to also add the configuration to it.
500+ With other build systems, or when running on Node, the following process will be different and
501+ maybe easier.
502+
503+ :marked
504+ Add the following configuration to your `systemjs.config.js` file under the `map` key:
505+
506+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-test-utils-map' , 'under map: { (excerpt)' )
507+
508+ :marked
509+ and the following configuration to your `systemjs.config.js` file under the `packages` key:
510+
511+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-test-utils-packages' , 'under packages: { (excerpt)' )
512+ :marked
513+ Now you need to create a mocked network interface that will use `apollo-test-utils` and the schema you created.
514+
515+ Create a file called `mockedNetworkInterface.ts` and import the schema and the tools to create a mock network interface:
516+ + makeExample('heroes-graphql/ts/src/app/mockedNetworkInterface.ts' , 'imports' , 'imports' )
517+ :marked
518+ Now you need to make the schema executable, add the mocking functions to it, create the network interface and export it:
519+ + makeExample('heroes-graphql/ts/src/app/mockedNetworkInterface.ts' , 'create-interface' , 'Create Network Interface' )
520+ :marked
521+ Now all you need to do is use that network interface in your Apollo Client instead of your regular network interface:
522+ + makeExample('heroes-graphql/ts/src/app/client.3.ts' , 'import-and-use' , 'Use Network Interface' )
523+
524+ :marked
525+ Now every time you will query Apollo Client it will return a mocked data for your client.
526+
527+ To dive deeper to more advanced mocking, check out the [Apollo-Test-Utils repository](https://github.com/apollographql/apollo-test-utils).
528+
447529.l-main-section
448530<a id =" server" ></a >
449531:marked
450- ## Appendix: Setting up a GraphQL server
532+ ## Appendix 2 : Setting up a GraphQL server
451533
452534 This example shows how to run a GraphQL in the browser but running a GraphQL server on
453535 Node.js or in the browser is very similar.
@@ -466,7 +548,7 @@ figure.image-display
466548
467549 Additionally, there are a few GraphQL backend-as-a-service platforms available,
468550 similar to Firebase, but based on the GraphQL API spec.
469- For help on getting up and running, see [Scaphold](https://www.scaphold.io/) and [Graphcool ](https://www.graph.cool/).
551+ For help on getting up and running, see [Scaphold.io ](https://www.scaphold.io/) and [Graph.Cool ](https://www.graph.cool/).
470552
471553:marked
472554 In order to create a GraphQL schema, you need the `graphql-tools` library.
@@ -499,10 +581,10 @@ code-example(language="sh" class="code-shell").
499581+ makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-graphql-server-packages' , 'under packages: { (excerpt)' )
500582
501583:marked
502- Next, create a file called `in-memory-graphql .ts` in the `app` directory
584+ Next, create a file called `schema .ts` in the `app` directory
503585 and paste in the following schema:
504586
505- + makeExample('heroes-graphql/ts/src/app/in-memory-graphql .ts' , 'graphql-schema ' , 'in-memory-graphql .ts' )
587+ + makeExample('heroes-graphql/ts/src/app/schema .ts' , '' , 'schema .ts' )
506588:marked
507589 The schema starts with a represention of the model of data the server exposes.
508590 Then the schema specifies what queries are allowed on that data, followed by
@@ -514,7 +596,10 @@ code-example(language="sh" class="code-shell").
514596 While the schema includes the major points covered in this cookbook,
515597 you can read more in the [GraphQL.org Introduction to GraphQL](http://graphql.org/learn/).
516598:marked
517- Now, create your in-memory data:
599+ Now, create another file called `in-memory-graphql.ts` and import the schema into it:
600+ + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'graphql-schema' , 'in-memory-graphql.ts' )
601+ :marked
602+ next, create your in-memory data:
518603+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'heroes-array' , 'in-memory-graphql.ts (excerpt)' )
519604:marked
520605 The next step is writing a server that _resolves_
@@ -605,6 +690,7 @@ code-example(language="sh" class="code-shell").
605690 - What is GraphQL and why it can benefit Angular developers.
606691 - How to create a basic GraphQL query.
607692 - How to create a basic GraphQL mutation.
693+ - How to mock a GraphQL server.
608694 - How to build a GraphQL server.
609695 - Resources to dive deeper.
610696
0 commit comments