facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.38k stars 1.82k forks source link

Why can a fragment tagged with `@alias` be nullable? #4720

Open waynezhang1995 opened 3 months ago

waynezhang1995 commented 3 months ago

Creating issues for Relay

Questions regarding how to use Relay and/or GraphQL

We are experimenting with the new @alias directive. Given the following example:

  graphql`
          query aliasDirectiveConditionalFragmentTestQuery($skip: Boolean!) {
            viewer {
              ...aliasDirectiveTestFragment @skip(if: $skip) @alias
            }
          }
        `,

the type generated would be

 readonly viewer: {
    readonly aliasDirectiveTestFragment?: {
      readonly " $fragmentSpreads": FragmentRefs<"aliasDirectiveTestFragment">;
    } | null;
  };

while being undefined make sense but wondering why aliasDirectiveTestFragment can be null? what would be the possible scenarios where it will be null? Thank you

Reporting issues with Relay

We will be using GitHub Issues for our public bugs. We will keep a close eye on this and try to make it clear when we have an internal fix in progress. Before filing a new issue, make sure an issue for your problem doesn't already exist.

The best way to get your bug fixed is to provide a reduced test case. To make reproduction simple for you, and set-up simple for Relay's maintainers, you can use Glitch: https://glitch.com/edit/#!/remix/relay-starter-kit

You can also provide a public repository with a runnable example.

Security bugs

Facebook has a bounty program for the safe disclosure of security bugs. With that in mind, please do not file public issues; go through the process outlined on that page.

captbaritone commented 3 months ago

I think today, in this case, I believe it will only ever return undefined. The null options is a side effect of how it's implemented. Revisiting the spec, to align with how field works, I agree that we should not return null here and update the types to specifically be just optional and not nullable.

If someone wants to explore a PR to improve this, search for FragmentAliasMetadata in https://github.com/facebook/relay/blob/71047f7398cacf9092e5f1c918635b14c7982092/compiler/crates/relay-typegen/src/visit.rs

For now I'd recommend using if(viewer.aliasDirectiveTestFragment == null) {.