简体   繁体   English

如何在TypeScript中使用Relay Modern(babel-plugin-relay和relay-compiler)?

[英]How to use Relay Modern (babel-plugin-relay and relay-compiler) with TypeScript?

When using TypeScript, babel-plugin-relay and relay-compiler are not parsing our graphql calls. 使用TypeScript时,babel-plugin-relay和relay-compiler不会解析我们的graphql调用。 Our TS-generated JS files look like this, targeting ES2015: 我们的TS生成的JS文件看起来像这样,针对ES2015:

var mutation = compat_1.graphql(_templateObject2);

And our output from relay-compiler looks like this, indicating that no files were parsed: 我们的relay-compiler输出看起来像这样,表明没有文件被解析:

Parsed default in 0.05s Writing default Writer time: 0.05s [0.05s compiling, 0.00s generating, 0.00s extra] Unchanged: 0 files Written default in 0.07s

How can we get relay-compiler and babel-plugin-relay to operate properly on these files? 我们怎样才能让relay-compiler和babel-plugin-relay在这些文件上正常运行?

Both the plugin and the compiler need bare graphql calls to work properly. 插件和编译器都需要裸图形调用才能正常工作。 If the graphql call takes an object (as the above takes _templateObject2 ) or is called on another object (as it's called on compat_1 above), neither tool will work. 如果graphql调用采用一个对象(如上所述采用_templateObject2 )或在另一个对象上调用(因为它在上面的compat_1上调用),则两个工具都不起作用。

To fix, we're just using an old-style require statement so that the TS transpiler outputs plain graphql calls: 为了解决这个问题,我们只使用旧式的require语句,以便TS转换器输出普通的graphql调用:

const { commitMutation, graphql } = require('react-relay/compat');

Works perfectly. 完美的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM