-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
skip/include directives do not avoidOptionals
if field type is a list
#6748
Comments
Hi @brainkim , |
I tried upgrading typescript-operations to 2.1.5, but am now running into new errors related to the EDIT: Opened the following issue: #6757 Thanks for the quick turnaround and hello from ApolloGraphQL💜 |
Rethinking this case.
@brainkim what do you think? |
I agree with @gilgardosh and @n1ru4l in this case. |
Just to add to that another way of representing this (without optionals could be the following): export type MyQueryQuery = { __typename: "Query" } & {
me: { __typename: "User" } & (
| {}
| { messages: Array<{ __typename: "Message" } & { content: string }> });
};
const foo = (d: MyQueryQuery) => {
// @ts-expect-error
d.me.messages;
if ("messages" in d.me) {
// do sth with it here
d.me.messages;
}
}; This would be beneficial in a case where the boolean affects multiple fields🤔 query MyQuery($include: Boolean!) {
message {
id @include(if: $include)
content @include(if: $include)
}
} export type MessageQuery = { __typename: "Query" } & {
message:
| ({ __typename: "Message" } & ({} | { id: string }) &
({} | { content: string }))
| null;
};
const foo1 = (d: MessageQuery) => {
if (!d.message) return;
// @ts-expect-error
d.message.id;
// @ts-expect-error
d.message.content;
if ("id" in d.message) {
// do sth with it here
d.message.id;
}
if ("content" in d.message) {
// do sth with it here
d.message.content;
}
}; Or with an inline fragment: query MyQuery($include: Boolean!) {
message {
... on Message @include(if: $include) {
id
content
}
}
} export type MessageQuery = { __typename: "Query" } & {
message:
| ({ __typename: "Message" } & ({} | { id: string; content: string }))
| null;
};
const foo1 = (d: MessageQuery) => {
if (!d.message) return;
// @ts-expect-error
d.message.id;
// @ts-expect-error
d.message.content;
if ("id" in d.message) {
// do sth with it here
d.message.id;
d.message.content;
}
}; This has the following benefits: In the case of an optional fragment, only one But the following drawback: You cannot simply do In order to have the best out of both it would probably be best to settle on the following: For single fields: Use For fragments: Use |
Ah yeah. Some disorganized thoughts.
Thanks for the responses! |
Regarding 1: We are already considering dropping the Regarding 3: This is something we could make configurable. Regarding 4: skip/include and defer on fragments are IMHO best modeled with the |
Just joining this super interesting discussion. I think the path of TS optional is clear - it makes things more complex and could be solved with a simple explicit
With When we started working on it, we tried to align the @brainkim what do you think? |
I am agreeing with the proposal to ignore |
My colleague @martinbonnin mentioned a fun little example:
Another wrinkle to think about. |
@brainkim Forthat example, the generated code should be: export type MyQueryQuery = { __typename: "Query" } & {
me: { __typename: "User" } & {
messages?: Array<
{ __typename: "Message" } & Pick<Message, "content">
> | null;
};
}; |
@brainkim Can you please confirm that with the latest version the types are generated as we expected them to be? |
@n1ru4l I took a brief look at the PR referenced in the CHANGELOG.md. This test seems to indicate that we’re respecting For posterity, can you provide a brief description of what y‘all decided on with regard to Thank you so much for looking into this! |
@brainkim Thanks for being thorough! there actually is an issue with inline fragments mixed with conditional directive such as in this case. The directive is not influencing
since The Here is a fix, which you mentioned is not your cup of tea, but is a true representation of this case. TL;DR: |
Describe the bug
The
@skip
and@include
directives seem to be generating optional types forGraphQLList
fields, even whenavoidOptionals
is true.To Reproduce
Steps to reproduce the behavior:
https://codesandbox.io/s/practical-swanson-b93wg
codegen.yml
config file:Expected behavior
I expect TypeScript optionals not to be included in the generated output.
Actual behavior
messages
is optional.Environment:
Additional context
Probably related to this PR:
#5104
The text was updated successfully, but these errors were encountered: