Repository: apollographql/apollo-codegen
Pull request #361 (2018-01-18 01:15:46)

Remove `mkdirp`.
It doesn't seem to be used.


Patch:
diff --git a/package-lock.json b/package-lock.json
index 162c4e63..3eaceeb4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -113,15 +113,6 @@
       "integrity": "sha512-rUO/jz10KRSyA9SHoCWQ8WX9BICyj5jZYu1/ucKEJKb4KzLZCKMURdYbadP157Q6Zl1x0vHsrU+Z/O0XlhYQDw==",
       "dev": true
     },
-    "@types/mkdirp": {
-      "version": "0.5.2",
-      "resolved": "https://registry.npmjs.org/@types/mkdirp/-/mkdirp-0.5.2.tgz",
-      "integrity": "sha512-U5icWpv7YnZYGsN4/cmh3WD2onMY0aJIiTE6+51TwJCttdHvtCYmkBNOobHlXwrJRL0nkH9jH4kD+1FAdMN4Tg==",
-      "dev": true,
-      "requires": {
-        "@types/node": "8.0.47"
-      }
-    },
     "@types/node": {
       "version": "8.0.47",
       "resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.47.tgz",
@@ -2077,14 +2068,6 @@
             }
           }
         },
-        "string_decoder": {
-          "version": "1.0.1",
-          "bundled": true,
-          "dev": true,
-          "requires": {
-            "safe-buffer": "5.0.1"
-          }
-        },
         "string-width": {
           "version": "1.0.2",
           "bundled": true,
@@ -2095,6 +2078,14 @@
             "strip-ansi": "3.0.1"
           }
         },
+        "string_decoder": {
+          "version": "1.0.1",
+          "bundled": true,
+          "dev": true,
+          "requires": {
+            "safe-buffer": "5.0.1"
+          }
+        },
         "stringstream": {
           "version": "0.0.5",
           "bundled": true,
@@ -3642,12 +3633,14 @@
     "minimist": {
       "version": "0.0.8",
       "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
-      "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
+      "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
+      "dev": true
     },
     "mkdirp": {
       "version": "0.5.1",
       "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
       "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
+      "dev": true,
       "requires": {
         "minimist": "0.0.8"
       }
@@ -4411,15 +4404,6 @@
       "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=",
       "dev": true
     },
-    "string_decoder": {
-      "version": "1.0.3",
-      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
-      "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
-      "dev": true,
-      "requires": {
-        "safe-buffer": "5.1.1"
-      }
-    },
     "string-length": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/string-length/-/string-length-2.0.0.tgz",
@@ -4465,6 +4449,15 @@
         }
       }
     },
