zth / rescript-relay

Use Relay with ReScript.
https://rescript-relay-documentation.vercel.app/docs/getting-started
337 stars 50 forks source link

Blocked rescript@11 migration #467

Closed DZakh closed 10 months ago

DZakh commented 11 months ago

I've tried migrating the project at Carla to rescript@11 several times. I've fixed all the issues besides the problem with rescript-relay:

For context, we want to use uncurried: false since the codebase is quite big, and we want to gradually migrate to uncurried mode. Because of this, the rescript-relay V3 is not an option for us.

So we need to use rescript-relay V2 until we completely migrate to uncurried mode. But there's another problem. It uses _ prefixes for gql fields having restricted names. In rescript@10 the _s were removed in runtime, but after my PR πŸ˜… I removed the fields mangling logic from rescript@11. This way, rescript-relay V2 stopped handling private keywords in rescript@11 properly, the fields in compiled js are now prefixed with _.

Later, I've added the PR to allow using @as with @obj so there's a migration path from fields mangling logic.

So, now I'd like to update rescript-relay V2 to use @as instead of _ for private keywords in the generated code. I've already taken a look in the source code and noticed a submodule written in Rust. If you tell me how to contribute to it, I'd like to give it a try. I really wish to start using rescript@11 in production.

zth commented 11 months ago

Sure! Check this repo out: https://github.com/zth/relay/tree/rescript-relay. It's the Relay compiler fork RescriptRelay uses. It's in the compiler/ dir.

Place yourself in the compiler/test-project-res dir and run cargo build && ../target/debug/relay. That'll compile the compiler and run it on the test project. You'll see any changes you make reflected there.

Adding @as() is hopefully fairly straight forward. There should be plenty of examples in the code base.

mununki commented 10 months ago

Currently my project using "rescript": "v11-rc.4" and "rescript-relay": "^2.0.0" with uncurried: true. I have input type in graphQL schema:

input ShippingFeePolicyMutationInput {
  amount: Int!
  freeShippingThreshold: Int
  quantityStep: Int
  type: ShippingFeePolicyType!
}

The js output has _type instead of type now with this ReScript code.

Mutation.make_shippingFeePolicyMutationInput(~amount=0, ~_type=#FREE, ())
{
  amount: 0,
  _type: "FREE"
}

But, I can't figure it out why because playground looks fine to me. https://rescript-lang.org/try?version=v11.0.0-beta.4&code=AIGwlgbgpgUALgTwA5QARgHZIK5wPoDKAFmEkpgOYBiUUACgPbgDGCAsrgIZxgMYCSWXKgC8qAN4xUqTgFsG2DHABc6JQBopqAGYAnWsVLkMFACpF9AZyJMAJqoZIefADyY4APk3SAjtk5KYIgEcFBIDk68GG5KXlrAnJYAFABEiCgpAJSo6VB4qgDaAMQAwgDyAHIAIvym-JUAggAyeFQASgCiHagAPqhF7V29-RVlpngNdHRN-CUNAEJN3X1FdA38VcNFAIoAqg0VdaYAmnjzDQQdVQC6mgC+MDCgkGjADABGAFaoUAAeoboMJwQKhZJwANZ5axGSg0ehMMCsDhwbhRQQ4FSoJJaaQAPzkCiUqnc3mkqFxegMJDIlHMVhsIHsajgIgA-KS8X4AjxgqFwsy2RzyXhcoUcWSVuVqrV6hVmq1Oh1xdIVoMlWSJSMxhMpjM5ot1RqtmsNsqtnsDkdTudLlVxbdxYoglpsiIPGoMYRqcZqLRGCx2FxnAIhHBRKgUilHiAoGHZOGwZC8NCaSY4f7EYGUcH0bgkvj5IoWQAGdTC3IiAaKstJTKZIA

mununki commented 10 months ago

The functions and type definition are extracted from the generated modules by rescript-relay.

DZakh commented 10 months ago

It's the same issue I've described in the description. That's because of the PR: https://github.com/rescript-lang/rescript-compiler/pull/6354 Fix in the PR: https://github.com/zth/relay/pull/15

mununki commented 10 months ago

@DZakh Can you check the playground link I've added? I'm wondering why this output has type.

image
DZakh commented 10 months ago

See https://github.com/rescript-lang/rescript-compiler/pull/6354. There used to be the logic of mangling object keys up till 11-rc.3

DZakh commented 10 months ago

I think the change should be highlighted more in ReScript V11 release notes. It's a breaking change people should know about.

mununki commented 10 months ago

Ah! I confused the setting of playground it was v11-beta.4 not rc.3 πŸ˜“

mununki commented 10 months ago

I opened an issue https://github.com/rescript-lang/rescript-compiler/issues/6440, if the js output is generated by the return type of the external function, then _type issue here would be fixed, I guess.

DZakh commented 10 months ago

Yes, but I think it'll be quite difficult to make it work. Also, if you noticed, in our case the type has a field called type_, while external has a field called _type.

zth commented 10 months ago

2.1.0 is out, and should fix this. Reopen if it does not.