apollo-server/docs/source/features/data-sources.md

185 lines
6.9 KiB
Markdown
Raw Normal View History

---
title: Data Sources
description: Caching Partial Query Results
---
2018-06-19 23:47:25 +02:00
Data sources are classes that encapsulate fetching data from a particular service, with built-in support for caching, deduplication, and error handling. You write the code that is specific to interacting with your backend, and Apollo Server takes care of the rest.
## REST Data Source
2018-06-19 23:47:25 +02:00
A `RESTDataSource` is responsible for fetching data from a given REST API.
2018-06-19 23:47:25 +02:00
To get started, install the release candidate of the REST data source:
2018-06-15 10:26:24 -07:00
```bash
npm install apollo-datasource-rest@rc
```
2018-06-19 23:47:25 +02:00
To define a data source, extend the `RESTDataSource` class and implement the data fetching methods that your resolvers require. Your implementation of these methods can call on convenience methods built into the `RESTDataSource` class to perform HTTP requests, while making it easy to build up query parameters, parse JSON results, and handle errors.
```js
2018-06-15 10:26:24 -07:00
const { RESTDataSource } = require('apollo-datasource-rest');
2018-06-19 23:47:25 +02:00
class MoviesAPI extends RESTDataSource {
2018-06-25 12:29:48 -04:00
constructor() {
super();
this.baseURL = 'https://movies-api.example.com';
}
async getMovie(id) {
2018-06-19 23:47:25 +02:00
return this.get(`movies/${id}`);
}
async getMostViewedMovies(limit = 10) {
2018-06-19 23:47:25 +02:00
const data = await this.get('movies', {
per_page: limit,
order_by: 'most_viewed',
});
2018-06-19 23:47:25 +02:00
return data.results;
}
}
```
2018-06-19 23:47:25 +02:00
Data sources allow you to intercept fetches to set headers or make other changes to the outgoing request. This is most often used for authorization. Data sources also get access to the GraphQL execution context, which is a great place to store a user token or other information you need to have available.
```js
2018-06-19 23:47:25 +02:00
class PersonalizationAPI extends RESTDataSource {
2018-06-25 12:29:48 -04:00
constructor() {
super();
this.baseURL = 'https://personalization-api.example.com';
}
2018-06-19 23:47:25 +02:00
willSendRequest(request) {
2018-06-19 23:47:25 +02:00
request.headers.set('Authorization', this.context.token);
}
async getFavorites() {
return this.get('favorites');
}
async getProgressFor(movieId) {
2018-06-19 23:47:25 +02:00
return this.get('progress', {
id: movieId,
});
}
}
```
To give resolvers access to data sources, you pass them as options to the `ApolloServer` constructor:
```js
const server = new ApolloServer({
typeDefs,
resolvers,
2018-06-19 23:47:25 +02:00
dataSources: () => {
return {
moviesAPI: new MoviesAPI(),
personalizationAPI: new PersonalizationAPI(),
};
},
context: () => {
return {
token: 'foo',
2018-06-19 23:47:25 +02:00
};
},
});
```
2018-06-19 23:47:25 +02:00
Apollo Server will put the data sources on the context for every request, so you can access them from your resolvers. It will also give your data sources access to the context. (The reason for not having users put data sources on the context directly is because that would lead to a circular dependency.)
2018-06-19 23:47:25 +02:00
From our resolvers, we can access the data source and return the result:
```js
2018-06-19 23:47:25 +02:00
Query: {
movie: async (_source, { id }, { dataSources }) => {
return dataSources.moviesAPI.getMovie(id);
},
mostViewedMovies: async (_source, _args, { dataSources }) => {
return dataSources.moviesAPI.getMostViewedMovies();
},
favorites: async (_source, _args, { dataSources }) => {
return dataSources.personalizationAPI.getFavorites();
},
},
```
2018-06-19 23:47:25 +02:00
## What about DataLoader?
2018-06-19 23:47:25 +02:00
[DataLoader](https://github.com/facebook/dataloader) was designed by Facebook with a specific use case in mind: deduplicating and batching object loads from a data store. It provides a memoization cache, which avoids loading the same object multiple times during a single GraphQL request, and it coalesces loads that occur during a single tick of the event loop into a batched request that fetches multiple objects at once.
Although DataLoader is great for that use case, its less helpful when loading data from REST APIs because its primary feature is _batching_, not _caching_. What weve found to be far more important when layering GraphQL over REST APIs is having a resource cache that saves data across multiple GraphQL requests, can be shared across multiple GraphQL servers, and has cache management features like expiry and invalidation that leverage standard HTTP cache control headers.
#### Batching
Most REST APIs don't support batching, and if they do, using a batched endpoint may actually jeopardize caching. When you fetch data in a batch request, the response you receive is for the exact combination of resources you're requesting. Unless you request that same combination again, future requests for the same resource won't be served from cache.
Our recommendation is to restrict batching to requests that can't be cached. In those cases, you can actually take advantage of DataLoader as a private implementation detail inside your data source.
```js
2018-06-19 23:47:25 +02:00
class PersonalizationAPI extends RESTDataSource {
2018-06-25 12:29:48 -04:00
constructor() {
super();
this.baseURL = 'https://personalization-api.example.com';
}
2018-06-19 23:47:25 +02:00
willSendRequest(request) {
2018-06-19 23:47:25 +02:00
request.headers.set('Authorization', this.context.token);
}
private progressLoader = new DataLoader(async (ids) => {
2018-06-19 23:47:25 +02:00
const progressList = await this.get('progress', {
ids: ids.join(','),
});
return ids.map(id =>
progressList.find((progress) => progress.id === id),
2018-06-19 23:47:25 +02:00
);
});
async getProgressFor(id) {
2018-06-19 23:47:25 +02:00
return this.progressLoader.load(id);
}
```
## Using Memcached/Redis as a cache storage backend
By default, resource caching will use an in memory LRU cache. When running multiple server instances, you'll want to use a shared cache backend instead. That's why Apollo Server also includes support for using [Memcached](../../../packages/apollo-server-memcached) or [Redis](../../../packages/apollo-server-redis) as your backing store. You can specify which one to use by creating an instance and passing it into the Apollo Server constructor:
```js
const { MemcachedCache } = require('apollo-server-memcached');
const server = new ApolloServer({
typeDefs,
resolvers,
cache: new MemcachedCache(
['memcached-server-1', 'memcached-server-2', 'memcached-server-3'],
{ retries: 10, retry: 10000 }, // Options
),
dataSources: () => ({
moviesAPI: new MoviesAPI(),
}),
});
```
For documentation of the options you can pass to the underlying Memcached client, look [here](https://github.com/3rd-Eden/memcached).
```js
const { RedisCache } = require('apollo-server-redis');
const server = new ApolloServer({
typeDefs,
resolvers,
cache: new RedisCache({
host: 'redis-server',
// Options are passed through to the Redis client
}),
dataSources: () => ({
moviesAPI: new MoviesAPI(),
}),
});
```
For documentation of the options you can pass to the underlying Redis client, look [here](https://github.com/NodeRedis/node_redis).
## Implementing your own cache backend
Apollo Server exposes a `KeyValueCache` interface that you can use to implement connectors to other data stores, or to optimize for the query characteristics of your application. More information can be found in the package readme for [apollo-server-caching](https://www.npmjs.com/package/apollo-server-caching).