Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Add '| undefined' to optional input properties (option 2) #492

Conversation

merrywhether
Copy link
Contributor

@merrywhether merrywhether commented Oct 21, 2021

Address #447 with changes focused in TypeScriptTypeTransformers.ts (see #491 for alternate implementation, depending on maintainers' preference of approach). This adds | undefined only to input types, as those were the only sites our Relay/TS codebase complained about the generated types.

Changing this implementation to add | undefined to all optional types would be probably hybrid with #491 and would yield changes like this:

      // FriendFragment.graphql
      import { FragmentRefs } from "relay-runtime";
      export type FriendFragment = {
    -     readonly name?: string | null;
    -     readonly lastName?: string | null;
    +     readonly name?: string | null | undefined;
    +     readonly lastName?: string | null | undefined;
          readonly feedback?: {
              readonly id: string;
              readonly name: string | null;
    -     } | null;
    +     } | null | undefined;
          readonly " $refType": "FriendFragment";
      };
      export type FriendFragment$data = FriendFragment;
      export type FriendFragment$key = {
    -     readonly " $data"?: FriendFragment$data;
    +     readonly " $data"?: FriendFragment$data | undefined;
          readonly " $fragmentRefs": FragmentRefs<"FriendFragment">;
      };

I think the only time this would come up would be when trying to create test mocks? So might be worth doing as well and happy to augment, but wanted to start with the minimal changeset and work from there, especially as this version requires the addition of only a single extra conditional branch.

Also:

  • turns on exactOptionalPropertyTypes in this repo
  • narrows type of State.generatedInputObjectTypes (found while working in generateInputObjectTypes in Add '| undefined' to optional input properties (option 1) #491)
  • removes 2 duplicate fixtures that were named mutaion instead of mutation
  • replaces ts.factory.createLiteralTypeNode(ts.factory.createToken(ts.SyntaxKind.NullKeyword)) with ts.factory.createNull()

@merrywhether merrywhether changed the title Add '| undefined' to optional input properties Add '| undefined' to optional input properties (option 2) Oct 21, 2021
@@ -115,20 +113,25 @@ function transformGraphQLEnumType(
export function transformInputType(
schema: Schema,
type: TypeID,
state: State
state: State,
isListItemType = false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be something like isPropertySignature and then require an update at the two other call sites while reverting the one modified below.

@merrywhether
Copy link
Contributor Author

Replaced by #494

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant