Skip to content
This repository has been archived by the owner on Dec 4, 2017. It is now read-only.

Angular-GraphQL cookbook #3135

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Angular-GraphQL cookbook #3135

wants to merge 16 commits into from

Conversation

Urigo
Copy link

@Urigo Urigo commented Jan 25, 2017

Hello!

This PR is for an Angular & GraphQL cookbook and includes everything an Angular developer needs in order to integrate GraphQL into their Angular apps.

It includes:

  • Full cookbook of how to use GraphQL in your Angular app

  • Full Clone of Tour Of Heroes last chapter with in-memory-graphql instead of on-memory REST

  • All Tests pass both for JIT and AOT

  • Working Plunker

  • Green Lint

  • We've also updated a lot of libraries during that process to be compatible with Rollup and AOT

  • apollo-client

  • apollo-client-rxjs

  • apollo-angular (and renaming from angular2-apollo #JustAngular)

  • GraphQL-js

I believe I’ve followed all the guidelines needed but I’m also here in SF for 2 weeks if you need me to come over for a review!

@Urigo Urigo closed this Jan 25, 2017
@Urigo Urigo reopened this Jan 25, 2017
@kamilkisiela
Copy link

Soooooooooooooo awesome!

@DxCx
Copy link

DxCx commented Jan 26, 2017

Cool!! :)

@alonp99
Copy link

alonp99 commented Jan 29, 2017

Good job!

@Urigo Urigo force-pushed the graphql-cookbook branch 2 times, most recently from c03df74 to 6d7fa3f Compare February 3, 2017 05:31
@Urigo
Copy link
Author

Urigo commented Feb 3, 2017

Updated and rebased to latest master and included the changes made on these PRs:

All the tests of my PR passes but the tests for the server-communication example are failing..
@wardbell I've checked out master (without my changes) locally and the tests are failing as well.
Also I've looked at the deployed version here - https://angular.io/docs/ts/latest/guide/server-communication.html
Opened the live example in Plunker and it failed as well (changed the Wikipedia url inside the example to https to overcome another problem and then got the one the is happening also in the build).
This is weird because I can see that CI was passing in the last commit to master.
Just to be completely sure I also clone the angular.io master on another machine and had the same error.
I can maybe try to solve it but I'm not sure I want to change your examples as part of this PR, maybe as part of another one.
What would you suggest?
Thanks

@Urigo
Copy link
Author

Urigo commented Feb 3, 2017

ohhh just spotted this and that
good to know I'm not crazy :)

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot googlebot added CLA: no and removed CLA: yes labels Feb 7, 2017
Copy link
Contributor

@kapunahelewong kapunahelewong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. It's looking great. I've made a few comments.


.l-sub-section
:marked
You can think about it as a replacement/enhancement for REST (but you can use them both alongside each other)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add period.


Apollo's `mutate` function returns the result as an Observable.
The Observable returns a `mutationResult` variable that is structured
like the `ApolloQueryResult` Typescript Type, where the generic `T` type is a `Hero` type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript Type -> TypeScript type

Please change globally from Typescript to TypeScript.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

networkStatus: NetworkStatus;
};
:marked
and that's also how you reference that result variable in Typescript.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start a new sentence. How about:

If this looks familiar, it's because that's also how you reference the _____ variable in TypeScript.

Please be sure to specify the variable (sometimes a word like "that" can be vague).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

};
:marked
and that's also how you reference that result variable in Typescript.
So in order the get the actual hero data being returned, you access those values like so:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about (or something like - we just need to streamline it and state exactly what we are doing):

To access the hero data the ______ returns, use dot notation to traverse the mutationResult and assign it to a _____.

I don't know the exact words that should go in the blanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

So in order the get the actual hero data being returned, you access those values like so:
+makeExample('heroes-graphql/ts/src/app/heroes.component.ts', 'access-mutation-result', 'heroes.component.ts')
:marked
and then push them into the `heroes` array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you have access to the data, push them into the heroes array.

We want a complete sentence here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

:marked
Now create a new `networkInterface` class and call it `InBrowserNetworkInterface`.

That class will have a `schema` property which it will initialize in the constructor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That

This class initializes a schema property in the constructor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

That class will have a `schema` property which it will initialize in the constructor.

Then create a `query` function that will recieve a query as a variable and execute that query using
`graphql` execute function against the schema property.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:

Next, the query function takes as an argument the request, or query, and executes that query using the GraphQL execute function against the schema property.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took almost as is but changed a small thing. let me know if that makes sense

`graphql` execute function against the schema property.

You send empty objects to the `rootValue` and `contextValue` arguments of the function and send the
`variables` and `operationName` arguments that are related to the query request.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This:

You send empty objects to the rootValue and contextValue arguments of the function

Loses me. I get the second part of the sentence about variables and operationName but where is the first part happening in the code? It may be as simple as saying:

You send empty objects to the rootValue and contextValue arguments of the function with X and Y respectively.

X and Y being code snippets like {} or whatever is actually doing it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh I thought that empty objects are enough.
Ok changed the whole sentence, let me know what you think

You send empty objects to the `rootValue` and `contextValue` arguments of the function and send the
`variables` and `operationName` arguments that are related to the query request.

Now export the new `InBrowserNetworkInterface` class in order to import it to Apollo Client.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now -> Lastly

This lets them know we are at the end of covering what's happening in this code snippet.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

