Repository: lucasbento/create-graphql (js)

Issue:
`GraphQLNonNull` & `GraphQLID` not being imported on Mutation generator
We need to improve this: https://github.com/lucasbento/create-graphql/blob/master/packages/generator/src/mutation/index.js#L32-L51

Pull request:
Fix mutation not importing correct dependencies
Fixes #22

Patch:
diff --git a/packages/generator/src/mutation/index.js b/packages/generator/src/mutation/index.js
index b3556f9..0dae9ef 100644
--- a/packages/generator/src/mutation/index.js
+++ b/packages/generator/src/mutation/index.js
@@ -29,19 +29,24 @@ class MutationGenerator extends Base {
   }
 
   _parseSchema(schema) {
-    // Remove `GraphQLString` & `GraphQLID` dependencies from import if it exists,
-    // it's already hard-coded on the template
-    const dependencies = schema.dependencies.filter(dep => ['GraphQLID', 'GraphQLString'].indexOf(dep) === -1);
+    // Remove `GraphQLString` dependency from import if it exists,
+    // it's already hard-coded on `MutationAdd` template.
+    const addDependencies = schema.dependencies.filter(dep => ['GraphQLString'].indexOf(dep) === -1);
 
-    // Map through the fields checking if any of them is required, if so, use `GraphQLNonNull`
+    // Also remove `GraphQLString`, `GraphQLNonNull` & `GraphQLID` dependencies
+    // from import if they exist, they are already hard-coded on `MutationEdit` template.
+    const editDependencies = schema.dependencies.filter(dep => ['GraphQLString', 'GraphQLNonNull', 'GraphQLID'].indexOf(dep) === -1);
+
+    // Map through the fields checking if any of them is `required: true`, if so, use `GraphQLNonNull`
     const fields = schema.fields.map((field) => {
       if (!field.required) {
         return field;
       }
 
-      // Add `GraphQLNonNull` to dependencies import if it hasn't been added yet
-      if (schema.dependencies.indexOf('GraphQLNonNull') === -1) {
-        schema.dependencies.push('GraphQLNonNull');
+      // Add `GraphQLNonNull` to `addDependencies` import if it hasn't been added yet.
+      // Won't push to `editDependencies` because it's already specified on the template file.
+      if (addDependencies.indexOf('GraphQLNonNull') === -1) {
+        addDependencies.push('GraphQLNonNull');
       }
 
       return {
@@ -53,7 +58,8 @@ class MutationGenerator extends Base {
     return {
       ...schema,
       fields,
-      dependencies,
+      addDependencies,
+      editDependencies,
     };
   }
 
diff --git a/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template b/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
index c3c8f9e..fb26c35 100644
--- a/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
+++ b/packages/generator/src/mutation/templates/MutationAddWithSchema.js.template
@@ -2,8 +2,8 @@
 
 import {
   GraphQLString,
-  <%_ for (i in schema.dependencies) { -%>
-  <%= schema.dependencies[i] %>,
+  <%_ for (i in schema.addDependencies) { -%>
+  <%= schema.addDependencies[i] %>,
   <%_ } -%>
 } from 'graphql';
 import {
diff --git a/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template b/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
index 90f81c6..eeab1bf 100644
--- a/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
+++ b/packages/generator/src/mutation/templates/MutationEditWithSchema.js.template
@@ -2,8 +2,10 @@
 
 import {
   GraphQLString,
-  <%_ for (i in schema.dependencies) { -%>
-  <%= schema.dependencies[i] %>,
+  GraphQLNonNull,
+  GraphQLID,
+  <%_ for (i in schema.editDependencies) { -%>
+  <%= schema.editDependencies[i] %>,
   <%_ } -%>
 } from 'graphql';
 import {


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 a22f349..92a25b8 100644
--- a/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap
+++ b/packages/generator/src/mutation/__tests__/__snapshots__/MutationGenerator.spec.js.snap
@@ -256,6 +256,8 @@ Object {
 
 import {
   GraphQLString,
+  GraphQLID,
+  GraphQLNonNull,
 } from \'graphql\';
 import {
   mutationWithClientMutationId,
@@ -401,6 +403,8 @@ it(\'should create a record on database\', async () => {
 
 import {
   GraphQLString,
+  GraphQLNonNull,
+  GraphQLID,
 } from \'graphql\';
 import {
   mutationWithClientMutationId,