Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove filter never from select and add deep filter never to fix hand… #180

Merged
merged 3 commits into from
Apr 28, 2023

Conversation

jasong689
Copy link
Contributor

@jasong689 jasong689 commented Apr 26, 2023

Added a test to verify that you can still select the nested circular props. Also to note is that with DeepFilter you can no longer pass in a schema that has an optional property. This is fine with Gadget schemas as these are typed as prop: type | null instead of prop?: type

We will probably want to bump a minor version for this so that this change doesn't get picked up by older client packages

cc @angelini

PR Checklist

  • Important or complicated code is tested
  • Any user facing changes are documented in the Gadget-side changelog
  • Any immediate changes are slated for release in Gadget via a generated package dependency bump
  • Versions within this monorepo are matching and there's a valid upgrade path

@angelini
Copy link
Contributor

Just a few notes re deployment:

  • will need to increase the version of the package
  • the ShipIt tool to update the version in everyone's environment currently requires too much disk space, we need to update the job to cleanup after itself.

@jasong689
Copy link
Contributor Author

@angelini we won't need to rebuild everyone's client for this. just new ones being generated going forward

@@ -52,10 +59,10 @@ export type Select<Schema, Selection extends FieldSelection | null | undefined>
? Select<T, Selection>[]
: Schema extends null
? Select<Exclude<Schema, null>, Selection> | null
: FilterNever<{
Copy link
Contributor

Choose a reason for hiding this comment

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

If we remove this, there is a window of time where the generated API client for an app (potentially) uses this version of api-client-core but doesn't DeepFilterNever the return.

I think maybe we take a different approach: what if we give the current Select a new name, say InnerSelect, and instead do this:

export type Select<Schema, Selection extends FieldSelection | null | undefined> = DeepFilterNever<InnerSelect<Schema, Selection>>

which doesn't require a client rebuild and would only need a minor version bump (since it's backwards compatible).

Also, since the Select above has no constraints on the Schema type, we either need to consider adding the extends Record<string, unknown> constraint or making DeepFilterNever not need it, e.g.

export type DeepFilterNever<T> = T extends Record<string, unknown>
  ? FilterNever<{
      [Key in keyof T]:
         T[Key] extends Record<string, unknown>
         ? DeepFilterNever<T[Key]>
         : T[Key];
    }>
  : T
}>;

I think putting the constraint on Schema should be fine since a schema is never a literal / array / non-object thingy.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

And note, by "potentially uses", I think it would require the users to manually specify an api-client-core that would result in a yarn install grabbing the new version. Unlikely, but it could happen. If there's any concern there, we can double check existing package.jsons to see if this would happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thegedge that makes sense. One other thing that @airhorns brought up was that we don't yarn install on every rebuild so even though we've updated their api-client-core version the old version is the one that's imported until someone runs yarn install. So I think in addition to your suggested change, I'm going to re-implement Select in the same way in our codegen and remove it once all packages have been updated.

@jasong689 jasong689 force-pushed the filter-never branch 2 times, most recently from d234b9c to 5efd845 Compare April 26, 2023 18:53
Copy link
Contributor

@thegedge thegedge left a comment

Choose a reason for hiding this comment

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

Super happy this worked out! 🚀

};

/**
* Filter out any keys in `T` that are mapped to `never` recursively.
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth expanding a bit by calling out that objects, after having their never-valued keys filtered out, also get filtered out of their parent if they are empty. i.e.,

type A = DeepFilterNever<{ a: { b: never }, c: "test" }>;
type B = { c: "test" };  // same as A
type C = { a: {}, c: "test" };  // not this one though

Suggesting that because an empty object is distinct from never. Worth a double check that it's doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added an example in the comment and a test to make sure it's the case.

@jasong689
Copy link
Contributor Author

Failing tests look be a real issue. Will address

@jasong689 jasong689 force-pushed the filter-never branch 3 times, most recently from e7a2579 to 66b8841 Compare April 28, 2023 20:17
@jasong689 jasong689 merged commit d28e8ec into main Apr 28, 2023
@jasong689 jasong689 deleted the filter-never branch April 28, 2023 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants