Skip to content

Commit

Permalink
Add updater functions for mutations that do not work with there confi…
Browse files Browse the repository at this point in the history
…gs in modern mode.

This should not be necessary in the future, but the current release does not support all config types.
  • Loading branch information
Alex Langenfeld committed Apr 26, 2017
1 parent 6513512 commit f3fd9ea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion todo-modern/js/components/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default createFragmentContainer(TodoList, {
fragment TodoList_viewer on User {
todos(
first: 2147483647 # max GraphQLInt
) {
) @connection(key: "TodoList_todos") {
edges {
node {
id,
Expand Down
11 changes: 11 additions & 0 deletions todo-modern/js/mutations/AddTodoMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
commitMutation,
graphql,
} from 'react-relay/compat';
import {ConnectionHandler} from 'relay-runtime'

const mutation = graphql`
mutation AddTodoMutation($input: AddTodoInput!) {
Expand Down Expand Up @@ -83,6 +84,16 @@ function commit(
},
configs: getConfigs(user.id),
optimisticResponse: () => getOptimisticResponse(text, user),
updater: (store) => {
const userProxy = store.get(user.id);
const payload = store.getRootField('addTodo');
const newEdge = payload.getLinkedRecord('todoEdge');
const conn = ConnectionHandler.getConnection(
userProxy,
'TodoList_todos',
);
ConnectionHandler.insertEdgeAfter(conn, newEdge);
},
}
);
}
Expand Down
13 changes: 13 additions & 0 deletions todo-modern/js/mutations/RemoveCompletedTodosMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
commitMutation,
graphql,
} from 'react-relay/compat';
import {ConnectionHandler} from 'relay-runtime';

const mutation = graphql`
mutation RemoveCompletedTodosMutation($input: RemoveCompletedTodosInput!) {
Expand Down Expand Up @@ -73,6 +74,18 @@ function commit(
},
configs: getConfigs(user),
optimisticResponse: () => getOptimisticResponse(todos, user),
updater: (store) => {
const userProxy = store.get(user.id);
const conn = ConnectionHandler.getConnection(
userProxy,
'TodoList_todos',
);
const payload = store.getRootField('removeCompletedTodos');
const deletedIDs = payload.getValue('deletedTodoIds');
deletedIDs.forEach((deletedID) =>
ConnectionHandler.deleteNode(conn, deletedID)
);
}
}
);
}
Expand Down
13 changes: 13 additions & 0 deletions todo-modern/js/mutations/RemoveTodoMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
commitMutation,
graphql,
} from 'react-relay/compat';
import {ConnectionHandler} from 'relay-runtime';

const mutation = graphql`
mutation RemoveTodoMutation($input: RemoveTodoInput!) {
Expand Down Expand Up @@ -67,6 +68,18 @@ function commit(
},
configs: getConfigs(user.id),
optimisticResponse: () => getOptimisticResponse(user, todo),
updater: (store) => {
const userProxy = store.get(user.id);
const conn = ConnectionHandler.getConnection(
userProxy,
'TodoList_todos',
);
const payload = store.getRootField('removeTodo');
ConnectionHandler.deleteNode(
conn,
payload.getValue('deletedTodoId'),
);
}
}
);
}
Expand Down
12 changes: 6 additions & 6 deletions todo-modern/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,8 @@ get-caller-file@^1.0.1:
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"

getpass@^0.1.1:
version "0.1.6"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.6.tgz#283ffd9fc1256840875311c1b60e8c40187110e6"
version "0.1.7"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"
dependencies:
assert-plus "^1.0.0"

Expand Down Expand Up @@ -1736,7 +1736,7 @@ is-binary-path@^1.0.0:
dependencies:
binary-extensions "^1.0.0"

is-buffer@^1.0.2:
is-buffer@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc"

Expand Down Expand Up @@ -1905,10 +1905,10 @@ jsprim@^1.2.2:
verror "1.3.6"

kind-of@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.1.0.tgz#475d698a5e49ff5e53d14e3e732429dc8bf4cf47"
version "3.2.0"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.0.tgz#b58abe4d5c044ad33726a8c1525b48cf891bff07"
dependencies:
is-buffer "^1.0.2"
is-buffer "^1.1.5"

lazy-cache@^1.0.3:
version "1.0.4"
Expand Down

0 comments on commit f3fd9ea

Please sign in to comment.