Repository: lucasbento/create-graphql (js)

Issue:
Improve Relay generation
I think we need a flag on .graphqlrc to enable some Relay generation code tweaks

for instance a Relay GraphQL Type could have a  `globalIdField()`

Changes required on Type.js.template

```jsx
import {
  globalIdField,
} from 'graphql-relay';
import {
  NodeInterface,
} from '../interface/NodeInterface';
                                     
fields: () => ({
    id: globalIdField('Company'),
    _id: {
      type: GraphQLString,
      resolve: obj => obj._id,
    },
...

interfaces: () => [NodeInterface],                                                                                 
```

Pull request description:
Add missing global id field on Type template
Fixes #25.

Patch:
diff --git a/packages/generator/src/type/templates/Type.js.template b/packages/generator/src/type/templates/Type.js.template
index b21fe4f..cad87bd 100644
--- a/packages/generator/src/type/templates/Type.js.template
+++ b/packages/generator/src/type/templates/Type.js.template
@@ -2,11 +2,13 @@ import {
   GraphQLObjectType,
   GraphQLString,
 } from 'graphql';
+import { globalIdField } from 'graphql-relay';
 
 export default new GraphQLObjectType({
   name: '<%= name %>',
   description: 'Represents <%= name %>',
   fields: () => ({
+    id: globalIdField('<%= name %>'),
     example: {
       type: GraphQLString,
       description: 'My example field',
diff --git a/packages/generator/src/type/templates/TypeWithSchema.js.template b/packages/generator/src/type/templates/TypeWithSchema.js.template
index c027c2a..da3af24 100644
--- a/packages/generator/src/type/templates/TypeWithSchema.js.template
+++ b/packages/generator/src/type/templates/TypeWithSchema.js.template
@@ -6,11 +6,13 @@ import {
   <%= schema.dependencies[i] %>,
   <%_ } -%>
 } from 'graphql';
+import { globalIdField } from 'graphql-relay';
 
 export default new GraphQLObjectType({
   name: '<%= name %>',
   description: 'Represents <%= name %>',
   fields: () => ({
+    id: globalIdField('<%= name %>'),
     <%_ for (field of schema.fields) { -%>
     <%- field.name %>: {
       type: <%= field.type %>,


Test patch:
diff --git a/packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap b/packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap
index f305ff2..46d6f34 100644
--- a/packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap
+++ b/packages/generator/src/type/__tests__/__snapshots__/TypeGenerator.spec.js.snap
@@ -3,39 +3,41 @@ Object {
   "type": "import {
   GraphQLObjectType,
   GraphQLString,
-} from 'graphql';
+} from \'graphql\';
+import { globalIdField } from \'graphql-relay\';
 
 export default new GraphQLObjectType({
-  name: 'Example',
-  description: 'Represents Example',
+  name: \'Example\',
+  description: \'Represents Example\',
   fields: () => ({
+    id: globalIdField(\'Example\'),
     example: {
       type: GraphQLString,
-      description: 'My example field',
+      description: \'My example field\',
       resolve: obj => obj.example,
     },
   }),
 });",
-  "typeTest": "import { graphql } from 'graphql';
-import { schema } from '../../schema';
-import { setupTest } from '../../../../test/helper';
+  "typeTest": "import { graphql } from \'graphql\';
+import { schema } from \'../../schema\';
+import { setupTest } from \'../../../../test/helper\';
 import {
   User,
   Example,
-} from '../../../models';
+} from \'../../../models\';
 
 beforeEach(async () => await setupTest());
 
-it('should retrieve a record', async () => {
+it(\'should retrieve a record\', async () => {
   const user = await new User({
-    name: 'user',
-    email: 'user@example.com',
+    name: \'user\',
+    email: \'user@example.com\',
   }).save();
 
   // TODO: query to return a record
   const query = \`
     query Q {
-      node(id:"123") {
+      node(id:\"123\") {
         id
       }
     }
@@ -62,11 +64,13 @@ import {
   GraphQLString,
   GraphQLID,
 } from \'graphql\';
+import { globalIdField } from \'graphql-relay\';
 
 export default new GraphQLObjectType({
   name: \'Post\',
   description: \'Represents Post\',
   fields: () => ({
+    id: globalIdField(\'Post\'),
     title: {
       type: GraphQLString,
       description: \'\',
@@ -140,55 +144,57 @@ import {
   GraphQLObjectType,
   GraphQLString,
   GraphQLBoolean,
-} from 'graphql';
+} from \'graphql\';
+import { globalIdField } from \'graphql-relay\';
 
 export default new GraphQLObjectType({
-  name: 'User',
-  description: 'Represents User',
+  name: \'User\',
+  description: \'Represents User\',
   fields: () => ({
+    id: globalIdField(\'User\'),
     name: {
       type: GraphQLString,
-      description: '',
+      description: \'\',
       resolve: obj => obj.name,
     },
     password: {
       type: GraphQLString,
-      description: '',
+      description: \'\',
       resolve: obj => obj.password,
     },
     email: {
       type: GraphQLString,
-      description: '',
+      description: \'\',
       resolve: obj => obj.email,
     },
     active: {
       type: GraphQLBoolean,
-      description: '',
+      description: \'\',
       resolve: obj => obj.active,
     },
   }),
 });
 ",
-  "typeTest": "import { graphql } from 'graphql';
-import { schema } from '../../schema';
-import { setupTest } from '../../../../test/helper';
+  "typeTest": "import { graphql } from \'graphql\';
+import { schema } from \'../../schema\';
+import { setupTest } from \'../../../../test/helper\';
 import {
   User,
   User,
-} from '../../../models';
+} from \'../../../models\';
 
 beforeEach(async () => await setupTest());
 
-it('should retrieve a record', async () => {
+it(\'should retrieve a record\', async () => {
   const user = await new User({
-    name: 'user',
-    email: 'user@example.com',
+    name: \'user\',
+    email: \'user@example.com\',
   }).save();
 
   // TODO: query to return a record
   const query = \`
     query Q {
-      node(id:"123") {
+      node(id:\"123\") {
         id
       }
     }