:marked
Uri: Let's add a conclusion showing what was accomplished in the doc.
Maybe something like (please add anything I've missed):
:Kapunahele - done, but not sure if its enough?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's save the details about the conclusion for when we have finalized everything. Then we can make sure it has everything in the same way we are planning for the table of contents.

<a id="top"></a>
:marked
GraphQL is a network protocol, a query language for your API and a runtime for fulfilling those queries with your existing data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API, and

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


.l-sub-section
:marked
You can think about it as a replacement/enhancement for REST (but you can use them both alongside each other).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about this one for a while but only just now came up with this:

GraphQL is a replacement or enhancement for REST and can be used in conjunction with it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better! done.

:marked
## What is GraphQL?

GraphQL is an API query language, helping your Angular app to:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


GraphQL is an API query language, helping your Angular app to:

- Fetch exactly the information it needs from your server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your server -> the server

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

NoSql, REST, Queues, .NET servers, Java servers or any other technology or data source.
- GraphQL solves the need of sending multiple requests for multiple resources to the server and
then running complex joins on the client&mdash;without the need to create a custom endpoint like REST does.
- The GraphQL spec also includes protocols for real-time push updates from the server to the client.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please search for and update spec -> specification and docs -> documentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


Apollo's `mutate` function returns the result as an Observable.
The Observable returns a `mutationResult` variable that is structured
like the `ApolloQueryResult` TypeScript Type, where the generic `T` type is a `Hero` type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type -> type

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

};
:marked
If this looks familiar, it's because that's also how you reference the `mutationResult` variable in TypeScript.
To access the hero data `mutationResult` returns, use dot notation to traverse `mutationResult` and assign it to a new hero object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object. --> object:

It seems like it would flow better with a semicolon.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

To access the hero data `mutationResult` returns, use dot notation to traverse `mutationResult` and assign it to a new hero object.
+makeExample('heroes-graphql/ts/src/app/heroes.component.ts', 'access-mutation-result', 'heroes.component.ts')
:marked
Once you have created the new object with the data, push it into the `heroes` array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once --> "Now that..."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Once you have created the new object with the data, push it into the `heroes` array.

:marked
Just like a query, the mutate function returns an Observable you can subscribe to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutate --> mutate

(Please put in code font).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Just like a query, the mutate function returns an Observable you can subscribe to
that handles the data you request.

At this point, your existing heroes app can also add a hero:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point

also

Now your existing heroes app can add a hero using GraphQL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Urigo Urigo force-pushed the graphql-cookbook branch 2 times, most recently from 01b6e27 to 3ebfe5a Compare February 20, 2017 22:51
@googlebot
Copy link

CLAs look good, thanks!

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

@Urigo Urigo force-pushed the graphql-cookbook branch 3 times, most recently from 99d2a5a to 1d1d352 Compare March 20, 2017 10:46
Copy link
Contributor

@kapunahelewong kapunahelewong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was only able to get to line 227. Very simple changes. Will have to return to continue.

GraphQL is a replacement or enhancement for REST and can be used in conjunction with it.

:marked
GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these first three sentences start with "GraphQL", could we vary the language a little? Maybe one could start with "The GraphQL..." <--- I don't know what the right word would be there. In regular language, interface would work, but considering the context, we can't use that, right? Another way we could reduce repetition in the language could be by combining two of these sentences.

Maybe the first and third could be combined with the third starting with "It" instead. Then we leave the subsection immediately after.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

## What is GraphQL?

GraphQL is an API query language, helping your Angular app:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, helping your Angular app: --> "that helps your Angular app do the following:"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

- Merge multiple dependencies into one single response from the server.
- Handle server data dependency in a component structure.

It’s also important to understand that:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that --> "three key points" or "these key points"

We want intros to lists to be complete sentences, rather than having the list items complete the sentence. I just recently came to understand that if you're wondering why I hadn't mentioned it before.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

It’s also important to understand that:

- **GraphQL is not a data source**. The GraphQL runtime works on top of any data source&mdash;SQL,
NoSql, REST, Queues, .NET servers, Java servers or any other technology or data source.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma before or

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


- **GraphQL is not a data source**. The GraphQL runtime works on top of any data source&mdash;SQL,
NoSql, REST, Queues, .NET servers, Java servers or any other technology or data source.
- GraphQL solves the need of sending multiple requests for multiple resources to the server and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove one of the occurrences of "multiple" since they are close together. Does that still work? Is there another word we could use for the second one? Maybe "various"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


:marked
The `watchQuery` function tells the Apollo Client what data
this component needs. The Apollo Client then returns the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first mention of the Apollo Client. We should add a phrase explaining in just a few words (to preserve brevity) what the Apollo Client is:

"....the Apollo Client, the x that lets you x, what data...."

or:
"....the Apollo Client, covered later,...."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

necessary data to the component as an Observable.
For example, adding an `age` field to the app is
simple because you only change the component (you cover
this syntax [later](#querying) in the cookbook) And then modify the template accordingly:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"And then modify" --> "and then modify".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


:marked
That might work for simple cases but the problem here is that `getHeroes` might fetch
more information than the app really needs for each hero.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem here is that --> but as your app grows

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

and migrate gradually from them.

:marked
### Typed API, tooling and auto documentation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put a comma after and

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

The full documentation can be found on the [Apollo Client website](http://dev.apollodata.com/).

:marked
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is helpful, but we can't rely on third party for the download. We should change this to the live example/downloadable example of the TOH.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@kamilkisiela
Copy link

@googlebot I signed it

@kapunahelewong
Copy link
Contributor

FYI, I have to switch gears for a little bit, but will be back to work on this PR. Looking forward to seeing this cookbook in the docs!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants