Repository: lucasbento/create-graphql (js)

Issue:
 Cannot read property 'toString' of undefined on Loader
we should check if `id` is valid on Loader.load

```jsx
static async load(viewer, id) {
    if (!id) return null;

    const data = await User.UserLoader.load(id.toString());

    return User.viewerCanSee(viewer, data) ? new User(data) : null;
  }
```

Pull request:
Check if id is valid on Loader.load
Fixes #44.

Patch:
diff --git a/packages/generator/src/loader/templates/Loader.js.template b/packages/generator/src/loader/templates/Loader.js.template
index 3579f45..d5f2ba5 100644
--- a/packages/generator/src/loader/templates/Loader.js.template
+++ b/packages/generator/src/loader/templates/Loader.js.template
@@ -36,6 +36,10 @@ export default class <%= name %> {
   }
 
   static async load(viewer, id) {
+    if (!id) {
+      return null;
+    }
+
     const data = await <%= name %>.<%= rawName %>Loader.load(id.toString());
 
     return <%= name %>.viewerCanSee(viewer, data) ? new <%= name %>(data) : null;
diff --git a/packages/generator/src/loader/templates/LoaderWithSchema.js.template b/packages/generator/src/loader/templates/LoaderWithSchema.js.template
index 5620351..c0fc5b0 100644
--- a/packages/generator/src/loader/templates/LoaderWithSchema.js.template
+++ b/packages/generator/src/loader/templates/LoaderWithSchema.js.template
@@ -42,6 +42,10 @@ export default class <%= name %> {
   }
 
   static async load(viewer, id) {
+    if (!id) {
+      return null;
+    }
+
     const data = await <%= name %>.<%= rawName %>Loader.load(id.toString());
 
     return <%= name %>.viewerCanSee(viewer, data) ? new <%= name %>(data) : null;


Test patch:
diff --git a/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap b/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap
index 21bf128..0306ee0 100644
--- a/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap
+++ b/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap
@@ -38,6 +38,10 @@ export default class Example {
   }
 
   static async load(viewer, id) {
+    if (!id) {
+      return null;
+    }
+
     const data = await Example.ExampleLoader.load(id.toString());
 
     return Example.viewerCanSee(viewer, data) ? new Example(data) : null;
@@ -112,6 +116,10 @@ export default class Post {
   }
 
   static async load(viewer, id) {
+    if (!id) {
+      return null;
+    }
+
     const data = await Post.PostLoader.load(id.toString());
 
     return Post.viewerCanSee(viewer, data) ? new Post(data) : null;
@@ -183,6 +191,10 @@ export default class User {
   }
 
   static async load(viewer, id) {
+    if (!id) {
+      return null;
+    }
+
     const data = await User.UserLoader.load(id.toString());
 
     return User.viewerCanSee(viewer, data) ? new User(data) : null;