Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
(docs) - linking in resolve (#89)
Browse files Browse the repository at this point in the history
* (docs) - document how to link from a list to details

* (tests) - add a test that test link from list to details
  • Loading branch information
JoviDeCroock authored Sep 24, 2019
1 parent 8748862 commit 0c2229a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ console.log(name); // 'Bar'
This can help solve practical use cases like date formatting,
where you would query the date and then convert it in your resolver.

You can also link entities that come from a list, imagine the scenario where
we have queried `todos` but now want the detailView of a single `todo`.

```js
const cache = cacheExchange({
resolvers: {
Query: { todo: (parent, args) => ({ __typename: 'Todo', id: args.id }) },
},
});
```

will do the trick.

## `cache.readQuery`

Another method the cache allows is to let you read a full query, this method
Expand Down
44 changes: 43 additions & 1 deletion src/test-utils/examples-1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const Todos = gql`
}
`;

const Todo = gql`
query($id: ID!) {
__typename
todo(id: $id) {
id
text
complete
}
}
`;

const ToggleTodo = gql`
mutation($id: ID!) {
__typename
Expand Down Expand Up @@ -150,6 +161,37 @@ it('resolves missing, nullable arguments on fields', () => {
expect(data).toEqual(writeData);
});

it('should link entities', () => {
const store = new Store(undefined, {
Query: {
todo: (_parent, args) => {
return { __typename: 'Todo', ...args };
},
},
});

const todosData = {
__typename: 'Query',
todos: [
{ id: '0', text: 'Go to the shops', complete: false, __typename: 'Todo' },
{ id: '1', text: 'Pick up the kids', complete: true, __typename: 'Todo' },
{ id: '2', text: 'Install urql', complete: false, __typename: 'Todo' },
],
};

write(store, { query: Todos }, todosData);
const res = query(store, { query: Todo, variables: { id: '0' } });
expect(res.data).toEqual({
__typename: 'Query',
todo: {
id: '0',
text: 'Go to the shops',
complete: false,
__typename: 'Todo',
},
});
});

it('respects property-level resolvers when given', () => {
const store = new Store(undefined, {
Todo: { text: () => 'hi' },
Expand Down Expand Up @@ -210,7 +252,7 @@ it('respects property-level resolvers when given', () => {
});
});

it('respects property-level resolvers when given', () => {
it('respects Mutation update functions', () => {
const store = new Store(undefined, undefined, {
Mutation: {
toggleTodo: function toggleTodo(result, _, cache) {
Expand Down

0 comments on commit 0c2229a

Please sign in to comment.