Skip to content

Commit

Permalink
feat: update squiggle (#207)
Browse files Browse the repository at this point in the history
* feat: added mutation to update squiggle

* feat: writes mutation to update squiggle

* fix: added resolve

* fix: imported graphqlid

* fix: removed squiggletype from update mutation

* fix: updated permissions

* feat: added mongoose code to update

* fix: given permission

* fix: made changes

* feat: added error message
  • Loading branch information
Srish-ty authored Oct 21, 2023
1 parent 61d4ff0 commit 431da79
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
15 changes: 15 additions & 0 deletions server/schema/squiggle/squiggle.datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,26 @@ const create = async (squiggleType, content) => {
}
};

const updateContent = async (id, newContent) => {
try {
const _squiggle = await SquiggleModel.findById(id);

_squiggle.content = newContent || _squiggle.content;

await _squiggle.save();

return _squiggle;
} catch (error) {
throw APIError("failed to update squiggle content.", error);
}
};

const SquiggleDataSources = () => ({
getLatest,
findByID,
find,
create,
updateContent
});

module.exports = SquiggleDataSources;
15 changes: 13 additions & 2 deletions server/schema/squiggle/squiggle.mutation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { GraphQLObjectType, GraphQLNonNull, GraphQLString } = require('../scalars');
const { createSquiggle } = require('./squiggle.resolver');
const { GraphQLObjectType, GraphQLNonNull, GraphQLID ,GraphQLString } = require('../scalars');
const { createSquiggle, updateSquiggle } = require('./squiggle.resolver');

const SquiggleType = require('./squiggle.type');

Expand All @@ -13,5 +13,16 @@ module.exports = new GraphQLObjectType({
},
resolve: createSquiggle,
},
updateSquiggle: {
type: SquiggleType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: "The squiggle's mongo ID",
},
newContent: { type: GraphQLString },
},
resolve: updateSquiggle,
},
}
});
25 changes: 25 additions & 0 deletions server/schema/squiggle/squiggle.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ module.exports = {
throw APIError(null, error);
}
},

updateSquiggle: async (
_parent,
{ id, newContent },
{ session, authToken, decodedToken, API: { Squiggle } }
) => {
try {
if (!UserPermission.exists(session, authToken, decodedToken, 'squiggle.write.all')) {
throw APIError('FORBIDDEN', null, {
reason: 'The user does not have the required permissions to update squiggles.',
});
}
let _squiggle = await Squiggle.findByID(id);

if (!_squiggle) {
throw APIError('NOT_FOUND', null, { reason: 'The squiggle to update does not exist.',});
}

_squiggle = await Squiggle.updateContent(id, newContent);
return _squiggle;
} catch (error) {
throw APIError(null, error);
}
},

getLatestSquiggle: async (_parent, _args, { API: { Squiggle } }, _) => {
try {
const _squiggle = await Squiggle.getLatest();
Expand Down

0 comments on commit 431da79

Please sign in to comment.