Skip to content

Commit 6b26b1b

Browse files
committed
implement schema and resolvers for Projects (query & mutation)
1 parent e4d37c6 commit 6b26b1b

File tree

5 files changed

+145
-15
lines changed

5 files changed

+145
-15
lines changed

server/graphQL/resolvers/mutation.js

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const { Tests } = require('../../models/reactypeModels');
1+
const { Tests, Projects } = require('../../models/reactypeModels');
22
/*
33
* resolvers are functions that handles graphQL requests. This file defines the logic for graphQL mutation requests
44
* Link to Apollo Mutations:
55
* https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
66
*/
77

8-
9-
module.exports = {
10-
8+
const Test = {
119
addTest: async (parent, args) => {
1210
const resp = await Tests.create({ name: args.name, likes: args.likes });
1311
console.log('Added test', resp);
@@ -24,7 +22,6 @@ module.exports = {
2422

2523
console.log('Updated database with', resp);
2624

27-
2825
if (resp) return { description: resp.name, id: resp._id, likes: resp.likes };
2926
return { description: 'Error updating', id: resp._id, likes: 0 };
3027
},
@@ -36,3 +33,63 @@ module.exports = {
3633
return { description: 'Error updating', likes: 0 };
3734
},
3835
};
36+
37+
38+
const Project = {
39+
addLike: async (parent, { projId, likes}) => {
40+
const filter = { _id: projId };
41+
const update = { likes };
42+
const options = { new: true };
43+
const resp = await Projects.findOneAndUpdate(filter, update, options);
44+
45+
if (resp) {
46+
return ({
47+
name: resp.name,
48+
projId: resp._id,
49+
userId: resp.userId,
50+
likes: resp.likes,
51+
})}
52+
53+
// TODO: Go back to this to see how to handle error later
54+
return {
55+
name: 'Error',
56+
projId: 'Error',
57+
userId: 'Error',
58+
likes: -1,
59+
};
60+
},
61+
62+
makeCopy: async (parent, { projId, userId }) => {
63+
const filter = { _id: projId };
64+
const target = await Projects.findOne(filter);
65+
// make a copy with the passed in userId
66+
const copy = {
67+
name: target.name,
68+
likes: target.likes,
69+
project: target.project,
70+
userId,
71+
};
72+
const resp = await Projects.create(copy);
73+
74+
if (resp) {
75+
return ({
76+
name: resp.name,
77+
projId: resp._id,
78+
userId: resp.userId,
79+
likes: resp.likes,
80+
})}
81+
82+
// TODO: Go back to this to see how to handle error later
83+
return {
84+
name: 'Error',
85+
projId: 'Error',
86+
userId: 'Error',
87+
likes: -1,
88+
};
89+
},
90+
};
91+
92+
module.exports = {
93+
TestMutation: Test,
94+
ProjectMutation: Project,
95+
};

server/graphQL/resolvers/query.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const { Tests } = require('../../models/reactypeModels');
1+
const { Tests, Projects } = require('../../models/reactypeModels');
22

33
// Link to Apollo Query Types:
44
// https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
55

6-
module.exports = {
6+
const Test = {
7+
78
readTest: async (parent, args) => {
89
const resp = await Tests.findOne({ _id: args.id });
910
if (resp) return { description: resp.name, id: resp._id, likes: resp.likes };
1011
return { description: 'Error reading', id: '0', likes: '0' };
1112
},
13+
1214
readAllTests: async () => {
1315
const resp = await Tests.find({});
1416
// console.log('resp', resp);
@@ -18,4 +20,52 @@ module.exports = {
1820
// TODO: Go back to this to see how to handle error later
1921
return [{ description: 'Error reading all tests', id: '0', likes: '0' }];
2022
},
23+
24+
};
25+
26+
27+
const Project = {
28+
getProject: async (parent, { projId }) => {
29+
const resp = await Projects.findOne({ _id: projId });
30+
if (resp) {
31+
return ({
32+
name: resp.name,
33+
projId: resp._id,
34+
userId: resp.userId,
35+
likes: resp.likes,
36+
})}
37+
38+
// TODO: Go back to this to see how to handle error later
39+
return {
40+
name: 'Error',
41+
projId: 'Error',
42+
userId: 'Error',
43+
likes: -1,
44+
};
45+
},
46+
47+
getAllProjects: async () => {
48+
const resp = await Projects.find({});
49+
if (resp) {
50+
return resp.map(proj => ({
51+
name: proj.name,
52+
projId: proj._id,
53+
userId: proj.userId,
54+
likes: proj.likes,
55+
}));
56+
}
57+
// TODO: Go back to this to see how to handle error later
58+
return [{
59+
name: 'Error',
60+
projId: 'Error',
61+
userId: 'Error',
62+
likes: -1,
63+
}];
64+
},
65+
66+
};
67+
68+
module.exports = {
69+
TestQuery: Test,
70+
ProjectQuery: Project,
2171
};

server/graphQL/typeDefs.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { gql } = require('apollo-server-express');
66
// The schema specifies which queries and mutations are available for clients
77
// to execute against your data graph.
88

9-
const typeDefs = gql`
9+
const Test = gql`
1010
1111
type Mutation {
1212
addTest(name: String, likes: Int): Test
@@ -24,6 +24,28 @@ const typeDefs = gql`
2424
}
2525
`;
2626

27+
//NOTE: Project type does not return the detail of the project's components, but info needed for the dashboard
28+
const Project = gql`
29+
30+
type Mutation {
31+
addLike(projId: ID!, likes: Int!): Project
32+
makeCopy(projId: ID!, userId: ID!): Project
33+
}
34+
35+
type Project {
36+
name: String!
37+
likes: Int
38+
projId: ID!
39+
userId: ID!
40+
}
41+
42+
type Query {
43+
getProject(projId: ID!): Project
44+
getAllProjects: [Project]
45+
}
46+
`;
47+
2748
module.exports = {
28-
typeDefs,
49+
typeDefsTest: Test,
50+
typeDefsProject: Project,
2951
};

server/models/reactypeModels.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const sessionSchema = new Schema({
5858

5959
const projectSchema = new Schema({
6060
name: String,
61+
likes: Number,
6162
project: { type: Object, required: true },
6263
userId: {
6364
type: Schema.Types.ObjectId,

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 query = require('./graphQL/resolvers/query');
62+
const { TestQuery, ProjectQuery } = require('./graphQL/resolvers/query');
6363
// Mutation resolvers
64-
const mutation = require('./graphQL/resolvers/mutation');
64+
const { TestMutation, ProjectMutation } = require('./graphQL/resolvers/mutation');
6565

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

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

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

0 commit comments

Comments
 (0)