relay icon indicating copy to clipboard operation
relay copied to clipboard

Enhancement of the customScalars format for typescript

Open pebeka opened this issue 4 years ago • 19 comments

As of today, the customScalars format is straightforward, just a string / string mapping, and then limit the use of custom scalars to native types. The customScalars format can be enhanced to support more complex types: A proposal can be:

const customScalars = {
  MyType: {
    type: 'MyType',
    module: '../myModule'
  },
  MySimpleString: 'String'  
}

this will add

import { MyType } from '../myModule';

in the generated file. Do you agree with that format? I can do a PR on the relay-tools/relay-compiler-language-typescript repo.

pebeka avatar Aug 20 '19 19:08 pebeka

(When I suggested opening this ticket, I meant it to be agnostic of programming language. The TS plugin simply follows what Relay does.)

alloy avatar Aug 20 '19 19:08 alloy

ok, the typing language agnostic view of the proposal: customScalars is an object with keys as custom type name, and their associated value is either a string or an object with a 'type' key and eventually other keys:

const customScalars = {
  MyType: {
    type: 'MyType',
    // other props specific to a typing language
    module: '../myModule',
  },
  MySimpleString: 'String',
  // or the equivalent declaration:   
  MySimpleString: {
    type: 'String',
  }   
}

pebeka avatar Aug 25 '19 22:08 pebeka

this would be really great. To add to @pebeka proposal, to make it truly agnostic, I think it's necessary to provide a map for each target language. something like:

customScalars: {
  MyType: {
    flow: X,
    typeScript: Y,
  }
}

how X and Y look is actually specified by the plugin itself and relay-compiler remains unopinionated (for example, one implementation could be @pebeka's proposal, another could be a simple string that is evaled, etc).

itajaja avatar Aug 27 '19 12:08 itajaja

It'd be sort of weird for types to have different names in Flow and TS, esp. if any of them are runtime types, no?

taion avatar Aug 27 '19 14:08 taion

not different names, different definitions, eg typescript's Record is different from whatever flow has

itajaja avatar Aug 27 '19 15:08 itajaja

a proposal for the proposal: allow a custom scalar to have a serialized & a parsed value.

For example, a custom DateTime will come back as a string in the response payload. However, if I use DateTime as an argument, I'd like it to be a Date. Apologies if this is already possible, just haven't figured out how to do it yet!

mattkrick avatar Nov 20 '19 15:11 mattkrick

I'm using io-ts right now to serialize and deserialize at runtime the iso-strings received from my DB to Moment objects.

export const DateTime = new t.Type<moment.Moment, string>(
  'DateTime',
  (mixed): mixed is moment.Moment => moment.isMoment(mixed),
  (mixed, context) =>
    pipe(
      t.string.validate(mixed, context),
      either.chain(str => {
        const instance = moment(str)
        return instance.isValid() ? t.success(instance) : t.failure(str, context)
      })
    ),
  instance => instance.toISOString()
)

For now, I just map the payloads and responses to fit my needs but having custom scalars would be so much more convenient.

I would really like to have the ability to automatically decode payloads and encode responses when they are flagged with a specific GraphQL scalar by providing the appropriate handler to Relay Compiler.

Eldow avatar Sep 23 '20 15:09 Eldow

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 13:12 stale[bot]

What's the reason this was marked as "wontfix"? CC @mofeiZ

punkpeye avatar Sep 19 '22 02:09 punkpeye

This issue appears to be a duplicate of https://github.com/facebook/relay/issues/91 (91! 🤯)

punkpeye avatar Sep 19 '22 02:09 punkpeye

I think this issue could be closed. More complex custom scalars are already supported. Configuration:

https://github.com/facebook/relay/blob/dec019d3290233e134b603124001814a77832271/scripts/config.tests.json#L23-L25

Implementation:

https://github.com/facebook/relay/blob/2768c3ea12668c5bf23d47d51515bd3939f9543f/packages/relay-runtime/mutations/tests/OpaqueScalarType.js#L15-L30

mrtnzlml avatar Sep 19 '22 23:09 mrtnzlml

@mrtnzlml Do you have any more information about it? I can't find any documentation about it at all :eyes:

In particular: how do I need to define the custom scalar type? What are the requirements? Is it possible to parse DateTimes to Date objects (one of the main use cases of this feature request).

LukasKalbertodt avatar Oct 10 '22 07:10 LukasKalbertodt

@LukasKalbertodt, I do not have more info. But maybe @captbaritone?

mrtnzlml avatar Oct 15 '22 16:10 mrtnzlml

We added config-file-define TS/Flow types for custom scalars recently but have not yet documented them. They get specified in the compiler config. For now checking the Rust structs for the config file is your best bet to understand how they are used.

They do now allow you custom transformation (serialize/deserialize) functions.

That said, I think @alunyov recently mentioned he had done some investigation into how/if that might be possible.

captbaritone avatar Oct 16 '22 03:10 captbaritone

@captbaritone @alunyov Is this supported in new compiler ?

damikun avatar Dec 28 '22 10:12 damikun

@captbaritone I think you've got a misleading typo there 😅

They do ~now~ not allow you custom transformation (serialize/deserialize) functions.

Is custom serialize/deserialize investigation @alunyov conducted public, and is it something you would accept contributions on?

beaumontjonathan avatar Feb 08 '23 19:02 beaumontjonathan

^ would love full custom scalar support / happy to contribute as well, I think ideally you would be able to just use GraphQL custom scalar types from graphql-js (example)

robertf224 avatar Mar 20 '23 03:03 robertf224

This would be tremendously useful for me as well. Currently doing a lot of manual deserializing and useMemo.

piotrblasiak avatar May 31 '23 05:05 piotrblasiak

I would also love to hear some feedback about this feature. It doesn't seem to be in latest Relay compiler.

molinch avatar Feb 09 '24 08:02 molinch