Skip to content

Commit

Permalink
feat(apollo-cache-inmemory): support custom directives
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gregberge committed Dec 10, 2017
1 parent 05e18c0 commit 59e3da4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/apollo-cache-inmemory/src/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ describe('reading from the store', () => {
});
});

it('runs a basic query with custom directives', () => {
const query = gql`
query {
id
firstName @include(if: true)
lastName @upperCase
birthDate @dateFormat(format: "DD-MM-YYYY")
}
`;

const store = defaultNormalizedCacheFactory({
ROOT_QUERY: {
id: 'abcd',
firstName: 'James',
'lastName@upperCase': 'BOND',
'birthDate@dateFormat({"format":"DD-MM-YYYY"})': '20-05-1940',
},
});

const result = readQueryFromStore({
store,
query,
});

expect(result).toEqual({
id: 'abcd',
firstName: 'James',
lastName: 'BOND',
birthDate: '20-05-1940',
});
});

it('runs a basic query with default values for arguments', () => {
const query = gql`
query someBigQuery(
Expand Down
32 changes: 32 additions & 0 deletions packages/apollo-cache-inmemory/src/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ describe('writing to the store', () => {
});
});

it('properly normalizes a query with custom directives', () => {
const query = gql`
query {
id
firstName @include(if: true)
lastName @upperCase
birthDate @dateFormat(format: "DD-MM-YYYY")
}
`;

const result: any = {
id: 'abcd',
firstName: 'James',
lastName: 'BOND',
birthDate: '20-05-1940',
};

const normalized = writeQueryToStore({
result,
query,
});

expect(normalized.toObject()).toEqual({
ROOT_QUERY: {
id: 'abcd',
firstName: 'James',
'lastName@upperCase': 'BOND',
'birthDate@dateFormat({"format":"DD-MM-YYYY"})': '20-05-1940',
},
});
});

it('properly normalizes a nested object with an ID', () => {
const query = gql`
{
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Lukas Jakob <[email protected]>
Divyendu Singh <[email protected]>
Paddy Hamilton <[email protected]>
Robert Simon <[email protected]>
Greg Bergé <[email protected]>
3 changes: 3 additions & 0 deletions packages/apollo-utilities/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

### vNext

### 1.1.0
- update `getStoreKeyName` to support custom directives

### 1.0.4
- package dependency updates

Expand Down
18 changes: 16 additions & 2 deletions packages/apollo-utilities/src/storeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export type Directives = {
};
};

const KNOWN_DIRECTIVES: string[] = ['connection', 'include', 'skip'];

export function getStoreKeyName(
fieldName: string,
args?: Object,
Expand Down Expand Up @@ -197,13 +199,25 @@ export function getStoreKeyName(
}
}

let completeFieldName: string = fieldName;

if (args) {
const stringifiedArgs: string = JSON.stringify(args);
completeFieldName += `(${stringifiedArgs})`;
}

return `${fieldName}(${stringifiedArgs})`;
if (directives && Object.keys(directives).length) {
Object.keys(directives).forEach(key => {
if (KNOWN_DIRECTIVES.indexOf(key) !== -1) return;
if (directives[key] && Object.keys(directives[key]).length) {
completeFieldName += `@${key}(${JSON.stringify(directives[key])})`;
} else {
completeFieldName += `@${key}`;
}
});
}

return fieldName;
return completeFieldName;
}

export function argumentsObjectFromField(
Expand Down

0 comments on commit 59e3da4

Please sign in to comment.