Skip to content

Commit 09f72b5

Browse files
authored
[FEI-6084] Fix handling of input objects with required attributes that have default values (#72)
## 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: #72
1 parent 7401909 commit 09f72b5

5 files changed

+14
-2
lines changed

.changeset/quick-lies-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/graphql-flow": patch
3+
---
4+
5+
Fix handling of input objects with required attributes that have a default value

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"bin": {
55
"graphql-flow": "./dist/cli/run.js"
66
},
7+
"jest": {
8+
"testPathIgnorePatterns": ["dist"]
9+
},
710
"scripts": {
811
"test": "jest",
912
"publish:ci": "yarn run build && changeset publish",

src/__test__/example-schema.graphql

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ input CharacterInput {
6060
friends: [ID!]
6161
appearsIn: [Episode!]
6262
candies: PositiveNumber!
63+
friendly: Boolean! = true
6364
}
6465

6566
type Mutation {

src/__test__/graphql-flow.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ describe("graphql-flow generation", () => {
621621
- JEDI*/
622622
"NEW_HOPE" | "EMPIRE" | "JEDI"> | null | undefined;
623623
candies: number;
624+
friendly?: boolean | null | undefined;
624625
};
625626
},
626627
response: {

src/generateVariablesType.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const inputObjectToFlow = (
3131
vbl.description,
3232
maybeOptionalObjectTypeProperty(
3333
vbl.name,
34-
inputRefToFlow(ctx, vbl.type),
34+
inputRefToFlow(ctx, vbl.type, vbl.defaultValue != null),
3535
),
3636
),
3737
),
@@ -58,9 +58,11 @@ export const maybeOptionalObjectTypeProperty = (
5858
export const inputRefToFlow = (
5959
ctx: Context,
6060
inputRef: IntrospectionInputTypeRef,
61+
hasDefaultValue = false,
6162
): babelTypes.TSType => {
6263
if (inputRef.kind === "NON_NULL") {
63-
return _inputRefToFlow(ctx, inputRef.ofType);
64+
const result = _inputRefToFlow(ctx, inputRef.ofType);
65+
return hasDefaultValue ? nullableType(result) : result;
6466
}
6567
const result = _inputRefToFlow(ctx, inputRef);
6668
return transferLeadingComments(result, nullableType(result));

0 commit comments

Comments
 (0)