Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created February 13, 2019 05:10
Show Gist options
  • Save jcuffe/1bfa03b6357e0029b973d5bd1b5d74c6 to your computer and use it in GitHub Desktop.
Save jcuffe/1bfa03b6357e0029b973d5bd1b5d74c6 to your computer and use it in GitHub Desktop.
Diffs for bugfixes
diff --git a/backend/package.json b/backend/package.json
index d0b2711..b731869 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -16,6 +16,7 @@
"author": "Vincent Ning",
"license": "ISC",
"dependencies": {
+ "cors": "^2.8.5",
"express": "^4.16.4",
"express-graphql": "^0.7.1",
"graphql": "^14.1.1",
diff --git a/backend/src/data/resolvers.js b/backend/src/data/resolvers.js
index 6376c27..f20e042 100644
--- a/backend/src/data/resolvers.js
+++ b/backend/src/data/resolvers.js
@@ -12,7 +12,7 @@ const resolverMap = {
},
Mutation: {
upvotePost(_, { postId }) {
- const post = find(Posts, { id: postId });
+ const post = find(Posts, { id: Number(postId) });
if (!post) {
throw new Error(`Couldn't find post with id ${postId}`);
}
diff --git a/backend/src/server.js b/backend/src/server.js
index 02f6a22..a0d73bb 100644
--- a/backend/src/server.js
+++ b/backend/src/server.js
@@ -4,6 +4,7 @@ import resolverMap from './data/resolvers';
import { makeExecutableSchema } from 'graphql-tools';
import fs from 'fs';
import path from 'path';
+import cors from 'cors';
const schema = fs.readFileSync(path.join(__dirname, 'data/schema.graphql')).toString();
@@ -13,6 +14,7 @@ const MySchema = makeExecutableSchema({
});
const app = express();
+app.use(cors());
app.use('/graphql', graphqlHTTP({
schema: MySchema,
diff --git a/frontend/src/App.js b/frontend/src/App.js
index a8ccb4e..265d414 100644
--- a/frontend/src/App.js
+++ b/frontend/src/App.js
@@ -30,21 +30,21 @@ class App extends Component {
return <div>loading...</div>;
}
- upvote(postId) {
- const query = "mutation($postId:ID!) {upvotePost(postId:$postId) { id }}";
+ upvote = (postId) => {
+ const query = "mutation($postId:ID!) {upvotePost(postId:$postId) { id votes }}";
const variables = { postId };
fetchFromServer({ query, variables }).then(res => res.json()).then(res => {
const posts = this.state.posts;
posts.forEach(p => {
if (p.id === postId) {
- p.votes = res.upvotePost.votes;
+ p.votes = res.data.upvotePost.votes;
}
});
this.setState({ posts });
});
}
- renderPost(post) {
+ renderPost = (post) => {
const author = `${post.author.firstName} ${post.author.lastName}`;
return <li key={post.id}>
<span>"{post.title}" submitted by {author}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment