@@ -66,7 +66,11 @@ include ../_util-fns
6666.l-sub-section
6767 :marked
6868 You can check out [this](https://www.youtube.com/watch?v=Xx39bv-5ojA&t=1s) talk by [Jeff Cross](https://twitter.com/jeffbcross) and
69- [Uri Goldshtein](https://twitter.com/UriGoldshtein) that summarizes this part
69+ [Uri Goldshtein](https://twitter.com/UriGoldshtein) that summarizes this part:
70+
71+ iframe( type ='text/html' width ='560' height ='315'
72+ src ='https://www.youtube.com/embed/Xx39bv-5ojA'
73+ frameborder ='0' )
7074
7175:marked
7276 ### Component based API
@@ -219,7 +223,7 @@ a#http-heroes
219223 The full documentation can be found on the [Apollo Client docs website](http://dev.apollodata.com/).
220224
221225:marked
222- The starting point for the app is the [Tour of Heroes tutorial](https://github.com/johnpapa/angular2-tour-of-heroes /archive/master .zip) app at its end state.
226+ 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.
223227
224228 This guide shows you how to migrate that app from REST to GraphQL.
225229
@@ -232,6 +236,22 @@ a#http-heroes
232236
233237code-example( language ="sh" class ="code-shell" ) .
234238 npm install apollo-client apollo-angular graphql-tag --save
239+
240+ .l-sub-section
241+ :marked
242+ This example uses `system.js` so you need to also add the configuration to it.
243+ With other build systems, the following process will be different and maybe easier.
244+
245+ :marked
246+ Add the following configuration to your `systemjs.config.js` file under the `map` key:
247+
248+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-client-map' , 'under map: {' )
249+
250+ :marked
251+ and the following configuration to your `systemjs.config.js` file under the `packages` key:
252+
253+ + makeExample('heroes-graphql/ts/src/systemjs.config.extras.js' , 'systemjs-apollo-client-packages' , 'under packages: {' )
254+
235255:marked
236256 Next, initialize the client by creating a new file called `client.ts` and
237257 pasting in the following code:
@@ -245,21 +265,11 @@ code-example(language="sh" class="code-shell").
245265.l-sub-section
246266 :marked
247267 ### To use a different URI for the Apollo Client
248- You don't need the following code snippet to work through this cookbook,
249- but it's good to know how to configure the client to use a URI.
268+ For this cookbook we would use the default `/graphql` endpoint,
269+ but it's good to know it is possible to change those settings.
250270 To change the [settings](http://dev.apollodata.com/core/apollo-client-api.html#ApolloClient\.constructor)
251271 of `ApolloClient`, call its constructor with different parameters.
252- You can alter the endpoint by importing `createNetworkInterface` and configuring the
253- `networkInterface` property of `ApolloClient` to
254- use a URI:
255-
256- + makeExample('heroes-graphql/ts/src/app/client.2.ts' , 'network-initialization' , 'app/client.ts' )
257- .l-sub-section
258- :marked
259- For instructions on running a GraphQL server,
260- see [Appendix: Setting up a GraphQL server](#server).
261- The appendix shows you how to run a GraphQL server in-memory
262- on the client, which is helpful for testing.
272+ Go to the [Apollo Docs](http://dev.apollodata.com/angular2/initialization.html#creating-client) for further resources.
263273
264274:marked
265275 After initializing the Apollo client, import the `ApolloModule`
@@ -273,27 +283,37 @@ code-example(language="sh" class="code-shell").
273283
274284+ makeExample('heroes-graphql/ts/src/app/app.module.ts' , 'apollo-ngmodule' , 'app.module.ts (excerpt)' )
275285
286+ :marked
287+ Now Apollo is initialized and ready for use in the app.
288+
276289.l-main-section
277290<a id =" querying" ></a >
278291:marked
279292 ## Performing a query
280293
281294 With GraphQL you query a schema, which is organized into types and fields,
282295 that represents the data you can query.
283-
284- The server for this guide is based on the [Tour of Heroes](ts/latest/tutorial/).
285- Copy the schema that follows into a file named `in-memory-graphql.ts` in the `app` directory:
286296
287- :marked
288297 The schema begins with data types and fields followed by the specific queries
289298 you can perform on the data. These are in turn followed by
290299 mutations, which are _actions_ that you
291300 can call on the server, similar to a POST request in REST.
292301
293- + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'graphql-schema' , 'app/in-memory-graphql.ts' )
302+ Here is an example schema:
303+
304+ + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'graphql-schema' , 'Tour of heroes schema' )
305+
306+ .l-sub-section
307+ :marked
308+ Usually you need to query an existing server.
309+
310+ The server for this guide is based on the [Tour of Heroes](ts/latest/tutorial/) app.
311+
312+ In order to create the GraphQL server for this example, follow the instructions on
313+ [Appendix: Setting up a GraphQL server](#server).
294314
295315:marked
296- To start querying data, begin by importing `Apollo` into `heroes.component.ts`,
316+ Once you have a server, to start querying data, begin by importing `Apollo` into `heroes.component.ts`,
297317 and injecting it into the constructor:
298318+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'import-apollo' , 'heroes.component.ts (excerpt)' )
299319+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'inject-apollo' , 'heroes.component.ts (excerpt)' )
@@ -305,6 +325,11 @@ code-example(language="sh" class="code-shell").
305325
306326+ makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'import-graphql-tag' , 'heroes.component.ts' )
307327
328+ :marked
329+ In order to specify the TypeScript type of the data that is recieved, import `ApolloQueryResult` from `apollo-client`:
330+
331+ + makeExample('heroes-graphql/ts/src/app/heroes.component.ts' , 'import-apollo-query-result' , 'import type' )
332+
308333:marked
309334 To query data with the Apollo Client, pass a GraphQL query with the
310335 data and structure that you want to the `Apollo` `watchQuery` function.
@@ -436,8 +461,9 @@ figure.image-display
436461:marked
437462 ## Appendix: Setting up a GraphQL server
438463
439- Running the server in the browser helps you start using
440- GraphQL on your frontend without having a GraphQL backend.
464+ This example shows how to run a GraphQL in the browser but running a GraphQL server on
465+ Node.js or in the browser is very similar.
466+
441467 If you don't have the option of running GraphQL on the server,
442468 this method makes it possible to still use GraphQL in your app with the
443469 benefits for not needing to sync multiple REST requests and join logic
@@ -459,9 +485,31 @@ figure.image-display
459485 It allows you to write a GraphQL schema as a string and make it executable.
460486 In a terminal window, issue the following command:
461487
488+ .l-sub-section
489+ :marked
490+ This example uses `system.js` so you need to also add the configuration to it.
491+ With other build systems, or when running on Node, the following process will be different and
492+ maybe easier.
493+
462494code-example( language ="sh" class ="code-shell" ) .
463495 npm install graphql-tools --save
464496
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-graphql-server-map' , 'under map: {' )
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-graphql-server-packages' , 'under packages: {' )
512+
465513:marked
466514 Next, create a file called `in-memory-graphql.ts` in the `app` directory
467515 and paste in the following schema:
@@ -486,6 +534,13 @@ code-example(language="sh" class="code-shell").
486534 Hence, the GraphQL server consists of _resolver
487535 functions_ that correspond to the _types_ of the schema.
488536
537+ In some server functions you use the `lodash` library so don't
538+ forget to install them from npm and import them:
539+ code-example( language ="sh" class ="code-shell" ) .
540+ npm install lodash --save
541+ + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-lodash' , 'in-memory-graphql.ts (imports)' )
542+
543+ :marked
489544 To create the resolvers, copy the following code and add it to `in-memory-graphql.ts`.
490545+ makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'resolvers' , 'in-memory-graphql.ts' )
491546
@@ -494,13 +549,6 @@ code-example(language="sh" class="code-shell").
494549 For the full explanation about how GraphQL resolvers work see
495550 [Execution](http://graphql.org/learn/execution/) on [GraphQL.org](http://graphql.org/).
496551
497- :marked
498- You may have noticed some functions from `lodash` such as `lodashFind()` so don't
499- forget to install them from npm and import them:
500- code-example( language ="sh" class ="code-shell" ) .
501- npm install lodash --save
502- + makeExample('heroes-graphql/ts/src/app/in-memory-graphql.ts' , 'import-lodash' , 'in-memory-graphql.ts (imports)' )
503-
504552:marked
505553 Notice that the server includes functions that correspond to each
506554 type in the schema _and_ the mutations.
0 commit comments