Skip to content

Commit

Permalink
Changed resolvers to return correct types
Browse files Browse the repository at this point in the history
  • Loading branch information
NataliaTepluhina committed Apr 29, 2019
1 parent 9c7729c commit fbcd37c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 8 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title>vue-todo-apollo</title>
</head>
<body>
<noscript>
<strong>We're sorry but vue-todo-apollo doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
<strong
>We're sorry but vue-todo-apollo doesn't work properly without
JavaScript enabled. Please enable it to continue.</strong
>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
Expand Down
14 changes: 12 additions & 2 deletions src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const typeDefs = gql`
text: String!
done: Boolean!
}
extend type Mutation {
changeItem(id: ID!): Boolean
deleteItem(id: ID!): Boolean
addItem(text: String!): Item
}
`;

export const resolvers = {
Expand All @@ -17,24 +23,28 @@ export const resolvers = {
const currentItem = data.todoItems.find(item => item.id === id);
currentItem.done = !currentItem.done;
cache.writeQuery({ query: todoItemsQuery, data });
return currentItem.done;
},

addItem: (_, { text }, { cache }) => {
const data = cache.readQuery({ query: todoItemsQuery });
data.todoItems.push({
const newItem = {
__typename: 'Item',
id: shortid.generate(),
text,
done: false,
});
};
data.todoItems.push(newItem);
cache.writeQuery({ query: todoItemsQuery, data });
return newItem;
},

deleteItem: (_, { id }, { cache }) => {
const data = cache.readQuery({ query: todoItemsQuery });
const currentItem = data.todoItems.find(item => item.id === id);
data.todoItems.splice(data.todoItems.indexOf(currentItem), 1);
cache.writeQuery({ query: todoItemsQuery, data });
return data.todoItems;
},
},
};

0 comments on commit fbcd37c

Please sign in to comment.