facebook / relay

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

relay-compiler 13 custom transforms #3899

Open mle-moni opened 2 years ago

mle-moni commented 2 years ago

It would be nice to be able to add custom transforms like it was possible in v12

in our project we used it to add some code in generated files

// our code is something like
import { relayCompiler } from 'relay-compiler';

const tsPluginDefault = require('relay-compiler-language-typescript').default();
const { inputExtensions, outputExtension, findGraphQLTags, typeGenerator } = tsPluginDefault;

const langPlugin = {
    inputExtensions,
    outputExtension,
    findGraphQLTags,
    // in the formatModule we transform the ouput files to add some code specific to the request
    formatModule: ourSpecificFormatModule,
    typeGenerator,
}

await relayCompiler({
    ...moreConfig,
    language: langPlugin,
});
voideanvalue commented 2 years ago

I agree that it would be nice. The new compiler is implemented in Rust. Some searching led me to https://nullderef.com/blog/plugin-tech/. I'm not sure what the best approach for creating a plugin system for the new compiler might be. Optimizing compiler perf for large scale projects was one of the reasons for the Rust rewrite. We'd also need to think about performance implications and the sort of constrains we'd want for the plugin system.

As far as I'm aware, the relay team has no plans to investigate this space.

What sort of additional information are you looking to add? Maybe there's a simpler way to enable that without a proper plugin system for custom transforms.

mle-moni commented 2 years ago

We use graphql on the backend and we generate queries with relay-compiler, but for some queries we add some functions, mainly CRUD utils: e.g. in this query :

query getMissionQuery(
  $id: MissionId!
) @generateCRUD {
  EU {
    findOne {
      Mission(id: $id) {
        SOME_FIELD
        SOME_OTHER_FIELD
      }
    }
  }
}

we would generate the functions: createMission, readMission, updateMission and deleteMission