Skip to content

Commit 66f862d

Browse files
committed
Initial commit
0 parents  commit 66f862d

21 files changed

+7658
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = crlf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @dios-david

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Description
2+
3+
Please include a summary of the change here.
4+
5+
## Type of change
6+
7+
Please check options that are relevant:
8+
9+
- [ ] Bug fix (non-breaking change which fixes an issue)
10+
- [ ] New feature (non-breaking change which adds functionality)
11+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
12+
- [ ] This change requires a documentation update

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.cache
2+
node_modules
3+
dist

.np-config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"yarn": false
3+
}

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
**/*
2+
!dist/**/*
3+
!package.json
4+
!package-lock.json
5+
!LICENSE

CONTRIBUTING.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Contributing to apollo-cache-lite
2+
3+
👍🎉 Thanks for taking the time to contribute! 🎉👍
4+
5+
We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:
6+
7+
- Reporting a bug
8+
- Discussing the current state of the code
9+
- Submitting a fix
10+
- Proposing new features
11+
- Becoming a maintainer
12+
13+
## We develop with GitHub
14+
15+
We use GitHub to host the code, track issues and feature requests.
16+
17+
## Code changes happen through pull requests
18+
19+
Pull requests are the best way to propose changes to the codebase (we use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests:
20+
21+
1. Fork the repo and create your branch from `master`.
22+
2. If you've added code that should be tested, add tests.
23+
3. If you've changed APIs, update the documentation.
24+
4. Ensure the test suite passes.
25+
5. Make sure your code passes the linters.
26+
6. Open that pull request!
27+
28+
## Any contributions you make will be under the GNU GPLv3 Software License
29+
30+
In short, when you submit code changes, your submissions are understood to be under the same [GNU GPLv3](https://choosealicense.com/licenses/gpl-3.0/) that covers the project. Feel free to contact the maintainers if that's a concern.
31+
32+
## Report bugs using GitHub's [issues](https://github.com/thedevcore/apollo-cache-lite/issues)
33+
34+
We use GitHub issues to track public bugs. Report a bug by opening a new issue - it's that easy!
35+
36+
## Write bug reports with detail, background, and sample code
37+
38+
**Great Bug Reports** tend to have:
39+
40+
- A quick summary and/or background
41+
- Steps to reproduce
42+
- Be specific!
43+
- Provide a sample code if you can
44+
- What you expected would happen
45+
- What actually happens
46+
- Notes - possibly including why you think this might be happening, or stuff you tried that didn't work
47+
48+
❤️📝 People **love** detailed bug reports!
49+
50+
## License
51+
52+
By contributing, you agree that your contributions will be licensed under its GNU GPLv3 License.

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# apollo-cache-lite
2+
3+
A lightweight cache implementation for [Apollo Client](https://github.com/apollographql/apollo-client).
4+
5+
## Features
6+
7+
- 🚀 **Lightweight** - **Supports the most frequently used use-cases:** SSR, extracting cache state, restoring cache state in browser, reading fragments from cache
8+
- 🔥 **Fast** - Read/write **performance is the top priority** to make your Apollo app faster
9+
- 🚨 **Beta** - Use it carefully, it's **not production ready yet!**
10+
11+
## Getting started
12+
13+
1. Install with npm or yarn:
14+
15+
```shell
16+
npm i --save apollo-cache-lite
17+
18+
yarn add apollo-cache-lite
19+
```
20+
21+
2. Import in your app:
22+
```js
23+
import { ApolloClient } from 'apollo-client';
24+
import { ApolloCacheLite } from 'apollo-cache-lite';
25+
26+
const client = new ApolloClient({
27+
cache: new ApolloCacheLite(),
28+
// other apollo-client options…
29+
});
30+
```
31+
32+
## Options
33+
34+
The ApolloCacheLite constructor takes an optional configuration object to customize the cache:
35+
36+
### `getIdFromObject`
37+
38+
A function that takes a data object and returns a unique identifier.
39+
40+
```js
41+
import { ApolloCacheLite, getDefaultIdFromNode } from 'apollo-cache-lite';
42+
43+
const cache = new ApolloCacheLite({
44+
getIdFromObject(object) {
45+
switch (object.__typename) {
46+
case 'foo': return object.key; // use `key` as the primary key
47+
case 'bar': return `bar:${object.blah}`; // use `bar` prefix and `blah` as the primary key
48+
default: return getDefaultIdFromNode(object); // fall back to default handling
49+
}
50+
}
51+
});
52+
```

__tests__/ApolloCacheLite.spec.js

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
const { ApolloClient } = require('apollo-client');
2+
const { SchemaLink } = require('apollo-link-schema');
3+
const { makeExecutableSchema } = require('graphql-tools');
4+
const gql = require('graphql-tag');
5+
6+
const { ApolloCacheLite } = require('../src/ApolloCacheLite');
7+
const resolvers = require('./test-utils/resolvers');
8+
const typeDefs = require('./test-utils/type-defs');
9+
10+
function createApolloClient() {
11+
const schema = makeExecutableSchema({ typeDefs, resolvers });
12+
13+
return new ApolloClient({
14+
cache: new ApolloCacheLite(),
15+
link: new SchemaLink({ schema })
16+
});
17+
}
18+
19+
describe('ApolloCacheLite', () => {
20+
it('should export a class', () => {
21+
expect(ApolloCacheLite).toBeDefined();
22+
expect(new ApolloCacheLite()).toBeInstanceOf(ApolloCacheLite);
23+
});
24+
25+
it('client.query() / cache.write() :: should store query and cacheable nodes', async () => {
26+
const client = createApolloClient();
27+
28+
const { data } = await client.query({
29+
query: gql(`{
30+
getBook(id: 1) {
31+
id
32+
title
33+
author {
34+
id
35+
name
36+
}
37+
}
38+
}`)
39+
});
40+
41+
const { queries, store } = client.cache.extract();
42+
43+
expect(data).toEqual({
44+
getBook: {
45+
__typename: 'Book',
46+
id: '1',
47+
title: 'Test book',
48+
author: {
49+
__typename: 'Author',
50+
id: '1',
51+
name: 'D'
52+
}
53+
}
54+
});
55+
56+
expect(queries).toEqual({ '-702561878___3938': data });
57+
58+
expect(store).toEqual({
59+
Author: {
60+
'1': {
61+
__typename: 'Author',
62+
id: '1',
63+
name: 'D'
64+
}
65+
},
66+
Book: {
67+
'1': {
68+
__typename: 'Book',
69+
id: '1',
70+
title: 'Test book',
71+
author: {
72+
__typename: 'Author',
73+
id: '1',
74+
name: 'D'
75+
}
76+
}
77+
}
78+
});
79+
});
80+
81+
it('client.readQuery() / cache.read() :: should return cached result for a query', async () => {
82+
const client = createApolloClient();
83+
const options = {
84+
query: gql(`{
85+
getBook(id: 1) {
86+
id
87+
title
88+
author {
89+
id
90+
name
91+
}
92+
}
93+
}`)
94+
};
95+
96+
// calling client.readQuery() with cold cache should return undefined
97+
expect(await client.readQuery(options)).not.toBeDefined();
98+
99+
// cache warmup
100+
await client.query(options);
101+
102+
// test client.readQuery() with warm cache
103+
const data = await client.readQuery(options);
104+
const { queries, store } = client.cache.extract();
105+
106+
expect(data).toEqual({
107+
getBook: {
108+
__typename: 'Book',
109+
id: '1',
110+
title: 'Test book',
111+
author: {
112+
__typename: 'Author',
113+
id: '1',
114+
name: 'D'
115+
}
116+
}
117+
});
118+
119+
expect(queries).toEqual({ '-702561878___3938': data });
120+
121+
expect(store).toEqual({
122+
Author: {
123+
'1': {
124+
__typename: 'Author',
125+
id: '1',
126+
name: 'D'
127+
}
128+
},
129+
Book: {
130+
'1': {
131+
__typename: 'Book',
132+
id: '1',
133+
title: 'Test book',
134+
author: {
135+
__typename: 'Author',
136+
id: '1',
137+
name: 'D'
138+
}
139+
}
140+
}
141+
});
142+
});
143+
144+
it('client.readFragment() / cache.readFragment() :: should return cached node', async () => {
145+
const client = createApolloClient();
146+
147+
// cache warmup
148+
await client.query({
149+
query: gql(`{
150+
getBook(id: 1) {
151+
id
152+
title
153+
author {
154+
id
155+
name
156+
}
157+
}
158+
}`)
159+
});
160+
161+
// test readFragment() with warm cache
162+
const data = client.readFragment({
163+
id: 'Book:1',
164+
fragment: `
165+
fragment fullBookDetails on Book {
166+
id
167+
title
168+
author {
169+
id
170+
name
171+
}
172+
}
173+
`
174+
});
175+
176+
expect(data).toEqual({
177+
__typename: 'Book',
178+
id: '1',
179+
title: 'Test book',
180+
author: {
181+
__typename: 'Author',
182+
id: '1',
183+
name: 'D'
184+
}
185+
});
186+
});
187+
});

0 commit comments

Comments
 (0)