Repository: lucasbento/create-graphql (js)

Issue:
Improve Mutation templates to have a pre-ready Mongoose save/update
The generated templates should have a code to save or edit the record in `MutationAdd` and `MutationEdit`.

Pull request description:
Improve mutation generated code
Fixes #40.

Patch:
diff --git a/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template b/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
index fb26c35..0294f23 100644
--- a/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
+++ b/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
@@ -37,10 +37,17 @@ export default mutationWithClientMutationId({
       <%_ } -%>
     } = args;
 
+    // Create new record
+    const <%= camelCaseName %> = await new <%= name %>({
+      <%_ for (field of schema.fields) { -%>
+      <%- field.name %>,
+      <%_ } -%>
+    }).save();
+
     // TODO: mutation logic
 
     return {
-      // id: id, // ID of the newly created row
+      id: <%= camelCaseName %>._id,
       error: null,
     };
   },
diff --git a/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template b/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
index eeab1bf..ace5b9c 100644
--- a/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
+++ b/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
@@ -53,6 +53,13 @@ export default mutationWithClientMutationId({
       throw new Error('Invalid <%= camelCaseName %>Id');
     }
 
+    // Edit record
+    await <%= camelCaseName %>.update({
+      <%_ for (field of schema.fields) { -%>
+      <%- field.name %>,
+      <%_ } -%>
+    });
+
     // TODO: mutation logic
 
     // Clear dataloader cache


Test patch:
diff --git a/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap b/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap
index af0822a..12341d5 100644
--- a/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap
+++ b/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap
@@ -294,10 +294,17 @@ export default mutationWithClientMutationId({
       slug,
     } = args;
 
+    // Create new record
+    const post = await new Post({
+      title,
+      user,
+      slug,
+    }).save();
+
     // TODO: mutation logic
 
     return {
-      // id: id, // ID of the newly created row
+      id: post._id,
       error: null,
     };
   },
@@ -455,6 +462,13 @@ export default mutationWithClientMutationId({
       throw new Error(\'Invalid postId\');
     }
 
+    // Edit record
+    await post.update({
+      title,
+      user,
+      slug,
+    });
+
     // TODO: mutation logic
 
     // Clear dataloader cache