Repository: apollographql/apollo-codegen
Pull request #398 (2018-03-21 06:31:34)

Typo on command name
<!--**Pull Request Labels**

While not necessary, you can help organize our pull requests by labeling this issue when you open it.  To add a label automatically, simply [x] mark the appropriate box below:

- [ ] feature
- [ ] blocking
- [x] docs

To add a label not listed above, simply place `/label another-label-name` on a line by itself.
-->

<img width="638" alt="screen shot 2018-03-20 at 11 31 03 pm" src="https://user-images.githubusercontent.com/992008/37696925-fdadad18-2c96-11e8-8f45-31c5877ce31f.png">

### Also

> If you're using a client like apollo-client that **does** this automatically for your GraphQL operations

Is **does** the correct word here, as opposed to **doesn't**?

meteor-bot: @chibicode: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/
chibicode: Already signed!

Patch:
diff --git a/README.md b/README.md
index f1bb451d..f82f4d43 100644
--- a/README.md
+++ b/README.md
@@ -75,7 +75,7 @@ In case you specify multiple schemas in your `.graphqlconfig` file, choose which
 
 When using `apollo-codegen` with Typescript or Flow, make sure to add the `__typename` introspection field to every selection set within your graphql operations.
 
-If you're using a client like `apollo-client` that does this automatically for your GraphQL operations, pass in the `--addTypename` option to `apollo-codegen` to make sure the generated Typescript and Flow types have the `__typename` field as well. This is required to ensure proper type generation support for `GraphQLUnionType` and `GraphQLInterfaceType` fields.
+If you're using a client like `apollo-client` that does this automatically for your GraphQL operations, pass in the `--add-typename` option to `apollo-codegen` to make sure the generated Typescript and Flow types have the `__typename` field as well. This is required to ensure proper type generation support for `GraphQLUnionType` and `GraphQLInterfaceType` fields.
 
 ### Why is the __typename field required?
 
diff --git a/package-lock.json b/package-lock.json
index cd05cb25..0913b4d5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "apollo-codegen",
-  "version": "0.19.0",
+  "version": "0.19.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index 56a920d1..6a1afffc 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "apollo-codegen",
-  "version": "0.19.0",
+  "version": "0.19.1",
   "description": "Generate API code or type annotations based on a GraphQL schema and query documents",
   "main": "./lib/index.js",
   "bin": "./lib/cli.js",
