I have a custom scalar on my backend called DataUrl which is just a string which has been validated to be a data: url.
In https://github.com/facebook/relay/blob/master/packages/relay-compiler/bin/RelayCompilerBin.js#L177, customScalars is passed as an empty object, but could be configuration-based.
It is used by https://github.com/facebook/relay/blob/master/packages/relay-compiler/core/RelayFlowTypeTransformers.js#L74. There appears to be special support for the nonstandard Url scalar type already.
It appears that this is a duplicate of #1874, which is closed but still relevant.
I would like to note, though, that simple configuration of the serialized data type probably isn't sufficient. Given that custom scalars might be hydrated into any given type via a handler (see #91, which should also be reopened) we would ideally have the ability to hook into the flow types declared elsewhere in the app.
Perhaps the real solution requires customizing the compiler, or maybe building a compiler plugin (is this a thing?) as alluded to here:
https://github.com/facebook/relay/blob/51e8b54b2866576d2c28761febaace1854e3ad0d/packages/relay-compiler/graphql-compiler/core/GraphQLParser.js#L87-L92
In my opinion the cleanest solution would be to allow configuration of this field in relay-compiler: https://github.com/facebook/relay/blob/5b8040a4c63495a7313f88bf9124fbca8944f521/packages/relay-compiler/bin/RelayCompilerBin.js#L189
Since its not something that could easily be passed through command line args, would the relay maintainers accept a PR that added something like https://github.com/davidtheclark/cosmiconfig to relay-compiler so that users can have more control over the options?
My current (read: terrible) solution for this is to use https://www.npmjs.com/package/patch-package to patch the unconfigurable customScalars object.
Of course, doing this leaves a bad taste in my mouth.
customScalars (--custom-scalar <customname>=<graphqlname> and allow the flag to be repeated). @ckknight interested in sending a PR?For what it's worth, the Apollo codegen provides an option to names the flow type of custom scalars using the GraphQL name and an optional prefix. So for example, if I had a custom scalar called PolygonCoordinates and a prefix for these called GraphQL$ it would use GraphQL$PolygonCoordinates as the flow type.
This allows me to define a global GraphQL$PolygonCoordinates in my root libdef and be done with it. Using globals doesn't exactly give me the warm fuzzies, but it's a really effective, easy-to-implement solution that properly fixes the problem.
I like the idea of relay.config.js using https://github.com/davidtheclark/cosmiconfig, metro bundler uses it
https://github.com/facebook/metro/blob/master/packages/metro-config/src/loadConfig.js#L60
@josephsavona I'm working with 30+ custom scalars. Seems like that'd get a bit onerous rather quickly. It'd be a stopgap that I'd prefer over what I'm currently doing.
@ckknight Ah, yeah with that many custom scalars command line flags would get annoying quickly. Maybe not that then.
Thanks so much for your effort on this, @alloy
You got it 馃憤
Most helpful comment
Thanks so much for your effort on this, @alloy