Skip to content

Commit

Permalink
adust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jul 19, 2023
1 parent b559c9d commit ded8571
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
8 changes: 5 additions & 3 deletions docs/graphcache/local-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ If you want to add directives yourself you can do so by performing
cacheExchange({
directives: {
// If you now add `@_pagination` to your document we will execute this
pagination: () => {},
pagination: directiveArguments => () => {
/* Resolver */
},
},
});
```

The function signature of a directive is the same as the one of a [Resolver](./local-directives.md). In
case you need to access the arguments you have passed to a directive you can do so by checking `info.directiveArguments`.
The function signature of a directive is a function which receives the arguments the directive is called with in the document.
That function should returns a [Resolver](./local-directives.md).

### Reading on

Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/extras/relayPagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ describe('as directive', () => {

const store = new Store({
directives: {
relayPagination: relayPagination(),
relayPagination: relayPagination,
},
});

Expand Down
72 changes: 71 additions & 1 deletion exchanges/graphcache/src/extras/simplePagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ describe('as directive', () => {

const store = new Store({
directives: {
simplePagination: simplePagination(),
simplePagination: simplePagination,
},
});

Expand Down Expand Up @@ -508,4 +508,74 @@ describe('as directive', () => {
});
expect(pageThreeResult.data).toEqual(null);
});

it('works with backwards pagination', () => {
const Pagination = gql`
query ($skip: Number, $limit: Number) {
__typename
persons(skip: $skip, limit: $limit)
@_simplePagination(mergeMode: "before") {
__typename
id
name
}
}
`;

const store = new Store({
directives: {
simplePagination: simplePagination,
},
});

const pageOne = {
__typename: 'Query',
persons: [
{ id: 7, name: 'Jovi', __typename: 'Person' },
{ id: 8, name: 'Phil', __typename: 'Person' },
{ id: 9, name: 'Andy', __typename: 'Person' },
],
};

const pageTwo = {
__typename: 'Query',
persons: [
{ id: 4, name: 'Kadi', __typename: 'Person' },
{ id: 5, name: 'Dom', __typename: 'Person' },
{ id: 6, name: 'Sofia', __typename: 'Person' },
],
};

write(
store,
{ query: Pagination, variables: { skip: 0, limit: 3 } },
pageOne
);
const pageOneResult = query(store, {
query: Pagination,
variables: { skip: 0, limit: 3 },
});
expect(pageOneResult.data).toEqual(pageOne);

write(
store,
{ query: Pagination, variables: { skip: 3, limit: 3 } },
pageTwo
);

const pageTwoResult = query(store, {
query: Pagination,
variables: { skip: 3, limit: 3 },
});
expect((pageTwoResult.data as any).persons).toEqual([
...pageTwo.persons,
...pageOne.persons,
]);

const pageThreeResult = query(store, {
query: Pagination,
variables: { skip: 6, limit: 3 },
});
expect(pageThreeResult.data).toEqual(null);
});
});
4 changes: 2 additions & 2 deletions exchanges/graphcache/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ type DocumentNode = TypedDocumentNode<any, any>;
type RootField = 'query' | 'mutation' | 'subscription';

const defaultDirectives: DirectivesConfig = {
optional: (_parent, args, cache, info) => {
optional: () => (_parent, args, cache, info) => {
const result = cache.resolve(info.parentFieldKey, info.fieldName, args);
return result === undefined ? null : result;
},
required: (_parent, args, cache, info) => {
required: () => (_parent, args, cache, info) => {
const result = cache.resolve(info.parentFieldKey, info.fieldName, args);
return result === null ? undefined : result;
},
Expand Down

0 comments on commit ded8571

Please sign in to comment.