diff --git a/src/compiler/index.ts b/src/compiler/index.ts
index 1ae98bd5..af190615 100644
--- a/src/compiler/index.ts
+++ b/src/compiler/index.ts
@@ -6,6 +6,7 @@ import {
   Kind,
   isCompositeType,
   GraphQLOutputType,
+  GraphQLInputType,
   GraphQLScalarType,
   GraphQLEnumType,
   GraphQLInputObjectType,
@@ -19,7 +20,6 @@ import {
   FragmentDefinitionNode,
   SelectionSetNode,
   SelectionNode,
-  ArgumentNode,
   isSpecifiedScalarType
 } from 'graphql';
 
@@ -49,15 +49,6 @@ export interface CompilerContext {
   options: CompilerOptions;
 }
 
-function argumentsFromAST(args: ArgumentNode[]): Argument[] {
-  return (
-    args &&
-    args.map(arg => {
-      return { name: arg.name.value, value: valueFromValueNode(arg.value) };
-    })
-  );
-}
-
 export interface Operation {
   operationId?: string;
   operationName: string;
@@ -88,6 +79,7 @@ export interface SelectionSet {
 export interface Argument {
   name: string;
   value: any;
+  type?: GraphQLInputType;
 }
 
 export type Selection = Field | TypeCondition | BooleanCondition | FragmentSpread;
@@ -164,14 +156,14 @@ export function compileToIR(
       fragmentSpread.selectionSet.possibleTypes.includes(type)
     );
 
-    fragmentSpread.isConditional = fragment.selectionSet.possibleTypes.some(type =>
-      !fragmentSpread.selectionSet.possibleTypes.includes(type)
+    fragmentSpread.isConditional = fragment.selectionSet.possibleTypes.some(
+      type => !fragmentSpread.selectionSet.possibleTypes.includes(type)
     );
 
     fragmentSpread.selectionSet = {
       possibleTypes,
       selections: fragment.selectionSet.selections
-    }
+    };
   }
 
   const typesUsed = compiler.typesUsed;
@@ -292,11 +284,6 @@ class Compiler {
         const name = selectionNode.name.value;
         const alias = selectionNode.alias ? selectionNode.alias.value : undefined;
 
-        const args =
-          selectionNode.arguments && selectionNode.arguments.length > 0
-            ? argumentsFromAST(selectionNode.arguments)
-            : undefined;
-
         const fieldDef = getFieldDef(this.schema, parentType, selectionNode);
         if (!fieldDef) {
           throw new GraphQLError(`Cannot query field "${name}" on type "${String(parentType)}"`, [
@@ -313,6 +300,19 @@ class Compiler {
 
         const responseKey = alias || name;
 
+        const args =
+          selectionNode.arguments && selectionNode.arguments.length > 0
+            ? selectionNode.arguments.map(arg => {
+                const name = arg.name.value;
+                const argDef = fieldDef.args.find(argDef => argDef.name === arg.name.value);
+                return {
+                  name,
+                  value: valueFromValueNode(arg.value),
+                  type: (argDef && argDef.type) || undefined
+                };
+              })
+            : undefined;
+
         let field: Field = {
           kind: 'Field',
           responseKey,
@@ -343,7 +343,7 @@ class Compiler {
       }
       case Kind.INLINE_FRAGMENT: {
         const typeNode = selectionNode.typeCondition;
-        const type = typeNode ? typeFromAST(this.schema, typeNode) as GraphQLCompositeType : parentType;
+        const type = typeNode ? (typeFromAST(this.schema, typeNode) as GraphQLCompositeType) : parentType;
         const possibleTypesForTypeCondition = this.possibleTypesForType(type).filter(type =>
           possibleTypes.includes(type)
         );
diff --git a/src/compiler/legacyIR.ts b/src/compiler/legacyIR.ts
index 166cd284..7cb6763e 100644
--- a/src/compiler/legacyIR.ts
+++ b/src/compiler/legacyIR.ts
@@ -1,4 +1,11 @@
-import { GraphQLSchema, GraphQLType, GraphQLObjectType, GraphQLCompositeType, DocumentNode } from 'graphql';
+import {
+  GraphQLSchema,
+  GraphQLType,
+  GraphQLObjectType,
+  GraphQLCompositeType,
+  GraphQLInputType,
+  DocumentNode
+} from 'graphql';
 
 import { compileToIR, CompilerContext, SelectionSet, Field, FragmentSpread } from './';
 
@@ -85,6 +92,7 @@ export interface BooleanCondition {
 export interface Argument {
   name: string;
   value: any;
+  type?: GraphQLInputType;
 }
 
 export function compileToLegacyIR(
@@ -156,7 +164,9 @@ class LegacyIRTransformer {
   transformSelectionSetToLegacyIR(selectionSet: SelectionSet) {
     const typeCase = typeCaseForSelectionSet(selectionSet, this.options.mergeInFieldsFromFragmentSpreads);
 
-    const fields: LegacyField[] = this.transformFieldsToLegacyIR(collectAndMergeFields(typeCase.default, false));
+    const fields: LegacyField[] = this.transformFieldsToLegacyIR(
+      collectAndMergeFields(typeCase.default, false)
+    );
 
     const inlineFragments: LegacyInlineFragment[] = typeCase.variants.flatMap(variant => {
       const fields = this.transformFieldsToLegacyIR(collectAndMergeFields(variant, false));
@@ -220,7 +230,7 @@ class LegacyIRTransformer {
         description,
         isDeprecated,
         deprecationReason,
-        ...selectionSet ? this.transformSelectionSetToLegacyIR(selectionSet) : {}
+        ...(selectionSet ? this.transformSelectionSetToLegacyIR(selectionSet) : {})
       } as LegacyField;
     });
   }
diff --git a/src/swift/codeGeneration.ts b/src/swift/codeGeneration.ts
index 69175083..634cb539 100644
--- a/src/swift/codeGeneration.ts
+++ b/src/swift/codeGeneration.ts
@@ -816,7 +816,7 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
 
     properties.forEach(property => {
       if (property.isOptional) {
-        property.typeName = `Optional<${property.typeName}>`;
+        property.typeName = `Swift.Optional<${property.typeName}>`;
       }
     });
 


Test patch:
diff --git a/test/__snapshots__/jsonOutput.ts.snap b/test/__snapshots__/jsonOutput.ts.snap
index 762482a1..46382794 100644
--- a/test/__snapshots__/jsonOutput.ts.snap
+++ b/test/__snapshots__/jsonOutput.ts.snap
@@ -30,14 +30,16 @@ exports[`JSON output should generate JSON output for a mutation with an enum and
 							\\"value\\": {
 								\\"kind\\": \\"Variable\\",
 								\\"variableName\\": \\"episode\\"
-							}
+							},
+							\\"type\\": \\"Episode\\"
 						},
 						{
 							\\"name\\": \\"review\\",
 							\\"value\\": {
 								\\"kind\\": \\"Variable\\",
 								\\"variableName\\": \\"review\\"
-							}
+							},
+							\\"type\\": \\"ReviewInput!\\"
 						}
 					],
 					\\"isConditional\\": false,
@@ -412,7 +414,8 @@ exports[`JSON output should generate JSON output for a query with an enum variab
 							\\"value\\": {
 								\\"kind\\": \\"Variable\\",
 								\\"variableName\\": \\"episode\\"
-							}
+							},
+							\\"type\\": \\"Episode\\"
 						}
 					],
 					\\"isConditional\\": false,
@@ -502,7 +505,8 @@ exports[`JSON output should generate JSON output for a subscription 1`] = `
 							\\"value\\": {
 								\\"kind\\": \\"Variable\\",
 								\\"variableName\\": \\"repoFullName\\"
-							}
+							},
+							\\"type\\": \\"String!\\"
 						}
 					],
 					\\"isConditional\\": false,
@@ -571,7 +575,8 @@ exports[`JSON output should generate JSON output for an input object type with d
 							\\"value\\": {
 								\\"kind\\": \\"Variable\\",
 								\\"variableName\\": \\"input\\"
-							}
+							},
+							\\"type\\": \\"ComplexInput!\\"
 						}
 					],
 					\\"isConditional\\": false,
diff --git a/test/compiler/legacyIR.js b/test/compiler/legacyIR.js
index 46894d49..ee91ebd3 100644
--- a/test/compiler/legacyIR.js
+++ b/test/compiler/legacyIR.js
@@ -159,7 +159,7 @@ describe('Compiling query documents to the legacy IR', () => {
     const { operations } = compileToLegacyIR(schema, document);
 
     expect(operations['HeroName'].fields[0].args)
-      .toEqual([{ name: "episode", value: "EMPIRE" }]);
+      .toEqual([{ name: "episode", value: "EMPIRE", type: schema.getType("Episode") }]);
   });
 
   it(`should include isConditional if a field has skip or include directives with variables`, () => {
diff --git a/test/swift/__snapshots__/codeGeneration.ts.snap b/test/swift/__snapshots__/codeGeneration.ts.snap
index 27a4cebd..cc7aa78c 100644
--- a/test/swift/__snapshots__/codeGeneration.ts.snap
+++ b/test/swift/__snapshots__/codeGeneration.ts.snap
@@ -1506,7 +1506,7 @@ exports[`Swift code generation #typeDeclarationForGraphQLType() should generate
 public struct ReviewInput: GraphQLMapConvertible {
   public var graphQLMap: GraphQLMap
 
-  public init(stars: Int, commentary: Optional<String?> = nil, favoriteColor: Optional<ColorInput?> = nil) {
+  public init(stars: Int, commentary: Swift.Optional<String?> = nil, favoriteColor: Swift.Optional<ColorInput?> = nil) {
     graphQLMap = [\\"stars\\": stars, \\"commentary\\": commentary, \\"favorite_color\\": favoriteColor]
   }
 
@@ -1521,9 +1521,9 @@ public struct ReviewInput: GraphQLMapConvertible {
   }
 
   /// Comment about the movie, optional
-  public var commentary: Optional<String?> {
+  public var commentary: Swift.Optional<String?> {
     get {
-      return graphQLMap[\\"commentary\\"] as! Optional<String?>
+      return graphQLMap[\\"commentary\\"] as! Swift.Optional<String?>
     }
     set {
       graphQLMap.updateValue(newValue, forKey: \\"commentary\\")
@@ -1531,9 +1531,9 @@ public struct ReviewInput: GraphQLMapConvertible {
   }
 
   /// Favorite color, optional
-  public var favoriteColor: Optional<ColorInput?> {
+  public var favoriteColor: Swift.Optional<ColorInput?> {
     get {
-      return graphQLMap[\\"favorite_color\\"] as! Optional<ColorInput?>
+      return graphQLMap[\\"favorite_color\\"] as! Swift.Optional<ColorInput?>
     }
     set {
       graphQLMap.updateValue(newValue, forKey: \\"favorite_color\\")