Skip to content

Commit

Permalink
[FEI-6084] Fix handling of input objects with required attributes tha…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
jaredly authored Jan 9, 2025
1 parent 7401909 commit 09f72b5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-lies-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/graphql-flow": patch
---

Fix handling of input objects with required attributes that have a default value
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"bin": {
"graphql-flow": "./dist/cli/run.js"
},
"jest": {
"testPathIgnorePatterns": ["dist"]
},
"scripts": {
"test": "jest",
"publish:ci": "yarn run build && changeset publish",
Expand Down
1 change: 1 addition & 0 deletions src/__test__/example-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ input CharacterInput {
friends: [ID!]
appearsIn: [Episode!]
candies: PositiveNumber!
friendly: Boolean! = true
}

type Mutation {
Expand Down
1 change: 1 addition & 0 deletions src/__test__/graphql-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ describe("graphql-flow generation", () => {
- JEDI*/
"NEW_HOPE" | "EMPIRE" | "JEDI"> | null | undefined;
candies: number;
friendly?: boolean | null | undefined;
};
},
response: {
Expand Down
6 changes: 4 additions & 2 deletions src/generateVariablesType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const inputObjectToFlow = (
vbl.description,
maybeOptionalObjectTypeProperty(
vbl.name,
inputRefToFlow(ctx, vbl.type),
inputRefToFlow(ctx, vbl.type, vbl.defaultValue != null),
),
),
),
Expand All @@ -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));
Expand Down

0 comments on commit 09f72b5

Please sign in to comment.