graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Aliased fields with `@include`/`@skip` are treated as mandatory

Open RadovanBednar opened this issue 2 years ago • 5 comments

Which packages are impacted by your issue?

@graphql-codegen/typescript

Describe the bug

Since https://github.com/dotansimha/graphql-code-generator/issues/3836 fields adorned with @include or @skip directives are correctly treated as optional:

query getUsers($includeName: Boolean!) {
  users {
    id
    name @include(if: $includeName)
  }
}

becomes

export type GetUsersQuery = (
  { users: Array<(
    { id: string, name?: string }
    & { __typename?: 'User' }
  )> }
  & { __typename?: 'Query' }
);

However, when the conditional field is aliased, it's treated as mandatory:

query getUsers($includeName: Boolean!) {
  users {
    id
    userName: name @include(if: $includeName)
  }
}

becomes

export type GetUsersQuery = (
  { users: Array<(
    { id: string, userName: string } // no question mark in sight
    & { __typename?: 'User' }
  )> }
  & { __typename?: 'Query' }
);

Your Example Website or App

Steps to Reproduce the Bug or Issue

Alias a conditional field (i.e. one with @include or @skip directive). It's no longer treated as optional in the generated type.

Expected behavior

Fields adorned with @include or @skip should be treated as optional in the generated type, even if they are aliased.

Screenshots or Videos

No response

Platform

-

Codegen Config File

No response

Additional context

No response

RadovanBednar avatar Nov 01 '23 10:11 RadovanBednar

Thank you for reporting @RadovanBednar ! Seems like a real issue

I'm labeling it according to our new Contribution Guide and issue flow as a stage 0 issue

Now in order to advance to stage 1 we'll need a simple reproduction, maybe in code-sandbox?

Later to progress to stage 2, a failing test would be needed - you can also start directly from that Thanks for the report!

Urigo avatar Nov 19 '23 14:11 Urigo

Hello !

I found that this issue is still open, and since I faced it in my codebase too I managed to reproduce it in this sandbox 😄 As you can see in the document.graphql file, I added a @skip directive on both email and eml (which is email, but aliased). We can see that in the types.ts file we have email that is optional (as intended), but eml is shown as mandatory.

Is that helpful enough or do you need more infos @Urigo ?

Thanks, and have a good day!

FlorianVenturini avatar Jan 29 '24 13:01 FlorianVenturini

I have a similar issue but with fragments instead:

...Fragment @include(if: $condition) {
    field
}

Does not make field optional.

However, I just found a workaround after looking at relevant tests in the codebase:

...@include(if: $condition) {
    ...Fragment {
        field
    }
}

Works fine and seems to have the expected behavior otherwise.

Unfortunately, it doesn't work for aliased fields:

...@include(if: $condition) {
    alias: field
}

yasamoka avatar Feb 04 '24 21:02 yasamoka

I can confirm the issue with fragments with "@graphql-codegen/typescript-operations": "^4.2.1". @yasamoka thanks for the workaround! Unfortunately, it didn't work in my case. The type is still the same.

anton-amelekhin-veeva avatar Jun 17 '24 19:06 anton-amelekhin-veeva

I can confirm the issue with fragments with "@graphql-codegen/typescript-operations": "^4.2.1". @yasamoka thanks for the workaround! Unfortunately, it didn't work in my case. The type is still the same.

Can you provide a minimal example so I can take a look?

yasamoka avatar Jun 17 '24 19:06 yasamoka