Skip to content

Commit 87d6548

Browse files
committed
feat(apollo-cache-inmemory): support custom directives
Before this change, using a custom directive corrupted the cache. It is now possible to safely use a custom directive without breaking it. Example of custom directives: https://github.com/smooth-code/graphql-directive
1 parent 05e18c0 commit 87d6548

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

packages/apollo-cache-inmemory/src/__tests__/readFromStore.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,38 @@ describe('reading from the store', () => {
165165
});
166166
});
167167

168+
it('runs a basic query with custom directives', () => {
169+
const query = gql`
170+
query {
171+
id
172+
firstName @include(if: true)
173+
lastName @upperCase
174+
birthDate @dateFormat(format: "DD-MM-YYYY")
175+
}
176+
`;
177+
178+
const store = defaultNormalizedCacheFactory({
179+
ROOT_QUERY: {
180+
id: 'abcd',
181+
firstName: 'James',
182+
'lastName@upperCase': 'BOND',
183+
'birthDate@dateFormat({"format":"DD-MM-YYYY"})': '20-05-1940',
184+
},
185+
});
186+
187+
const result = readQueryFromStore({
188+
store,
189+
query,
190+
});
191+
192+
expect(result).toEqual({
193+
id: 'abcd',
194+
firstName: 'James',
195+
lastName: 'BOND',
196+
birthDate: '20-05-1940',
197+
});
198+
});
199+
168200
it('runs a basic query with default values for arguments', () => {
169201
const query = gql`
170202
query someBigQuery(

packages/apollo-cache-inmemory/src/__tests__/writeToStore.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,38 @@ describe('writing to the store', () => {
221221
});
222222
});
223223

224+
it('properly normalizes a query with custom directives', () => {
225+
const query = gql`
226+
query {
227+
id
228+
firstName @include(if: true)
229+
lastName @upperCase
230+
birthDate @dateFormat(format: "DD-MM-YYYY")
231+
}
232+
`;
233+
234+
const result: any = {
235+
id: 'abcd',
236+
firstName: 'James',
237+
lastName: 'BOND',
238+
birthDate: '20-05-1940',
239+
};
240+
241+
const normalized = writeQueryToStore({
242+
result,
243+
query,
244+
});
245+
246+
expect(normalized.toObject()).toEqual({
247+
ROOT_QUERY: {
248+
id: 'abcd',
249+
firstName: 'James',
250+
'lastName@upperCase': 'BOND',
251+
'birthDate@dateFormat({"format":"DD-MM-YYYY"})': '20-05-1940',
252+
},
253+
});
254+
});
255+
224256
it('properly normalizes a nested object with an ID', () => {
225257
const query = gql`
226258
{

packages/apollo-utilities/src/storeUtils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export type Directives = {
164164
};
165165
};
166166

167+
const KNOWN_DIRECTIVES: string[] = ['connection', 'include', 'skip'];
168+
167169
export function getStoreKeyName(
168170
fieldName: string,
169171
args?: Object,
@@ -197,13 +199,25 @@ export function getStoreKeyName(
197199
}
198200
}
199201

202+
let completeFieldName: string = fieldName;
203+
200204
if (args) {
201205
const stringifiedArgs: string = JSON.stringify(args);
206+
completeFieldName += `(${stringifiedArgs})`;
207+
}
202208

203-
return `${fieldName}(${stringifiedArgs})`;
209+
if (directives && Object.keys(directives).length) {
210+
Object.keys(directives).forEach(key => {
211+
if (KNOWN_DIRECTIVES.indexOf(key) !== -1) return;
212+
if (directives[key] && Object.keys(directives[key]).length) {
213+
completeFieldName += `@${key}(${JSON.stringify(directives[key])})`;
214+
} else {
215+
completeFieldName += `@${key}`;
216+
}
217+
});
204218
}
205219

206-
return fieldName;
220+
return completeFieldName;
207221
}
208222

209223
export function argumentsObjectFromField(

0 commit comments

Comments
 (0)