relay icon indicating copy to clipboard operation
relay copied to clipboard

Add relay-config library

Open robertf224 opened this issue 1 year ago • 5 comments

Adding (or rather reviving) a relay-config library for loading/saving Relay configs. Will port babel-plugin-relay to use this but hitting "module not found" errors in tests.

robertf224 avatar Sep 03 '24 03:09 robertf224

Would love a more complete understanding of what problem(s) we're trying to solve here.

Here's what I'm inferring: The babel plugin currently needs to be able to see the Relay config file in order to apply the correct transformation. This requires:

  1. Locating the same config file that the compiler finds
  2. Parsing the config file in the same way the compiler does
  3. Validate the config file in the same way the compiler does
  4. Being able to have type-safe interactions with reading the config file once it's been validated

Am I correct in inferring that this is the set of problems we are trying to solve here?

captbaritone avatar Sep 23 '24 20:09 captbaritone

@captbaritone yeah that's about right. For context I'm working on a nice integration layer right now between Relay and Next.js, but the same idea would apply to any tools building on top of Relay. The two places I'm using loadConfig are:

  • To automatically populate the Relay component of the Next.js config (which sets up the SWC transform)
  • I'm providing a pull-schema cli command that loads environment variables using @next/env, executes an introspection query using your Relay environment, and saves the schema to the path specified in the schema field of your Relay config

Providing a type-safe/standard library for loading Relay's config file will avoid bugs/hacks in tools building on top of Relay, e.g. see this cli that just hardcodes relay.config.json and then does a bunch of non-type-safe loading and manipulation on top

I will also port the Babel plugin to use this library in a follow-up!

robertf224 avatar Sep 23 '24 20:09 robertf224

btw @captbaritone this is where I have this logic duplicated in the integration library I wrote, would love to move upstream! https://github.com/robertf224/next-relay/tree/main/packages/next-relay/src/config/relay-config

robertf224 avatar Jan 31 '25 18:01 robertf224

Thanks for your work on this! It makes sense to me. A few thoughts:

  1. What do you think about moving this directly into the relay-compiler NPM package? The compiler is really the source of truth for the shape of the config and where it looks for the config, so the two packages will will always need to stay in sync anyway.
  2. Right now the babel transform also implements this search. If we're going to provide this logic in JS can we refactor the babel transform to import/use the new externally provided logic?

captbaritone avatar Feb 14 '25 17:02 captbaritone

how would you want to structure the exports from relay-compiler? looks like right now we basically just export the path to the binary as a string (https://github.com/facebook/relay/blob/main/packages/relay-compiler/index.js) so maybe would want to export a subpath or something to avoid a breaking change

robertf224 avatar Feb 20 '25 01:02 robertf224