Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the filter parameter in the @connection directive #1862

Merged
merged 4 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
- Add the `filter` argument to the `@connection` directive so that custom store keys can include query arguments [PR #1862](https://github.com/apollographql/apollo-client/pull/1862)
- Add support for flow typecheck to work out of the box (without any configuration) [PR #1820](https://github.com/apollographql/apollo-client/pull/1820)
- Remove the dependency on the query and mutation store from the data reducer. Apollo actions sent to Redux now contain additional information that was originally pulled from the query and mutation stores [PR #1845](https://github.com/apollographql/apollo-client/pull/1845)
- Fix: Avoiding reprocessing of identical data when writing to the store [PR #1675](https://github.com/apollographql/apollo-client/pull/1675)
Expand Down
15 changes: 14 additions & 1 deletion src/data/storeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ export type Directives = {

export function getStoreKeyName(fieldName: string, args?: Object, directives?: Directives): string {
if (directives && directives['connection'] && directives['connection']['key']) {
return directives['connection']['key'];
if (directives['connection']['filter'] && (directives['connection']['filter'] as string[]).length > 0) {
const filterKeys = directives['connection']['filter'] ? (directives['connection']['filter'] as string[]) : [];
filterKeys.sort();

const queryArgs = args as {[key: string]: any};
const filteredArgs = {} as {[key: string]: any};
filterKeys.forEach((key) => {
filteredArgs[key] = queryArgs[key];
});

return `${directives['connection']['key']}(${JSON.stringify(filteredArgs)})`;
} else {
return directives['connection']['key'];
}
}

if (args) {
Expand Down
46 changes: 46 additions & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,52 @@ it('should run a query with the connection directive and write the result to the
});
});

it('should run a query with the connection directive and filter arguments and write the result to the correct store key', () => {
const query = gql`
query books($order: string) {
books(skip: 0, limit: 2, order: $order) @connection(key: "abc", filter: ["order"]) {
name
__typename
}
}`;

const result = {
'books': [
{
'name': 'abcd',
'__typename': 'Book',
},
],
};

const variables = {order: 'popularity'};

const networkInterface = mockNetworkInterface({
request: { query: query, variables },
result: { data: result },
});

const client = new ApolloClient({
networkInterface,
});

return client.query({ query, variables }).then((actualResult) => {
assert.deepEqual(actualResult.data, result);
assert.deepEqual(client.store.getState().apollo.data, {
'ROOT_QUERY.abc({"order":"popularity"}).0': { name: 'abcd', __typename: 'Book' },
'ROOT_QUERY': {
'abc({"order":"popularity"})': [
{
'generated': true,
'id': 'ROOT_QUERY.abc({"order":"popularity"}).0',
'type': 'id',
},
],
},
});
});
});

function clientRoundtrip(
query: DocumentNode,
data: ExecutionResult,
Expand Down