diff --git a/.changeset/quick-lies-bathe.md b/.changeset/quick-lies-bathe.md new file mode 100644 index 0000000..b14692a --- /dev/null +++ b/.changeset/quick-lies-bathe.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/graphql-flow": patch +--- + +Fix handling of input objects with required attributes that have a default value diff --git a/package.json b/package.json index 5737061..c4789e9 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "bin": { "graphql-flow": "./dist/cli/run.js" }, + "jest": { + "testPathIgnorePatterns": ["dist"] + }, "scripts": { "test": "jest", "publish:ci": "yarn run build && changeset publish", diff --git a/src/__test__/example-schema.graphql b/src/__test__/example-schema.graphql index e25fac1..91b53cb 100644 --- a/src/__test__/example-schema.graphql +++ b/src/__test__/example-schema.graphql @@ -60,6 +60,7 @@ input CharacterInput { friends: [ID!] appearsIn: [Episode!] candies: PositiveNumber! + friendly: Boolean! = true } type Mutation { diff --git a/src/__test__/graphql-flow.test.ts b/src/__test__/graphql-flow.test.ts index c6f4838..97d6517 100644 --- a/src/__test__/graphql-flow.test.ts +++ b/src/__test__/graphql-flow.test.ts @@ -621,6 +621,7 @@ describe("graphql-flow generation", () => { - JEDI*/ "NEW_HOPE" | "EMPIRE" | "JEDI"> | null | undefined; candies: number; + friendly?: boolean | null | undefined; }; }, response: { diff --git a/src/generateVariablesType.ts b/src/generateVariablesType.ts index 4e61c1e..5d23508 100644 --- a/src/generateVariablesType.ts +++ b/src/generateVariablesType.ts @@ -31,7 +31,7 @@ export const inputObjectToFlow = ( vbl.description, maybeOptionalObjectTypeProperty( vbl.name, - inputRefToFlow(ctx, vbl.type), + inputRefToFlow(ctx, vbl.type, vbl.defaultValue != null), ), ), ), @@ -58,9 +58,11 @@ export const maybeOptionalObjectTypeProperty = ( export const inputRefToFlow = ( ctx: Context, inputRef: IntrospectionInputTypeRef, + hasDefaultValue = false, ): babelTypes.TSType => { if (inputRef.kind === "NON_NULL") { - return _inputRefToFlow(ctx, inputRef.ofType); + const result = _inputRefToFlow(ctx, inputRef.ofType); + return hasDefaultValue ? nullableType(result) : result; } const result = _inputRefToFlow(ctx, inputRef); return transferLeadingComments(result, nullableType(result));