@@ -27,7 +27,9 @@ include ../_util-fns
27
27
28
28
- [Performing a mutation](#mutation)
29
29
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)
31
33
32
34
- [Further resources](#resources)
33
35
@@ -224,7 +226,9 @@ a#http-heroes
224
226
The full documentation can be found on the [Apollo Client website](http://dev.apollodata.com/).
225
227
226
228
: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>**.
228
232
229
233
This guide shows you how to migrate that app from REST to GraphQL.
230
234
@@ -253,6 +257,19 @@ code-example(language="sh" class="code-shell").
253
257
254
258
+ makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-client-packages' , 'under packages: { (excerpt)' )
255
259
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
+
256
273
:marked
257
274
Next, initialize the client by creating a new file called `client.ts` and
258
275
pasting in the following code:
@@ -268,7 +285,7 @@ code-example(language="sh" class="code-shell").
268
285
.l-sub-section
269
286
:marked
270
287
### 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,
272
289
but it's good to know it is possible to change those settings.
273
290
To change the [settings](http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient\.constructor)
274
291
of `ApolloClient`, call its constructor with different parameters.
@@ -277,15 +294,18 @@ code-example(language="sh" class="code-shell").
277
294
:marked
278
295
Usually you need to query an existing server.
279
296
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
+
282
299
Now all that's left is to connect the in-memory server to the Apollo Client configuration
283
300
by importing `networkInterface` and adding it to the `client` constant in `client.ts`.
284
301
+ makeExample('heroes-graphql/ts/src/app/client.ts' , '' , 'client.ts' )
285
302
.l-sub-section
286
303
:marked
287
304
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.
289
309
290
310
:marked
291
311
After initializing the Apollo Client, import the `ApolloModule` and `getClient`
@@ -297,7 +317,7 @@ code-example(language="sh" class="code-shell").
297
317
is an initialization function that accepts the Apollo configuration
298
318
you created earlier as an argument and creates a new Apollo instance for the app.
299
319
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)' )
301
321
302
322
:marked
303
323
Now Apollo is initialized and ready for use in the app.
@@ -317,10 +337,17 @@ code-example(language="sh" class="code-shell").
317
337
318
338
Here is the schema the Tour of Heroes server in the app use:
319
339
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.
321
346
347
+ First remove all references of the exisitng `HeroService`:
348
+ + makeExample('heroes-graphql-starter/ts/src/app/heroes.component.1.ts' , '' , 'heroes.component.ts' )
322
349
: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`,
324
351
and injecting it into the constructor:
325
352
+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'import-apollo' , 'heroes.component.ts (excerpt)' )
326
353
+ 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").
350
377
For more information on GraphQL queries, see the GraphQL documentation on
351
378
[Queries and Mutations](http://graphql.org/learn/queries/).
352
379
: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:
354
381
+ makeExample('heroes-graphql/ts/src/app/heroes.component.1.html' , 'render-heroes' , 'heroes.component.html' )
355
382
356
383
: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.
359
386
360
387
.l-main-section
361
388
<a id =" mutation" ></a >
@@ -405,10 +432,10 @@ code-example(language="json").
405
432
documentation](http://graphql.org/learn/queries/#mutations)
406
433
at [GraphQL.org](http://graphql.org).
407
434
: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:
409
436
+ makeExample('heroes-graphql/ts/src/app/heroes.component.html' , 'add' , 'heroes.component.html' )
410
437
: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`
412
439
and is `void` because it returns nothing.
413
440
+ makeExample('heroes-graphql/ts/src/app/heroes.component.1.ts' , 'add' , 'heroes.component.ts' )
414
441
:marked
@@ -444,10 +471,65 @@ code-example(language="json").
444
471
figure.image-display
445
472
img( src ='/resources/images/cookbooks/heroes-graphql/heroes- graphql-mutation.gif' alt ="Heroes GraphQL Mutation" )
446
473
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
+
447
529
.l-main-section
448
530
<a id =" server" ></a >
449
531
:marked
450
- ## Appendix: Setting up a GraphQL server
532
+ ## Appendix 2 : Setting up a GraphQL server
451
533
452
534
This example shows how to run a GraphQL in the browser but running a GraphQL server on
453
535
Node.js or in the browser is very similar.
@@ -466,7 +548,7 @@ figure.image-display
466
548
467
549
Additionally, there are a few GraphQL backend-as-a-service platforms available,
468
550
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/).
470
552
471
553
:marked
472
554
In order to create a GraphQL schema, you need the `graphql-tools` library.
@@ -499,10 +581,10 @@ code-example(language="sh" class="code-shell").
499
581
+ makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-graphql-server-packages' , 'under packages: { (excerpt)' )
500
582
501
583
: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
503
585
and paste in the following schema:
504
586
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' )
506
588
:marked
507
589
The schema starts with a represention of the model of data the server exposes.
508
590
Then the schema specifies what queries are allowed on that data, followed by
@@ -514,7 +596,10 @@ code-example(language="sh" class="code-shell").
514
596
While the schema includes the major points covered in this cookbook,
515
597
you can read more in the [GraphQL.org Introduction to GraphQL](http://graphql.org/learn/).
516
598
: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:
518
603
+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'heroes-array' , 'in-memory-graphql.ts (excerpt)' )
519
604
:marked
520
605
The next step is writing a server that _resolves_
@@ -605,6 +690,7 @@ code-example(language="sh" class="code-shell").
605
690
- What is GraphQL and why it can benefit Angular developers.
606
691
- How to create a basic GraphQL query.
607
692
- How to create a basic GraphQL mutation.
693
+ - How to mock a GraphQL server.
608
694
- How to build a GraphQL server.
609
695
- Resources to dive deeper.
610
696
0 commit comments