facebook / relay

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

Compiler: warn about missing/invalid directives #4776

Open MarceloPrado opened 2 months ago

MarceloPrado commented 2 months ago

When writing an inline fragment to be used by readInlineData, Relay won't warn you if you forget to use the @inline directive:


export function getFooBarValue(fragmentRef: DummyExampleFragment$key) {
  const data = readInlineData(
    graphql`
      fragment DummyExampleFragment on Node {
        ... on Foo {
          bar
        }
      }
    `,
    fragmentRef,
  );

  return data?.foo?.bar ?? "";
}

From the compiler standpoint, everything works great as long as you spread DummyExampleFragment on Node. However, at runtime, Relay will throw an error:

Invariant Violation: GraphQLTag: Expected an inline data fragment, got `{"argumentDefinitions": ... }`.

The fix is simple - we just need to add @inline to our fragment definition. It would be great if the compiler explained that to me at build-time.

Overall, I believe we need "smarter" fragment types. Typescript should complain if: