https://github.com/matissjanis/apollo-server-cache-sql
Minimalistic Apollo Server SQL Cache driver
https://github.com/matissjanis/apollo-server-cache-sql
apollo apollo-server cache graphql nodejs sql
Last synced: 2 months ago
JSON representation
Minimalistic Apollo Server SQL Cache driver
- Host: GitHub
- URL: https://github.com/matissjanis/apollo-server-cache-sql
- Owner: MatissJanis
- License: mit
- Created: 2020-05-23T20:58:19.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-20T21:40:40.000Z (almost 6 years ago)
- Last Synced: 2024-05-02T05:13:51.322Z (about 2 years ago)
- Topics: apollo, apollo-server, cache, graphql, nodejs, sql
- Language: TypeScript
- Size: 138 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Apollo Server SQL Cache driver
[](https://badge.fury.io/js/apollo-server-cache-sql)

[](https://codecov.io/gh/MatissJanis/apollo-server-cache-sql)
[](https://www.codacy.com/manual/matiss/apollo-server-cache-sql?utm_source=github.com&utm_medium=referral&utm_content=MatissJanis/apollo-server-cache-sql&utm_campaign=Badge_Grade)
[](https://github.com/prettier/prettier)
[](https://www.npmjs.com/package/apollo-server-cache-sql)
Minimalistic Apollo Server SQL Cache driver for times when [Redis](https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-cache-redis) or other more modern caching solutions are too expensive or unavailable.
## Installing
```sh
npm install --save-dev apollo-server-cache-sql
# or
yarn add -D apollo-server-cache-sql
```
## Setup
A SQL table for cache artifact storage must be created. The following is a blueprint for a basic caching table.
```sql
CREATE TABLE `cache` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`key` varchar(255) DEFAULT NULL,
`value` longtext,
`ttl` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `key` (`key`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
## Usage
```js
import mysql from 'mysql';
import { SqlCache } from 'apollo-server-cache-sql';
// Setup the connection client
const connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'secret',
database: 'my_db',
});
connection.connect();
// Setup Apollo Server with SQL cache driver
const server = new ApolloServer({
typeDefs,
resolvers,
cache: new SqlCache({
client: connection,
databaseName: 'my_db',
tableName: 'cache',
}),
dataSources: () => ({
moviesAPI: new MoviesAPI(),
}),
});
```
### Usage with full-query caching
```js
import mysql from 'mysql';
import { SqlCache } from 'apollo-server-cache-sql';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
// Setup the connection client
const connection = mysql.createConnection({
host: 'localhost',
user: 'me',
password: 'secret',
database: 'my_db',
});
connection.connect();
// Setup Apollo Server with SQL cache driver
const server = new ApolloServer({
// ...
plugins: [
responseCachePlugin({
cache: new SqlCache({
client: connection,
databaseName: 'my_db',
tableName: 'cache',
}),
}),
],
});
```