Skip to content

Commit 5211416

Browse files
committed
implmenet resolver and schema for publishProject
1 parent 5b8789e commit 5211416

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

server/graphQL/resolvers/mutation.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Project = {
1919
id: resp._id,
2020
userId: resp.userId,
2121
likes: resp.likes,
22+
published: resp.published,
2223
});
2324
}
2425

@@ -39,7 +40,7 @@ const Project = {
3940

4041
// check if user account exists
4142
const user = await Users.findOne({ _id: userId });
42-
console.log ('User >>> ', user);
43+
4344
if (user) {
4445
// make a copy with the passed in userId
4546
const copy = {
@@ -58,7 +59,9 @@ const Project = {
5859
userId: resp.userId,
5960
username: resp.username,
6061
likes: resp.likes,
61-
})}
62+
published: resp.published,
63+
});
64+
}
6265

6366
throw new UserInputError('Internal Server Error');
6467
}
@@ -67,8 +70,28 @@ const Project = {
6770
argumentName: 'userId',
6871
});
6972
},
70-
};
7173

72-
module.exports = {
73-
ProjectMutation: Project,
74+
publishProject: async (parent, { projId, published }) => {
75+
76+
const filter = { _id: projId };
77+
const update = { published };
78+
const options = { new: true };
79+
const resp = await Projects.findOneAndUpdate(filter, update, options);
80+
if (resp) {
81+
return ({
82+
name: resp.name,
83+
id: resp._id,
84+
userId: resp.userId,
85+
likes: resp.likes,
86+
published: resp.published,
87+
});
88+
}
89+
90+
throw new UserInputError('Project is not found. Please try another project ID', {
91+
argumentName: 'projId',
92+
});
93+
},
94+
7495
};
96+
97+
module.exports = Project;

server/graphQL/resolvers/query.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const Project = {
1515
userId: resp.userId,
1616
username: resp.username,
1717
likes: resp.likes,
18+
published: resp.published,
1819
});
1920
}
2021

@@ -44,16 +45,15 @@ const Project = {
4445
userId: proj.userId,
4546
username: proj.username,
4647
likes: proj.likes,
48+
published: proj.published,
4749
}));
4850
}
4951

50-
// resp is null, return error message
52+
// resp is null, return error message
5153
throw new UserInputError('Internal Server Error');
5254
},
5355

5456

5557
};
5658

57-
module.exports = {
58-
ProjectQuery: Project,
59-
};
59+
module.exports = Project;

server/graphQL/typeDefs.js renamed to server/graphQL/schema/typeDefs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ const Project = gql`
1212
addLike(projId: ID!, likes: Int!): Project
1313
makeCopy(projId: ID!, userId: ID!, username: String!): Project
1414
deleteProject(projId: ID!): Project
15+
publishProject(projId: ID!, published: Boolean!): Project
1516
}
1617
1718
type Project {
1819
name: String!
1920
likes: Int
21+
published: Boolean!
2022
id: ID!
2123
userId: ID!
2224
username: String!
@@ -28,6 +30,4 @@ const Project = gql`
2830
}
2931
`;
3032

31-
module.exports = {
32-
typeDefsProject: Project,
33-
};
33+
module.exports = Project;

server/models/reactypeModels.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ const sessionSchema = new Schema({
5959
const projectSchema = new Schema({
6060
name: String,
6161
likes: { type: Number, default: 0 },
62+
published: { type: Boolean, default: false },
6263
project: { type: Object, required: true },
6364
userId: {
6465
type: Schema.Types.ObjectId,
6566
ref: 'Users',
6667
},
67-
username: {type: String, required: true },
68-
createdAt: { type: Date, default: Date.now }
69-
}, { minimize: false }
70-
);
68+
username: { type: String, required: true },
69+
createdAt: { type: Date, default: Date.now },
70+
}, { minimize: false });
7171
// option 'minimize' prevent Mongoose from removing empty 'style' value in the copy
7272

7373
// Test schema for implementing GraphQL

server/server.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ GraphQl Router
5959
const { ApolloServer } = require('apollo-server-express');
6060

6161
// Query resolvers
62-
const { ProjectQuery } = require('./graphQL/resolvers/query');
62+
const Query = require('./graphQL/resolvers/query');
6363
// Mutation resolvers
64-
const { ProjectMutation } = require('./graphQL/resolvers/mutation');
64+
const Mutation = require('./graphQL/resolvers/mutation');
6565

6666
// package resolvers into one variable to pass to Apollo Server
6767
const resolvers = {
68-
Query: ProjectQuery,
69-
Mutation: ProjectMutation,
68+
Query,
69+
Mutation,
7070
};
7171

7272
// schemas used for graphQL
73-
const { typeDefsProject } = require('./graphQL/typeDefs');
73+
const typeDefs = require('./graphQL/schema/typeDefs.js');
7474

7575
// instantiate Apollo server and attach to Express server, mounted at 'http://localhost:PORT/graphql'
76-
const server = new ApolloServer({ typeDefs: typeDefsProject, resolvers });
76+
const server = new ApolloServer({ typeDefs, resolvers });
7777
server.applyMiddleware({ app });
7878
/** ****************************************************************** */
7979

0 commit comments

Comments
 (0)