Skip to content

Commit

Permalink
Created demo and updated Readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyThorpe committed Feb 6, 2017
1 parent 2224cfb commit d70d01b
Show file tree
Hide file tree
Showing 8 changed files with 26,326 additions and 45 deletions.
26 changes: 19 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Attaches additional methods to Observables/Observable Arrays via a [Custom Funct
* [ko.plus](http://stevegreatrex.github.io/ko.plus/)
* [jQuery](http://jquery.com)

## Demo
Find out info about the Pokemon from https://graphql-pokemon.now.sh/ endpoint at [http://antonythorpe.github.io/knockout-apollo](http://antonythorpe.github.io/knockout-apollo)

## Installation
In the command line:
```sh
Expand All @@ -24,18 +27,26 @@ npm install --save apollo-client graphql-tag knockout knockout-apollo ko.plus jq
## Getting Started
```js
// set the JavaScript template literal tag that parses GraphQL query strings into the standard GraphQL AST
import ApolloClient, {
createNetworkInterface
} from 'apollo-client';
import gql from 'graphql-tag';
global.jQuery = require('jquery');
import ko from 'knockout';
require('ko.plus');
import 'knockout-apollo.js';

// Create an ApolloClient
var apolloClient = new ApolloClient({
const apolloClient = new ApolloClient({
networkInterface: createNetworkInterface({ uri: https://yourwebsite.net/graphql})
});

// Next initialise via a Custom Function
this.yourObservable = ko.observable().launchApollo(apolloClient, errorCallback);
var self = this;
self.yourObservable = ko.observable().launchApollo(apolloClient, errorCallback);

// Now we are ready to make queries etc with Apollo. Outline below:
this.yourObservable.apollo({object for Apollo}, {object of callbacks});
self.yourObservable.apollo({object for Apollo}, {object of callbacks});
```
## Query with Parameters
Expand Down Expand Up @@ -64,7 +75,7 @@ var query = gql`query PingMessage($message: String!) {
ping(message: $message)
}`;

this.yourObservable.apollo(
self.yourObservable.apollo(
{
query: query,
variables: {
Expand All @@ -75,10 +86,11 @@ this.yourObservable.apollo(
// callback when successful
resolve: function(data) {
console.log(data); //An ApolloQueryResult
self.observableToUpdate(data.data.PingMessage);
},
// callback to report errors (optional)
reject: function(error) {
console.log(error);
self.yourObservable.failMessage("Hey, that didnt work " + error);
},
always: function(){
// called for both success and failure (optional)
Expand All @@ -91,20 +103,20 @@ this.yourObservable.apollo(
Thanks to the `ko.plus` knockout plugin there are a couple of useful public observables. A loading indicator can be used through `yourObservable.apollo.isRunning` and if failed through `yourObservable.apollo.failed`. See [link](https://github.com/stevegreatrex/ko.plus/blob/master/README.md#example-implementation).
## Error Callback
The default is to console log the error. For an alternative, provide a callback function as a second arguement to the `launchApollo` function when initialising (or you can simply pass it in with the call).
The default is to console log the error. For an alternative, provide a callback function as a second arguement to the `launchApollo` function when initialising (or you can simply pass it in with the call - `ko.plus` provides a `failMessage` observable that can be set within the reject callback).
## Example
Under the example folder, is code modified from [Learn Apollo](https://www.learnapollo.com). If you want to get this up and running:
* start the `Learn Apollo` React tutorial until you get past the Getting Started section. Note down the Apollo Client configuration. You will need this for line 5 of `index.js`.
* cd to exercise 6 and `npm install`
* copy `index.html` and `index.js` from the example folder and replace the code in exercise 6
* replace the `client` variable with the configuration noted above from `Learn Apollo`.
* add your name as the trainer in `index.js` (line 273)
* then `npm install --save graphql-tag knockout knockout-apollo ko.plus jquery knockoutcrud`
* To launch a browser `npm start`
Hope this works :)
## Todo
* build Apollo for the browser
* watchQuery
* subscriptions
* tests
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Changelog of Knockout Apollo

0.1.0 Initial commit - Queries and Mutations
0.1.1 Added demo

0 comments on commit d70d01b

Please sign in to comment.