繁体   English   中英

中继:在 Fragment 中使用常量而不是 graphql`...`

[英]Relay: use constant inside Fragment instead of graphql`...`

中继createFragmentContainer是一个有用的功能,它很容易使用:

const MyComponent = createFragmentContainer(
    MyFragmentComponent,
    {
        job: graphql`
            fragment MyComponent_job on Job {
                id
            }
        `
    }
);

问题是当查询位于我的文件末尾时,真的很难阅读代码。 我更喜欢在导入后将它放在右上角。 像这样:

const QUERY_FRAGMENT = graphql`
    fragment MyComponent_job on Job {
        id
    }     
`
// Main code here

const MyComponent = createFragmentContainer(
    MyFragmentComponent,
    {
        job: QUERY_FRAGMENT
    }
);

但在这种情况下,中继编译器会抛出错误: FindGraphQLTags: 'createFragmentContainer' expects fragment definitions to be 'key: graphql'.

有没有办法将createFragmentContainergraphql分开?

这似乎是babel-plugin-relay一个已知问题。 本期所述,解决方法是更改​​您的导入:

import Relay, { graphql } from 'react-relay'

const fragment = graphql`...`

...

Relay.createFragmentContainer(Component, fragment)

暂无
暂无

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

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