+    "string_decoder": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz",
+      "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==",
+      "dev": true,
+      "requires": {
+        "safe-buffer": "5.1.1"
+      }
+    },
     "stringstream": {
       "version": "0.0.5",
       "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz",
diff --git a/package.json b/package.json
index 8bc76d25..2fff4307 100644
--- a/package.json
+++ b/package.json
@@ -32,7 +32,6 @@
     "@types/graphql": "^0.11.7",
     "@types/inflected": "^1.1.29",
     "@types/jest": "^21.1.8",
-    "@types/mkdirp": "^0.5.2",
     "@types/node-fetch": "^1.6.7",
     "@types/rimraf": "^2.0.2",
     "@types/yargs": "^10.0.0",
@@ -51,7 +50,6 @@
     "graphql": "^0.12.3",
     "graphql-config": "^1.1.1",
     "inflected": "^2.0.3",
-    "mkdirp": "^0.5.1",
     "node-fetch": "^1.7.3",
     "rimraf": "^2.6.2",
     "source-map-support": "^0.5.0",
diff --git a/src/cli.js b/src/cli.js
index 48d44290..99807c2f 100755
--- a/src/cli.js
+++ b/src/cli.js
@@ -39,7 +39,9 @@ yargs
         coerce: (arg) => {
           let additionalHeaders = {};
           for (const header of arg) {
-            const [name, value] = header.split(/\s*:\s*/);
+            const separator = header.indexOf(":");
+            const name = header.substring(0, separator).trim();
+            const value = header.substring(separator + 1).trim();
             if (!(name && value)) {
               throw new ToolError('Headers should be specified as "Name: Value"');
             }
diff --git a/src/flow/codeGeneration.js b/src/flow/codeGeneration.js
index c5f0d8b2..cac87dcf 100644
--- a/src/flow/codeGeneration.js
+++ b/src/flow/codeGeneration.js
@@ -374,7 +374,19 @@ export function propertyDeclarations(generator, properties, isInput) {
         || property.fragmentSpreads && property.fragmentSpreads.length > 0
       ) {
         propertyDeclaration(generator, property, () => {
-          const properties = propertiesFromFields(generator.context, property.fields);
+          const fields = property.fields.map(field => {
+            if (field.fieldName === '__typename') {
+              const objectTypeName = getObjectTypeName(property.fieldType || property.type);
+              return {
+                ...field,
+                typeName: objectTypeName,
+                type: { name: objectTypeName }
+              }
+            } else {
+              return field;
+            }
+          });
+          const properties = propertiesFromFields(generator.context, fields);
           propertyDeclarations(generator, properties, isInput);
         });
       } else {
diff --git a/src/typescript/codeGeneration.ts b/src/typescript/codeGeneration.ts
index bc8f10c0..afe02aef 100644
--- a/src/typescript/codeGeneration.ts
+++ b/src/typescript/codeGeneration.ts
@@ -395,7 +395,19 @@ export function propertyDeclarations(generator: CodeGenerator, properties: Prope
         || property.fragmentSpreads && property.fragmentSpreads.length > 0
       ) {
         propertyDeclaration(generator, property, () => {
-          const properties = propertiesFromFields(generator.context, property.fields!);
+          const fields = property.fields!.map(field => {
+            if (field.fieldName === '__typename') {
+              const objectTypeName = getObjectTypeName(property.fieldType || property.type!);
+              return {
+                ...field,
+                typeName: objectTypeName,
+                type: { name: objectTypeName }
+              }
+            } else {
+              return field;
+            }
+          });
+          const properties = propertiesFromFields(generator.context, fields);
           propertyDeclarations(generator, properties, isInput);
         });
       } else {


Test patch:
diff --git a/test/flow/__snapshots__/codeGeneration.js.snap b/test/flow/__snapshots__/codeGeneration.js.snap
index d3e28fb8..056cb715 100644
--- a/test/flow/__snapshots__/codeGeneration.js.snap
+++ b/test/flow/__snapshots__/codeGeneration.js.snap
@@ -520,6 +520,65 @@ export type CustomScalarQuery = {|
 |};"
 `;
 
+exports[`Flow code generation #generateSource() should have __typename value in nested property 1`] = `
+"/* @flow */
+/* eslint-disable */
+//  This file was automatically generated and should not be edited.
+
+export type HeroNameQuery = {|
+  hero: ?( {
+      __typename: \\"Human\\",
+      // The friends of the human exposed as a connection with edges
+      friendsConnection: {|
+        __typename: \\"FriendsConnection\\",
+        // The edges for each of the character's friends.
+        edges: ? Array<? {|
+          __typename: \\"FriendsEdge\\",
+          // The character represented by this friendship edge
+          node: ?( {
+              __typename: \\"Human\\",
+              // The name of the character
+              name: string,
+            } | {
+              __typename: \\"Droid\\",
+              // The name of the character
+              name: string,
+            }
+          ),
+        |} >,
+      |},
+      // A list of starships this person has piloted, or an empty list if none
+      starships: ? Array<? {|
+        __typename: \\"Starship\\",
+        // The name of the starship
+        name: string,
+      |} >,
+    } | {
+      __typename: \\"Droid\\",
+      // The friends of the character exposed as a connection with edges
+      friendsConnection: {|
+        __typename: \\"FriendsConnection\\",
+        // The edges for each of the character's friends.
+        edges: ? Array<? {|
+          __typename: \\"FriendsEdge\\",
+          // The character represented by this friendship edge
+          node: ?( {
+              __typename: \\"Human\\",
+              // The name of the character
+              name: string,
+            } | {
+              __typename: \\"Droid\\",
+              // The name of the character
+              name: string,
+            }
+          ),
+        |} >,
+      |},
+    }
+  ),
+|};"
+`;
+
 exports[`Flow code generation #generateSource() should have __typename value matching fragment type on generic type 1`] = `
 "/* @flow */
 /* eslint-disable */
diff --git a/test/flow/codeGeneration.js b/test/flow/codeGeneration.js
index 245d1b9c..e4c5d65a 100644
--- a/test/flow/codeGeneration.js
+++ b/test/flow/codeGeneration.js
@@ -370,5 +370,31 @@ describe('Flow code generation', function() {
       const source = generateSource(context);
       expect(source).toMatchSnapshot();
     });
+    
+    test('should have __typename value in nested property', () => {
+      const { compileFromSource } = setup(starWarsSchema);
+      const context = compileFromSource(`
+        query HeroName {
+          hero {
+            friendsConnection {
+              edges {
+                node {
+                  name
+                  
+                }
+              }
+            }
+            ... on Human {
+              starships {
+                name
+              }
+            }
+          }
+        }
+      `);
+
+      const source = generateSource(context);
+      expect(source).toMatchSnapshot();
+    });
   });
 });
diff --git a/test/typescript/__snapshots__/codeGeneration.js.snap b/test/typescript/__snapshots__/codeGeneration.js.snap
index 8ae8e8a7..935f3814 100644
--- a/test/typescript/__snapshots__/codeGeneration.js.snap
+++ b/test/typescript/__snapshots__/codeGeneration.js.snap
@@ -414,6 +414,65 @@ export interface CustomScalarQuery {
 "
 `;
 
+exports[`TypeScript code generation #generateSource() should have __typename value in nested property 1`] = `
+"/* tslint:disable */
+//  This file was automatically generated and should not be edited.
+
+export interface HeroNameQuery {
+  hero: ( {
+      __typename: \\"Human\\",
+      // The friends of the human exposed as a connection with edges
+      friendsConnection:  {
+        __typename: \\"FriendsConnection\\",
+        // The edges for each of the character's friends.
+        edges:  Array< {
+          __typename: \\"FriendsEdge\\",
+          // The character represented by this friendship edge
+          node: ( {
+              __typename: \\"Human\\",
+              // The name of the character
+              name: string,
+            } | {
+              __typename: \\"Droid\\",
+              // The name of the character
+              name: string,
+            }
+          ) | null,
+        } | null > | null,
+      },
+      // A list of starships this person has piloted, or an empty list if none
+      starships:  Array< {
+        __typename: \\"Starship\\",
+        // The name of the starship
+        name: string,
+      } | null > | null,
+    } | {
+      __typename: \\"Droid\\",
+      // The friends of the character exposed as a connection with edges
+      friendsConnection:  {
+        __typename: \\"FriendsConnection\\",
+        // The edges for each of the character's friends.
+        edges:  Array< {
+          __typename: \\"FriendsEdge\\",
+          // The character represented by this friendship edge
+          node: ( {
+              __typename: \\"Human\\",
+              // The name of the character
+              name: string,
+            } | {
+              __typename: \\"Droid\\",
+              // The name of the character
+              name: string,
+            }
+          ) | null,
+        } | null > | null,
+      },
+    }
+  ) | null,
+};
+"
+`;
+
 exports[`TypeScript code generation #generateSource() should have __typename value matching fragment type on generic type 1`] = `
 "/* tslint:disable */
 //  This file was automatically generated and should not be edited.
diff --git a/test/typescript/codeGeneration.js b/test/typescript/codeGeneration.js
index 47359650..3c5320b9 100644
--- a/test/typescript/codeGeneration.js
+++ b/test/typescript/codeGeneration.js
@@ -313,5 +313,31 @@ describe('TypeScript code generation', function() {
       const source = generateSource(context);
       expect(source).toMatchSnapshot();
     });
+    
+    test('should have __typename value in nested property', () => {
+      const { compileFromSource } = setup(starWarsSchema);
+      const context = compileFromSource(`
+        query HeroName {
+          hero {
+            friendsConnection {
+              edges {
+                node {
+                  name
+                  
+                }
+              }
+            }
+            ... on Human {
+              starships {
+                name
+              }
+            }
+          }
+        }
+      `);
+
+      const source = generateSource(context);
+      expect(source).toMatchSnapshot();
+    });
   });
 });