Aliased fields with `@include`/`@skip` are treated as mandatory
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
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!
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!
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
}
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.
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?