UNPKG

7.21 kBMarkdownView Raw
1# [Apollo Client](https://www.apollographql.com/client/) [![npm version](https://badge.fury.io/js/apollo-client.svg)](https://badge.fury.io/js/apollo-client) [![Open Source Helpers](https://www.codetriage.com/apollographql/apollo-client/badges/users.svg)](https://www.codetriage.com/apollographql/apollo-client) [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/apollo)
2
3Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components that fetch data via GraphQL. To get the most value out of `apollo-client`, you should use it with one of its view layer integrations.
4
5To get started with the React integration, go to our [**React Apollo documentation website**](https://www.apollographql.com/docs/react/).
6
7Apollo Client also has view layer integrations for [all the popular frontend frameworks](#learn-how-to-use-apollo-client-with-your-favorite-framework). For the best experience, make sure to use the view integration layer for your frontend framework of choice.
8
9Apollo Client can be used in any JavaScript frontend where you want to use data from a GraphQL server. It's:
10
111. **Incrementally adoptable**, so that you can drop it into an existing JavaScript app and start using GraphQL for just part of your UI.
122. **Universally compatible**, so that Apollo works with any build setup, any GraphQL server, and any GraphQL schema.
133. **Simple to get started with**, so you can start loading data right away and learn about advanced features later.
144. **Inspectable and understandable**, so that you can have great developer tools to understand exactly what is happening in your app.
155. **Built for interactive apps**, so your users can make changes and see them reflected in the UI immediately.
166. **Small and flexible**, so you don't get stuff you don't need. The core is under 25kb compressed.
177. **Community driven**, because Apollo is driven by the community and serves a variety of use cases. Everything is planned and developed in the open.
18
19Get started on the [home page](http://apollographql.com/client), which has great examples for a variety of frameworks.
20
21## Installation
22
23```bash
24# installing the preset package
25npm install apollo-boost graphql-tag graphql --save
26# installing each piece independently
27npm install apollo-client apollo-cache-inmemory apollo-link-http graphql-tag graphql --save
28```
29
30To use this client in a web browser or mobile app, you'll need a build system capable of loading NPM packages on the client. Some common choices include Browserify, Webpack, and Meteor 1.3+.
31
32Install the [Apollo Client Developer tools for Chrome](https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm) for a great GraphQL developer experience!
33
34## Usage
35
36You get started by constructing an instance of the core class [`ApolloClient`][]. If you load `ApolloClient` from the [`apollo-boost`][] package, it will be configured with a few reasonable defaults such as our standard in-memory cache and a link to a GraphQL API at `/graphql`.
37
38```js
39import ApolloClient from 'apollo-boost';
40
41const client = new ApolloClient();
42```
43
44
45To point `ApolloClient` at a different URL, add your GraphQL API's URL to the `uri` config property:
46
47```js
48import ApolloClient from 'apollo-boost';
49
50const client = new ApolloClient({
51 uri: 'https://graphql.example.com'
52});
53```
54
55Most of the time you'll hook up your client to a frontend integration. But if you'd like to directly execute a query with your client, you may now call the `client.query` method like this:
56
57```js
58import gql from 'graphql-tag';
59
60client.query({
61 query: gql`
62 query TodoApp {
63 todos {
64 id
65 text
66 completed
67 }
68 }
69 `,
70})
71 .then(data => console.log(data))
72 .catch(error => console.error(error));
73```
74
75Now your client will be primed with some data in its cache. You can continue to make queries, or you can get your `client` instance to perform all sorts of advanced tasks on your GraphQL data. Such as [reactively watching queries with `watchQuery`][], [changing data on your server with `mutate`][], or [reading a fragment from your local cache with `readFragment`][].
76
77To learn more about all of the features available to you through the `apollo-client` package, be sure to read through the [**`apollo-client` API reference**](https://www.apollographql.com/docs/react/api/apollo-client.html).
78
79[`ApolloClient`]: https://www.apollographql.com/docs/react/api/apollo-client.html
80[`apollo-boost`]: https://www.apollographql.com/docs/react/essentials/get-started.html#apollo-boost
81[reactively watching queries with `watchQuery`]: https://www.apollographql.com/docs/react/api/apollo-client.html#ApolloClient.watchQuery
82[changing data on your server with `mutate`]: https://www.apollographql.com/docs/react/essentials/mutations.html
83[reading a fragment from your local cache with `readFragment`]: https://www.apollographql.com/docs/react/advanced/caching.html#direct
84
85## Learn how to use Apollo Client with your favorite framework
86
87- [React](http://apollographql.com/docs/react/)
88- [Angular](http://apollographql.com/docs/angular/)
89- [Vue](https://github.com/Akryum/vue-apollo)
90- [Ember](https://github.com/bgentry/ember-apollo-client)
91- [Web Components](https://github.com/apollo-elements/apollo-elements)
92- [Meteor](http://apollographql.com/docs/react/recipes/meteor.html)
93- [Blaze](http://github.com/Swydo/blaze-apollo)
94- [Vanilla JS](https://www.apollographql.com/docs/react/api/apollo-client.html)
95- [Next.js](https://github.com/zeit/next.js/tree/master/examples/with-apollo)
96
97---
98
99## Contributing
100
101[![CircleCI](https://circleci.com/gh/apollographql/apollo-client.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-client)
102[![codecov](https://codecov.io/gh/apollographql/apollo-client/branch/master/graph/badge.svg)](https://codecov.io/gh/apollographql/apollo-client)
103
104[Read the Apollo Contributor Guidelines.](CONTRIBUTING.md)
105
106Running tests locally:
107
108```
109npm install
110npm test
111```
112
113This project uses TypeScript for static typing and TSLint for linting. You can get both of these built into your editor with no configuration by opening this project in [Visual Studio Code](https://code.visualstudio.com/), an open source IDE which is available for free on all platforms.
114
115#### Important discussions
116
117If you're getting booted up as a contributor, here are some discussions you should take a look at:
118
1191. [Static typing and why we went with TypeScript](https://github.com/apollostack/apollo-client/issues/6) also covered in [the Medium post](https://medium.com/apollo-stack/javascript-code-quality-with-free-tools-9a6d80e29f2d#.k32z401au)
1201. [Idea for pagination handling](https://github.com/apollostack/apollo-client/issues/26)
1211. [Discussion about interaction with Redux and domain vs. client state](https://github.com/apollostack/apollo-client/issues/98)
1221. [Long conversation about different client options, before this repo existed](https://github.com/apollostack/apollo/issues/1)
123
124## Maintainers
125
126- [@benjamn](https://github.com/benjamn) (Apollo)
127- [@hwillson](https://github.com/hwillson) (Apollo)