From 09f72b5fc511c6e2c29994bae46435600e046aa3 Mon Sep 17 00:00:00 2001 From: Jared Forsyth Date: Thu, 9 Jan 2025 12:36:51 -0600 Subject: [PATCH] [FEI-6084] Fix handling of input objects with required attributes that have default values (#72) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: Previously they were mistakenly reported as non-nullable. Issue: FEI-6084 ## Test plan: See updated snapshot; `friendly` is required with a default value, and the generated type has it as nullable. Author: jaredly Reviewers: lillialexis, kevinb-khan Required Reviewers: Approved By: lillialexis, kevinb-khan Checks: ✅ Lint & Test (ubuntu-latest, 20.x) Pull Request URL: https://github.com/Khan/graphql-flow/pull/72 --- .changeset/quick-lies-bathe.md | 5 +++++ package.json | 3 +++ src/__test__/example-schema.graphql | 1 + src/__test__/graphql-flow.test.ts | 1 + src/generateVariablesType.ts | 6 ++++-- 5 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .changeset/quick-lies-bathe.md 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));