Skip to content

Conversation

@gilgardosh
Copy link
Collaborator

@gilgardosh gilgardosh commented Nov 18, 2020

Related: #5099

@changeset-bot
Copy link

changeset-bot bot commented Nov 18, 2020

🦋 Changeset detected

Latest commit: 701d609

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@graphql-codegen/typescript-operations Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

@Cellule Cellule left a comment

Choose a reason for hiding this comment

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

Reviewed 10 of 10 files at r1, 42 of 42 files at r2.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @gilgardosh)


packages/plugins/typescript/operations/src/ts-selection-set-processor.ts, line 39 at r2 (raw file):

    if (hasConditionals) {
      if (

nit:

const avoidOptional = (
  // TODO: check type and exec only if relevant
  this.config.avoidOptionals === true ||
  this.config.avoidOptionals.field ||
  this.config.avoidOptionals.inputValue ||
  this.config.avoidOptionals.object
)
const transform = avoidOptional ? "MakeMaybe" : "MakeOptional"
resString = `${
  this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : ''
}${transform}<${resString}, ${conditilnalsList.map(field => `'${field}'`).join(' | ')}>`;

packages/plugins/typescript/operations/tests/ts-documents.spec.ts, line 4770 at r2 (raw file):

    });

    it('On avoidOptionals:true, fileds with @skip, @include should make container resolve into MakeMaybe type', async () => {

nit: fields

@dotansimha
Copy link
Owner

@gilgardosh ready for a review? (btw, changeset is missing)

@gilgardosh
Copy link
Collaborator Author

@dotansimha Ready now

export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> &
{ [SubKey in keyof Pick<T, K>]?: Maybe<Pick<T, K>[SubKey]> };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
Copy link
Owner

@dotansimha dotansimha Nov 23, 2020

Choose a reason for hiding this comment

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

I think we can experiment with TS generics inference here.
Something like :

export type MakeMaybe<T, K extends keyof T, AndOptional extends boolean> =
    AndOptional extends true ?
        Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> } : 
        Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> ;

then, we can generate MakeMaybe<A, B, true> for example.

What do you think?

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.

3 participants