Skip to content

Commit

Permalink
feat(apollo-cache-inmemory): support custom directives (apollographql…
Browse files Browse the repository at this point in the history
…#2710)

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 authored and James Baxley committed Jan 29, 2018
1 parent 9265ac0 commit de38f3c
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 @@ -89,3 +89,4 @@ Robert Simon <shock.shock@gmail.com>
Sondre Maere Overskaug <sondremo@gmail.com>
Tim Griesser <tgriesser10@gmail.com>
Adam Tuttle <adamtuttlecodes@gmail.com>
Greg Bergé <berge.greg@gmail.com>
3 changes: 3 additions & 0 deletions packages/apollo-utilities/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### vNext
- Fix typo in error message for invalid argument being passed to @skip or @include directives [PR#2867](https://github.com/apollographql/apollo-client/pull/2867)

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

### 1.0.5
- 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).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 de38f3c

Please sign in to comment.