Repository: lucasbento/create-graphql
Pull request #35 (2016-12-28 11:08:17)

Add timestamp fields to Loader


Patch:
diff --git a/packages/generator/src/loader/index.js b/packages/generator/src/loader/index.js
index 93e3def..5a4d6ee 100644
--- a/packages/generator/src/loader/index.js
+++ b/packages/generator/src/loader/index.js
@@ -31,7 +31,7 @@ class LoaderGenerator extends Generator {
 
   generateLoader() {
     const schema = this.options.model ?
-      getMongooseModelSchema(this.options.model)
+      getMongooseModelSchema(this.options.model, true)
       : null;
 
     const name = uppercaseFirstLetter(this.options.name);


Test patch:
diff --git a/packages/generator/src/loader/__tests__/LoaderGenerator.spec.js b/packages/generator/src/loader/__tests__/LoaderGenerator.spec.js
index fc2921a..6409101 100644
--- a/packages/generator/src/loader/__tests__/LoaderGenerator.spec.js
+++ b/packages/generator/src/loader/__tests__/LoaderGenerator.spec.js
@@ -29,7 +29,7 @@ it('generate a loader', async () => {
   expect(files).toMatchSnapshot();
 });
 
-it('generate a loader with Schema', async () => {
+it('generate a loader with schema', async () => {
   const folder = await helper.run(loaderGenerator)
     .inTmpDir(dir =>
       fs.copySync(
@@ -52,3 +52,27 @@ it('generate a loader with Schema', async () => {
 
   expect(files).toMatchSnapshot();
 });
+
+it('generate a loader with schema and without timestamps', async () => {
+  const folder = await helper.run(loaderGenerator)
+    .inTmpDir(dir =>
+      fs.copySync(
+        getFixturePath('User'),
+        path.join(dir, 'src/model/User.js'),
+      ),
+    )
+    .withArguments('User User')
+    .toPromise();
+
+  const destinationDir = getConfigDir('loader');
+
+  assert.file([
+    `${destinationDir}/UserLoader.js`,
+  ]);
+
+  const files = {
+    loader: getFileContent(`${folder}/${destinationDir}/UserLoader.js`),
+  };
+
+  expect(files).toMatchSnapshot();
+});
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 a649a47..21bf128 100644
--- a/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap
+++ b/packages/generator/src/loader/__tests__/__snapshots__/LoaderGenerator.spec.js.snap
@@ -60,7 +60,7 @@ export default class Example {
 }
 `;
 
-exports[`test generate a loader with Schema 1`] = `
+exports[`test generate a loader with schema 1`] = `
 Object {
   "loader": "// @flow
 
@@ -74,6 +74,8 @@ type PostType = {
   title: string,
   user: string,
   slug: string,
+  createdAt: string,
+  updatedAt: string,
 }
 
 export default class Post {
@@ -82,6 +84,8 @@ export default class Post {
   title: string;
   user: string;
   slug: string;
+  createdAt: string;
+  updatedAt: string;
 
   static PostLoader = new DataLoader(
     ids => Promise.all(
@@ -97,6 +101,8 @@ export default class Post {
     this.title = data.title;
     this.user = data.user;
     this.slug = data.slug;
+    this.createdAt = data.createdAt;
+    this.updatedAt = data.updatedAt;
   }
 
   static viewerCanSee(viewer, data) {
@@ -127,3 +133,74 @@ export default class Post {
 ",
 }
 `;
+
+exports[`test generate a loader with schema and without timestamps 1`] = `
+Object {
+  "loader": "// @flow
+
+import DataLoader from \'dataloader\';
+import ConnectionFromMongoCursor from \'../connection/ConnectionFromMongoCursor\';
+import UserModel from \'../model/User\';
+
+type UserType = {
+  id: string,
+  _id: string,
+  name: string,
+  password: string,
+  email: string,
+  active: boolean,
+}
+
+export default class User {
+  id: string;
+  _id: string;
+  name: string;
+  password: string;
+  email: string;
+  active: boolean;
+
+  static UserLoader = new DataLoader(
+    ids => Promise.all(
+      ids.map(id =>
+        UserModel.findOne({ _id: id })
+      ),
+    ),
+  );
+
+  constructor(data: UserType) {
+    this.id = data.id;
+    this._id = data._id;
+    this.name = data.name;
+    this.password = data.password;
+    this.email = data.email;
+    this.active = data.active;
+  }
+
+  static viewerCanSee(viewer, data) {
+    // TODO: handle security
+
+    return true;
+  }
+
+  static async load(viewer, id) {
+    const data = await User.UserLoader.load(id.toString());
+
+    return User.viewerCanSee(viewer, data) ? new User(data) : null;
+  }
+
+  static clearCache(id) {
+    return User.UserLoader.clear(id.toString());
+  }
+
+  static async loadUsers(viewer, args) {
+    // TODO: specify conditions
+    const users = UserModel.find({});
+
+    return ConnectionFromMongoCursor.connectionFromMongoCursor(
+      viewer, users, args, User.load,
+    );
+  }
+}
+",
+}
+`;