在Relay.createContainer中使用graphql`

时间:2018-05-03 07:08:37

标签: graphql relayjs

我有一个特定的用例,我希望将新的graphql babel transform since it provides graphql query name lookup but without createFragmentContainer. Is there any way to write a converter from gaphql用于Relay.QL`?

我查看了两个Relay.QL and graphql的输出,但无法找到转换graphql的好方法to Relay.QL

1 个答案:

答案 0 :(得分:0)

我在这里想到的是我编写的代码示例

const convertToClassicQuery = query => query.classic(RelayQL_GENERATED).node;

这是一个小型库,用于将createContainer转换为接受中继现代查询

import Relay from 'react-relay/classic';

const RelayQL_GENERATED = require('react-relay/classic').QL; // eslint-disable-line

const convertToClassicQuery = query => query.classic(RelayQL_GENERATED).node;

const convertFragments = fragments => _.mapValues(fragments, fragment => () => convertToClassicQuery(fragment()));

const convertFragmentProps = (fragmentProps) => {
  fragmentProps.fragments = convertFragments(fragmentProps.fragments); // eslint-disable-line
  return fragmentProps;
};
const createContainer = (Component, fragmentProps) => Relay.createContainer(Component, convertFragmentProps(fragmentProps));


module.exports = {
  createContainer,
};